Пример #1
0
        //---------------------------------------------------------------------

        //Modified to add inputMapPath, allowing users to specify raster paths to change at timestep
        public void ProcessInputMap(ProcessLandUseAt processLandUseAt)
        {
            string inputMapPath = MapNames.ReplaceTemplateVars(inputMapTemplate, Model.Core.CurrentTime);

            Model.Core.UI.WriteLine("  Reading map \"{0}\"...", inputMapPath);

            IInputRaster <MapPixel>  inputMap;
            Dictionary <string, int> counts = new Dictionary <string, int>();

            using (inputMap = Model.Core.OpenRaster <MapPixel>(inputMapPath))
            {
                MapPixel pixel = inputMap.BufferPixel;
                foreach (Site site in Model.Core.Landscape.AllSites)
                {
                    inputMap.ReadBufferPixel();
                    if (site.IsActive)
                    {
                        LandUse landUse = LandUseRegistry.LookUp(pixel.LandUseCode.Value);
                        if (landUse == null)
                        {
                            string message = string.Format("Error: Unknown map code ({0}) at pixel {1}",
                                                           pixel.LandUseCode.Value,
                                                           site.Location);
                            throw new System.ApplicationException(message);
                        }
                        string key = processLandUseAt(site, landUse);
                        if (key != null)
                        {
                            int count;
                            if (counts.TryGetValue(key, out count))
                            {
                                count = count + 1;
                            }
                            else
                            {
                                count = 1;
                            }
                            counts[key] = count;
                        }
                    }
                }
            }
            foreach (string key in counts.Keys)
            {
                Model.Core.UI.WriteLine("    {0} ({1:#,##0})", key, counts[key]);
            }
        }
Пример #2
0
        //---------------------------------------------------------------------

        protected void ReadLandUses()
        {
            InputVar <string> name                = new InputVar <string>("LandUse");
            InputVar <ushort> mapCode             = new InputVar <ushort>("MapCode");
            InputVar <bool>   allowHarvest        = new InputVar <bool>("AllowHarvest?");
            InputVar <string> landCoverChangeType = new InputVar <string>("LandCoverChange");

            Dictionary <string, int> nameLineNumbers    = new Dictionary <string, int>();
            Dictionary <ushort, int> mapCodeLineNumbers = new Dictionary <ushort, int>();

            PartialThinning.InitializeClass();
            while (!AtEndOfInput)
            {
                int nameLineNum = LineNumber;
                ReadVar(name);
                int lineNumber;
                if (nameLineNumbers.TryGetValue(name.Value.Actual, out lineNumber))
                {
                    throw new InputValueException(name.Value.String,
                                                  "The land use \"{0}\" was previously used on line {1}",
                                                  name.Value.Actual, lineNumber);
                }
                else
                {
                    nameLineNumbers[name.Value.Actual] = nameLineNum;
                }

                int mapCodeLineNum = LineNumber;
                ReadVar(mapCode);
                if (mapCodeLineNumbers.TryGetValue(mapCode.Value.Actual, out lineNumber))
                {
                    throw new InputValueException(mapCode.Value.String,
                                                  "The map code \"{0}\" was previously used on line {1}",
                                                  mapCode.Value.Actual, lineNumber);
                }
                else
                {
                    mapCodeLineNumbers[mapCode.Value.Actual] = mapCodeLineNum;
                }

                ReadVar(allowHarvest);

                // By default, a land use allows trees to establish.
                bool allowEstablishment = true;
                if (ReadPreventEstablishment())
                {
                    allowEstablishment = false;
                }

                Dictionary <string, LandCover.IChange> landCoverChanges =
                    new Dictionary <string, LandCover.IChange>();
                List <LandCover.IChange> landCoverList = new List <LandCover.IChange>();
                ReadVar(landCoverChangeType);
                LandCover.IChange landCoverChange = ProcessLandCoverChange(landCoverChangeType);
                landCoverChanges[landCoverChange.Type] = landCoverChange;
                landCoverList.Add(landCoverChange);
                while (ReadOptionalVar(landCoverChangeType)) //Get extra LandCoverChanges
                {
                    if (landCoverChanges.TryGetValue(landCoverChangeType.Value.Actual, out landCoverChange))
                    {
                        throw new InputValueException(landCoverChangeType.Value.String,
                                                      "The land cover change \"{0}\" has already been defined for land use: {1}",
                                                      landCoverChangeType.Value.Actual, name.Value.Actual);
                    }
                    else
                    {
                        landCoverChange = ProcessLandCoverChange(landCoverChangeType);
                        landCoverChanges[landCoverChange.Type] = landCoverChange;
                        landCoverList.Add(landCoverChange);
                    }
                }

                LandCover.IChange[] changes = new LandCover.IChange[landCoverList.Count];
                for (int i = 0; i < landCoverList.Count; i++)
                {
                    changes[i] = landCoverList[i];
                }

                LandUse landUse = new LandUse(name.Value.Actual,
                                              mapCode.Value.Actual,
                                              allowHarvest.Value.Actual,
                                              allowEstablishment,
                                              changes);
                LandUseRegistry.Register(landUse);
            }
        }
        //---------------------------------------------------------------------

        protected void ReadLandUses()
        {
            InputVar <string> name                = new InputVar <string>("LandUse");
            InputVar <ushort> mapCode             = new InputVar <ushort>("MapCode");
            InputVar <bool>   allowHarvest        = new InputVar <bool>("AllowHarvest?");
            InputVar <string> landCoverChangeType = new InputVar <string>("LandCoverChange");

            Dictionary <string, int> nameLineNumbers    = new Dictionary <string, int>();
            Dictionary <ushort, int> mapCodeLineNumbers = new Dictionary <ushort, int>();

            while (!AtEndOfInput)
            {
                int nameLineNum = LineNumber;
                ReadVar(name);
                int lineNumber;
                if (nameLineNumbers.TryGetValue(name.Value.Actual, out lineNumber))
                {
                    throw new InputValueException(name.Value.String,
                                                  "The land use \"{0}\" was previously used on line {1}",
                                                  name.Value.Actual, lineNumber);
                }
                else
                {
                    nameLineNumbers[name.Value.Actual] = nameLineNum;
                }

                int mapCodeLineNum = LineNumber;
                ReadVar(mapCode);
                if (mapCodeLineNumbers.TryGetValue(mapCode.Value.Actual, out lineNumber))
                {
                    throw new InputValueException(mapCode.Value.String,
                                                  "The map code \"{0}\" was previously used on line {1}",
                                                  mapCode.Value.Actual, lineNumber);
                }
                else
                {
                    mapCodeLineNumbers[mapCode.Value.Actual] = mapCodeLineNum;
                }

                ReadVar(allowHarvest);

                // By default, a land use allows trees to establish.
                bool allowEstablishment = true;

                ReadVar(landCoverChangeType);
                LandCover.IChange landCoverChange = null;
                if (landCoverChangeType.Value.Actual == LandCover.NoChange.TypeName)
                {
                    landCoverChange = noLandCoverChange;
                }
                else if (landCoverChangeType.Value.Actual == LandCover.RemoveTrees.TypeName)
                {
                    ICohortSelector selector = ReadSpeciesAndCohorts("LandUse",
                                                                     ParameterNames.Plant,
                                                                     ParameterNames.PreventEstablishment);
                    ICohortCutter cohortCutter = CohortCutterFactory.CreateCutter(selector,
                                                                                  Main.ExtType);
                    Planting.SpeciesList speciesToPlant = ReadSpeciesToPlant();
                    landCoverChange = new LandCover.RemoveTrees(cohortCutter, speciesToPlant);

                    if (ReadPreventEstablishment())
                    {
                        allowEstablishment = false;
                    }
                }
                else
                {
                    throw new InputValueException(landCoverChangeType.Value.String,
                                                  "\"{0}\" is not a type of land cover change",
                                                  landCoverChangeType.Value.Actual);
                }

                LandUse landUse = new LandUse(name.Value.Actual,
                                              mapCode.Value.Actual,
                                              allowHarvest.Value.Actual,
                                              allowEstablishment,
                                              landCoverChange);
                LandUseRegistry.Register(landUse);
            }
        }