//--------------------------------------------------------------------- public static void ReadSoilDepthMap(string path) { IInputRaster <DoublePixel> map = MakeDoubleMap(path); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); int mapValue = (int)pixel.MapCode.Value; if (site.IsActive) { if (mapValue <= 0 || mapValue > 300) { throw new InputValueException(mapValue.ToString(), "Soil depth value {0} is not between {1:0.0} and {2:0.0}. Site_Row={3:0}, Site_Column={4:0}", mapValue, 0, 300, site.Location.Row, site.Location.Column); } SiteVars.SoilDepth[site] = mapValue; } } } }
public static void ReadWoodyDebrisFromMap(string path) { IInputRaster <DoublePixel> map = MakeDoubleMap(path); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); int mapValue = (int)pixel.MapCode.Value; if (site.IsActive) { if (mapValue < minWoodyDebris || mapValue > maxWoodyDebris) { throw new InputValueException(mapValue.ToString(), "Down dead value {0} is not between {1:0.0} and {2:0.0}. Site_Row={3:0}, Site_Column={4:0}", mapValue, minWoodyDebris, maxWoodyDebris, site.Location.Row, site.Location.Column); } SiteVars.WoodyDebris[site].InitialMass = mapValue; SiteVars.WoodyDebris[site].Mass = mapValue; } } } }
//--------------------------------------------------------------------- public static void ReadWiltingPointMap(string path) { IInputRaster <DoublePixel> map = MakeDoubleMap(path); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 0.75) { throw new InputValueException(mapValue.ToString(), "Soil wilting point value {0} is not between {1:0.0} and {2:0.00}. Site_Row={3:0}, Site_Column={4:0}", mapValue, 0.0, 0.75, site.Location.Row, site.Location.Column); } if (mapValue > SiteVars.SoilFieldCapacity[site]) { throw new InputValueException(mapValue.ToString(), "Wilting Point {0} is greater than field capacity {1:0.0}. Site_Row={2:0}, Site_Column={3:0}", mapValue, SiteVars.SoilFieldCapacity[site], site.Location.Row, site.Location.Column); } SiteVars.SoilWiltingPoint[site] = mapValue; } } } }
//--------------------------------------------------------------------- public Band(Gdal.RasterBand band, IInputRaster raster) : base(band) { this.raster = raster; fromBytes = new FromBytesMethod <T>(new U().FromBytes); }
//--------------------------------------------------------------------- public static void ReadSoilStormFlowMap(string path) { if (PlugIn.StormFlowOverride > 0.0) { foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { SiteVars.SoilStormFlowFraction[site] = PlugIn.StormFlowOverride; } return; } IInputRaster <DoublePixel> map = MakeDoubleMap(path); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 1.0) { throw new InputValueException(mapValue.ToString(), "Soil storm flow value {0} is not between {1:0.0} and {2:0.0}. Site_Row={3:0}, Site_Column={4:0}", mapValue, 0.0, 1.0, site.Location.Row, site.Location.Column); } SiteVars.SoilStormFlowFraction[site] = mapValue; } } } }
//--------------------------------------------------------------------- /// <summary> /// Initializes a new instance using an input raster with ecoregion /// pixels. /// </summary> public InputGrid(IInputRaster <Pixel> raster, IDataset ecoregions) : base(raster.Dimensions) { this.raster = raster; this.ecoregions = ecoregions; // Initialize pixel location so the next call to RowMajor.Next // will return upper-left location (1,1) this.pixelLocation = new Location(1, 0); }
//--------------------------------------------------------------------- /// <summary> /// Initializes a new instance. /// </summary> /// <param name="path"> /// Path to the raster file that represents the map. /// </param> /// <param name="ecoregions">Dataset of ecoregions</param> public Map(string path, IDataset ecoregions) { this.path = path; this.ecoregions = ecoregions; IInputRaster <Pixel> map = Util.Raster.Open <Pixel>(path); using (map) { this.metadata = map.Metadata; } }
//--------------------------------------------------------------------- public static IInputRaster <T> Open <T>(string path) where T : IPixel, new() { try { IInputRaster <T> raster = driver.Open <T>(path); return(raster); } catch (System.IO.IOException exc) { string mesg = string.Format("Error opening map \"{0}\"", path); throw new MultiLineException(mesg, exc); } }
//--------------------------------------------------------------------- public static void ReadDeadWoodMaps(string surfacePath, string soilPath) { IInputRaster <DoublePixel> map = MakeDoubleMap(surfacePath); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 50000.0) { throw new InputValueException(mapValue.ToString(), "SurfDeadWood value {0} is not between {1:0.0} and {2:0.0}. Site_Row={3:0}, Site_Column={4:0}", mapValue, 0.0, 50000.0, site.Location.Row, site.Location.Column); } SiteVars.SurfaceDeadWood[site].Carbon = mapValue * 0.47; SiteVars.SurfaceDeadWood[site].Nitrogen = mapValue * 0.47 / 200.0; // 200 is a generic wood CN ratio SiteVars.SurfaceStructural[site].Carbon = SiteVars.SurfaceDeadWood[site].Carbon * 0.85 * PlugIn.Parameters.InitialFineFuels; SiteVars.SurfaceStructural[site].Nitrogen = SiteVars.SurfaceStructural[site].Carbon / OtherData.StructuralCN; SiteVars.SurfaceMetabolic[site].Carbon = SiteVars.SurfaceDeadWood[site].Carbon * 0.15 * PlugIn.Parameters.InitialFineFuels; SiteVars.SurfaceMetabolic[site].Nitrogen = SiteVars.SurfaceMetabolic[site].Carbon / 10; // a generic metabolic CN ratio } } } map = MakeDoubleMap(soilPath); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 50000.0) { throw new InputValueException(mapValue.ToString(), "SoilDeadWood value {0} is not between {1:0.0} and {2:0.0}. Site_Row={3:0}, Site_Column={4:0}", mapValue, 0.0, 50000.0, site.Location.Row, site.Location.Column); } SiteVars.SoilDeadWood[site].Carbon = mapValue * 0.47; SiteVars.SoilDeadWood[site].Nitrogen = mapValue * 0.47 / 200.0; // 200 is a generic wood CN ratio } } } }
//--------------------------------------------------------------------- /// <summary> /// Initializes a new instance. /// </summary> /// <param name="path"> /// Path to the raster file that represents the map. /// </param> /// <param name="ecoregions"> /// The dataset of ecoregions that are in the map. /// </param> /// <param name="rasterFactory"> /// The raster factory to use to read the map. /// </param> public Map(string path, IDataset ecoregions, IRasterFactory rasterFactory) { this.path = path; this.ecoregions = ecoregions; this.rasterFactory = rasterFactory; IInputRaster <Pixel> map = rasterFactory.OpenRaster <Pixel>(path); using (map) { this.metadata = map.Metadata; } }
//--------------------------------------------------------------------- public static void ReadDeadWoodMaps(string surfacePath, string soilPath) { IInputRaster <DoublePixel> map = MakeDoubleMap(surfacePath); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 50000.0) { throw new InputValueException(mapValue.ToString(), "SurfDeadWood value {0} is not between {1:0.0} and {2:0.0}", mapValue, 0.0, 50000.0); } SiteVars.SurfaceDeadWood[site].Carbon = mapValue * 0.47; SiteVars.SurfaceDeadWood[site].Nitrogen = mapValue * 0.47 / 200.0; // 200 is a generic wood CN ratio } } } map = MakeDoubleMap(soilPath); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 50000.0) { throw new InputValueException(mapValue.ToString(), "SoilDeadWood value {0} is not between {1:0.0} and {2:0.0}", mapValue, 0.0, 50000.0); } SiteVars.SoilDeadWood[site].Carbon = mapValue * 0.47; SiteVars.SoilDeadWood[site].Nitrogen = mapValue * 0.47 / 200.0; // 200 is a generic wood CN ratio } } } }
//--------------------------------------------------------------------- public static void ReadWiltingPointMap(string path) { IInputRaster <DoublePixel> map = MakeDoubleMap(path); //try //{ // map = PlugIn.ModelCore.OpenRaster<DoublePixel>(path); //} //catch (FileNotFoundException) //{ // string mesg = string.Format("Error: The file {0} does not exist", path); // throw new System.ApplicationException(mesg); //} //if (map.Dimensions != PlugIn.ModelCore.Landscape.Dimensions) //{ // string mesg = string.Format("Error: The input map {0} does not have the same dimension (row, column) as the ecoregions map", path); // throw new System.ApplicationException(mesg); //} using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 0.75) { throw new InputValueException(mapValue.ToString(), "Soil wilting point value {0} is not between {1:0.0} and {2:0.00}. Site_Row={3:0}, Site_Column={4:0}", mapValue, 0.0, 0.75, site.Location.Row, site.Location.Column); } if (mapValue > SiteVars.SoilFieldCapacity[site]) { throw new InputValueException(mapValue.ToString(), "Wilting Point {0} is greater than field capacity {1:0.0}. Site_Row={3:0}, Site_Column={4:0}", mapValue, SiteVars.SoilFieldCapacity[site], site.Location.Row, site.Location.Column); } SiteVars.SoilWiltingPoint[site] = mapValue; } } } }
//--------------------------------------------------------------------- //--------------------------------------------------------------------- /// <summary> /// Reads the initial communities map, finds all unique site keys, and sets aside sites to process first and second /// </summary> private void ProcessInitialCommunitiesMap(string initialCommunitiesMap, Landis.Library.InitialCommunities.IDataset communities, ref List <ActiveSite> processFirst, ref List <ActiveSite> processSecond) { IInputRaster <uintPixel> map = ModelCore.OpenRaster <uintPixel>(initialCommunitiesMap); Dictionary <uint, ActiveSite> uniqueKeys = new Dictionary <uint, ActiveSite>(); using (map) { uintPixel pixel = map.BufferPixel; foreach (Site site in ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); uint mapCode = pixel.MapCode.Value; if (!site.IsActive) { continue; } ActiveSite activeSite = (ActiveSite)site; var initialCommunity = communities.Find(mapCode); if (initialCommunity == null) { throw new ApplicationException(string.Format("Unknown map code for initial community: {0}", mapCode)); } sitesAndCommunities.Add(activeSite, initialCommunity); uint key = SiteCohorts.ComputeKey((ushort)initialCommunity.MapCode, Globals.ModelCore.Ecoregion[site].MapCode); if (!uniqueKeys.ContainsKey(key)) { uniqueKeys.Add(key, activeSite); processFirst.Add(activeSite); } else { processSecond.Add(activeSite); } if (!allKeys.ContainsKey(activeSite)) { allKeys.Add(activeSite, key); } } } }
//--------------------------------------------------------------------- /// <summary> /// Creates a site variable with ecoregions. /// </summary> public ISiteVar <IEcoregion> CreateSiteVar(ILandscape landscape) { ISiteVar <IEcoregion> siteVar = landscape.NewSiteVar <IEcoregion>(); IInputRaster <Pixel> map = Util.Raster.Open <Pixel>(path); foreach (Site site in landscape.AllSites) { Pixel pixel = map.ReadPixel(); if (site.IsActive) { ushort mapCode = pixel.Band0; siteVar[site] = ecoregions.Find(mapCode); } } map.Close(); return(siteVar); }
//--------------------------------------------------------------------- /// <summary> /// Creates a site variable with ecoregions. /// </summary> public ISiteVar <IEcoregion> CreateSiteVar(ILandscape landscape) { ISiteVar <IEcoregion> siteVar = landscape.NewSiteVar <IEcoregion>(); IInputRaster <Pixel> map = rasterFactory.OpenRaster <Pixel>(path); using (map) { foreach (Site site in landscape.AllSites) { Pixel pixel = map.ReadPixel(); if (site.IsActive) { ushort mapCode = pixel.Band0; siteVar[site] = ecoregions.Find(mapCode); } } } return(siteVar); }
//--------------------------------------------------------------------- /// <summary> /// Reads a fine-scale input map using majority rule. /// </summary> /// <param name="map"> /// The input map to read. /// </param> /// <param name="landscape"> /// The landscape that the input map is associated with. /// </param> /// <param name="initSiteMethod"> /// The method that's called to process a map code for a site. /// </param> public static void ReadWithMajorityRule <TPixel>(IInputRaster <TPixel> map, ILandscape landscape, Delegates.InitializeSite initSiteMethod) where TPixel : SingleBandPixel <ushort>, new() { BlockRowBuffer <IDictionary <ushort, int> > codeCountBuffer = GetCodeCountBuffer(landscape); Location lowerRight = new Location(landscape.BlockSize, landscape.BlockSize); foreach (Site site in landscape.AllSites) { TPixel pixel = map.ReadPixel(); if (site.IsActive) { ActiveSite activeSite = (ActiveSite)site; ushort mapCode = pixel.Band0; if (activeSite.SharesData) { // Increment the count for the current map code. Location blockLocation = activeSite.BroadScaleLocation; IDictionary <ushort, int> codeCounts = codeCountBuffer[blockLocation.Column]; int count; codeCounts.TryGetValue(mapCode, out count); codeCounts[mapCode] = count + 1; if (activeSite.LocationInBlock == lowerRight) { // Last site in the block, so select one of // map codes by majority rule. ushort selectedMapCode = MajorityRule.SelectMapCode(codeCounts); initSiteMethod(activeSite, selectedMapCode); codeCounts.Clear(); } } else { // Active site has a unique data index initSiteMethod(activeSite, mapCode); } } } }
public void Run() { Console.WriteLine("Running the example LANDIS-II extension..."); Console.WriteLine("Ecoregion codes:"); foreach (Site site in core.Landscape.AllSites) { Console.Write(" {0}", core.EcoregionCodes[site]); if (site.Location.Column == core.Landscape.Columns) { Console.WriteLine(); } } Console.WriteLine(); short[,] data = new short[90, 140]; int rows = data.GetLength(0); int columns = data.GetLength(1); Dimensions dimensions = new Dimensions(rows, columns); for (int r = 0; r < rows; ++r) { for (int c = 0; c < columns; ++c) { data[r, c] = (short)((c * 100) + r); } } string rasterPath = "slope-aspect.img"; using (IOutputRaster <SlopeAspectPixel> outputRaster = core.CreateRaster <SlopeAspectPixel>(rasterPath, dimensions)) { SlopeAspectPixel pixel = outputRaster.BufferPixel; for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { pixel.Slope.Value = (float)(data[row, column] / 100.0); pixel.Aspect.Value = data[row, column]; outputRaster.WriteBufferPixel(); } } } Console.WriteLine("Opening input raster \"{0}\":", rasterPath); // Have to use pixel with two float bands because GDAL allows only // one shared data type across all the raster bands when creating // a raster. // // TO DO: Consider changing spatial library's API so that all the // bands in an input or output raster have the same type. using (IInputRaster <SlopeAspectPixel_float> inputRaster = core.OpenRaster <SlopeAspectPixel_float>(rasterPath)) { Console.WriteLine(" Dimensions: {0}", inputRaster.Dimensions); SlopeAspectPixel_float pixel = inputRaster.BufferPixel; int slopeErrors = 0; int aspectErrors = 0; for (int row = 1; row <= inputRaster.Dimensions.Rows; ++row) { for (int column = 1; column <= inputRaster.Dimensions.Columns; ++column) { inputRaster.ReadBufferPixel(); // Check the actual slope and aspect values to expected values float expectedSlope = (float)(data[row - 1, column - 1] / 100.0); if (pixel.Slope.Value != expectedSlope) { Console.WriteLine("({0}, {1}) : expected Slope ({2}) not = actual Slope ({3})", row, column, expectedSlope, pixel.Slope.Value); slopeErrors++; } float expectedAspect = data[row - 1, column - 1]; if (pixel.Aspect.Value != expectedAspect) { Console.WriteLine("({0}, {1}) : expected Aspect ({2}) not = actual Aspect ({3})", row, column, expectedAspect, pixel.Aspect.Value); aspectErrors++; } } } Console.WriteLine("# of mismatched slope values: {0}", slopeErrors); Console.WriteLine("# of mismatched aspect values: {0}", aspectErrors); } }
//--------------------------------------------------------------------- /// <summary> /// Opens the map as an input grid of ecoregion codes. /// </summary> /// <remarks> /// For use in constructing a landscape. /// </remarks> public IInputGrid <EcoregionCode> OpenAsInputGrid() { IInputRaster <Pixel> map = rasterFactory.OpenRaster <Pixel>(path); return(new InputGrid(map, ecoregions)); }
public Byte(Gdal.RasterBand gdalBand, IInputRaster raster) : base(gdalBand, raster) { }
public UShort(Gdal.RasterBand gdalBand, IInputRaster raster) : base(gdalBand, raster) { }
//--------------------------------------------------------------------- /// <summary> /// Opens the map as an input grid of boolean values. /// </summary> /// <remarks> /// For use in constructing a landscape. /// </remarks> public IInputGrid <bool> OpenAsInputGrid() { IInputRaster <Pixel> map = Util.Raster.Open <Pixel>(path); return(new InputGrid(map, ecoregions)); }
public static void ReadSoilCNMaps(string path, string path2, string path3, string path4, string path5, string path6, string path7, string path8) { IInputRaster <DoublePixel> map = MakeDoubleMap(path); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 10000.0) { throw new InputValueException(mapValue.ToString(), "SOM1surf C value {0} is not between {1:0.0} and {2:0.0}", mapValue, 0.0, 10000.0); } SiteVars.SOM1surface[site].Carbon = mapValue; } } } map = MakeDoubleMap(path2); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 500.0) { throw new InputValueException(mapValue.ToString(), "SOM1surf N value {0} is not between {1:0.0} and {2:0.0}", mapValue, 0.0, 500.0); } SiteVars.SOM1surface[site].Nitrogen = mapValue; } } } map = MakeDoubleMap(path3); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 10000.0) { throw new InputValueException(mapValue.ToString(), "SOM1C value {0} is not between {1:0.0} and {2:0.0}", mapValue, 0.0, 10000.0); } SiteVars.SOM1soil[site].Carbon = mapValue; } } } map = MakeDoubleMap(path4); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 500.0) { throw new InputValueException(mapValue.ToString(), "SOM1N value {0} is not between {1:0.0} and {2:0.0}", mapValue, 0.0, 500.0); } SiteVars.SOM1soil[site].Nitrogen = mapValue; } } } map = MakeDoubleMap(path5); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 20000.0) { throw new InputValueException(mapValue.ToString(), "SOM2C value {0} is not between {1:0.0} and {2:0.0}", mapValue, 0.0, 20000.0); } SiteVars.SOM2[site].Carbon = mapValue; } } } map = MakeDoubleMap(path6); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 1000.0) { throw new InputValueException(mapValue.ToString(), "SOM2N value {0} is not between {1:0.0} and {2:0.0}", mapValue, 0.0, 1000.0); } SiteVars.SOM2[site].Nitrogen = mapValue; } } } map = MakeDoubleMap(path7); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 30000.0) { throw new InputValueException(mapValue.ToString(), "SOM3C value {0} is not between {1:0.0} and {2:0.0}", mapValue, 0.0, 20000.0); } SiteVars.SOM3[site].Carbon = mapValue; } } } map = MakeDoubleMap(path8); using (map) { DoublePixel pixel = map.BufferPixel; foreach (Site site in PlugIn.ModelCore.Landscape.AllSites) { map.ReadBufferPixel(); double mapValue = pixel.MapCode.Value; if (site.IsActive) { if (mapValue < 0.0 || mapValue > 1000.0) { throw new InputValueException(mapValue.ToString(), "SOm3N value {0} is not between {1:0.0} and {2:0.0}", mapValue, 0.0, 1000.0); } SiteVars.SOM3[site].Nitrogen = mapValue; } } } }