コード例 #1
0
ファイル: Program.cs プロジェクト: TsaiYoung/iSGMS
        List <double> suvProbility = new List <double>();     //间隔期后枯损概率

        private void StartGrowth()
        {
            //读取DEM
            dem.ReadDEM(Dempath);
            Console.WriteLine("Read DEM succeed!");
            //读取样地shp,或林场shp
            int standNum = forest.Readshp(Shppath);

            Console.WriteLine("Read shapefile succeed");
            //对活动按时间顺序排序
            MActives.Sort((left, right) => left.cuttingAge.CompareTo(right.cuttingAge));

            //ParallelLoopResult parallelresult = Parallel.For(0, standNum, forestId =>
            for (int forestId = 0; forestId < standNum; forestId++)
            {
                List <Tree> arrayTrees = new List <Tree>();

                if (forest.type == "points")
                {
                    arrayTrees = forest.getTrees();
                    Console.WriteLine("Get trees informations!");
                }
                else if (forest.type == "polygons")
                {
                    arrayTrees = forest.createTrees(dem, forestId);
                    Console.WriteLine("Get trees informations!");
                }
                else
                {
                    return;
                }

                //如果没有树高值,先计算
                if (arrayTrees[0].Height <= 0)
                {
                    arrayTrees = CalcuHeight(arrayTrees, heightModelParams, startage, forest.forestArea[forestId]);
                }

                List <Tree> initTrees = new List <Tree>();
                for (int i = 0; i < arrayTrees.Count; i++)
                {
                    initTrees.Add(arrayTrees[i]);
                }

                int  cutCount    = 0; //第几次砍伐
                bool isNumChange = true;
                for (int iage = startage + 1; iage <= endage; iage++)
                {
                    Console.WriteLine("Start to calculate the growth in " + iage + " years.");

                    //标记边界木
                    if (isNumChange)
                    {
                        //划分空间结构单元
                        unit.DivideUnit(arrayTrees);
                        for (int i = 0; i < unit.edgeTree.Count; i++)
                        {
                            int iTree = unit.edgeTree[i];
                            arrayTrees[iTree].isEdge = true;
                        }
                        isNumChange = false;

                        //空间结构单元按中心木排序
                        unit.near4Units.Sort((left, right) => left.centerID.CompareTo(right.centerID));
                        unit.allUnits.Sort((left, right) => left.centerID.CompareTo(right.centerID));
                    }

                    //计算胸径
                    arrayTrees = CalcuDBH(arrayTrees, unit.near4Units, dem, dbhModelParams, iage, forest.forestArea[forestId], SI, D0, minTg, meanPg, canopydensity, MT, VT, TS, BP);
                    Console.WriteLine("Calculate the DBH of stand " + forestId + " in year " + iage);

                    //计算树高
                    arrayTrees = CalcuHeight(arrayTrees, heightModelParams, iage, forest.forestArea[forestId]);
                    Console.WriteLine("Calculate the Height of stand " + forestId + " in year " + iage);

                    //计算枝下高
                    arrayTrees = CalcuUBH(arrayTrees, ubhModelParams, iage, forest.forestArea[forestId]);
                    Console.WriteLine("Calculate the UBH of stand " + forestId + " in year " + iage);

                    // 计算冠高
                    if (UBHmodeltype == (int)enUBHmodels.MaModel)
                    {
                        modelLibrary.CHmodels.CHGrowthModel ch = new modelLibrary.CHmodels.CHGrowthModel();
                        arrayTrees = ch.CalcuCrownHeight(unit, arrayTrees, chModelParams, iage);
                        Console.WriteLine("Calculate the CH of stand " + forestId + " in year " + iage);
                    }

                    // 计算冠幅
                    arrayTrees = CalcuCW(arrayTrees, cwModelParams, iage, forest.forestArea[forestId]);
                    Console.WriteLine("Calculate the CW of stand " + forestId + " in year " + iage);

                    // 计算生物量
                    arrayTrees = CalcuBiomass(arrayTrees, biomassParams);
                    Console.WriteLine("Calculate the Biomass of stand " + forestId + " in year " + iage);

                    //枯损(估算枯损情况)
                    if ((iage - startage) % interval == 0)
                    {
                        List <double> DIN = new List <double>();
                        for (int j = 0; j < arrayTrees.Count; j++)
                        {
                            DIN.Add(arrayTrees[j].DBH - initTrees[j].DBH);      //间隔期胸径增长量
                            initTrees[j] = arrayTrees[j];                       //更新
                        }
                        suvProbility = CalcuMortality(arrayTrees, mortalityParams, unit.allUnits, iage, forest.forestArea[forestId], SI, DIN);
                        for (int j = 0; j < arrayTrees.Count; j++)
                        {
                            if (suvProbility[j] < threshold)
                            {
                                arrayTrees[j].isMortality = true;
                                isNumChange = true;
                            }
                        }
                        Console.WriteLine("Predict " + "the mortality probility of " + forestId + " stand " + " in " + iage + " year.");
                    }

                    //砍伐
                    if (MActives.Count > 0 && cutCount < MActives.Count && iage == MActives[cutCount].cuttingAge)
                    {
                        Management manager = new Management(arrayTrees, unit.near4Units, MActives[cutCount].cuttingType, MActives[cutCount].cutWeights, MActives[cutCount].intensity);     //添加经营方面的各种参数
                        arrayTrees = manager.MarkCuttingTree();
                        cutCount++;
                        isNumChange = true;

                        Console.WriteLine("Calculate the Cutting of stand " + forestId + " in year " + iage);

                        //output 不同小班数据
                        string path = outputpath + "\\Age" + iage + "Stand" + forestId + "Cutting" + cutCount + ".shp";
                        Console.WriteLine("Output: " + path);

                        forest.WriteShp(path, arrayTrees);
                    }

                    //到间隔期
                    if ((iage - startage) % interval == 0)
                    {
                        //output 不同小班数据
                        string path = outputpath + "\\Age" + iage + "Stand" + forestId + ".shp";
                        Console.WriteLine("Output: " + path);

                        forest.WriteShp(path, arrayTrees);
                    }

                    //林木数量更新(倒序删除,防止删除后序号变化)
                    if (isNumChange)
                    {
                        Console.WriteLine("The number of forest trees have changed!");
                        arrayTrees = treesUpdate(arrayTrees);
                    }
                    else
                    {
                        Console.WriteLine("The number of forest trees no changes!");
                    }
                }
            }
        }