GeoEco.Datasets.Virtual.FastMarchingDistanceGrid
- class GeoEco.Datasets.Virtual.FastMarchingDistanceGrid(grid, minDist=None, maxDist=None)
Bases:
GridA
Gridrepresenting distances to features in anotherGrid, computed with a fast marching algorithm.To compute fast marching distances,
FastMarchingDistanceGridfirst classifies the cells of the providedGridbased on their values:Cells with values of zero or less represent features to which distances should be calculated, called inside cells below.
Cells with positive values represent areas that can be moved through, e.g. the ocean, called outside cells.
Cells of NoData represent obstacles that cannot be moved through (e.g. land), called barrier cells.
The interfaces where inside and outside cells touch each other are called the feature edges. Where inside and outside cells touch along a straight row or column, the edge is a straight line located exactly where the cells meet. When the feature has a “corner”, the edge is rounded and curved within the inside cell.
Fast marching distances are computed for both inside and outside cells. Distances are negative for inside cells and positive for outside cells. Distances are computed as the shortest path from the given cell to the closest edge, and will be positive for outside cells and negative for inside cells. If you instead want the distances for inside cells to zero throughout the feature, set minDist to zero.
Example usage:
# Load the study area raster and get the first band as a GDALRasterGrid. from GeoEco.Datasets.GDAL import GDALDataset dataset = GDALDataset('/home/jason/fast_marching_test/Study_Area.img') grid = dataset.QueryDatasets(reportProgress=False)[0] # This band uses an int32 data type and has values of 0 for land and 1 for # water. Every cell has data. First, extract the band's data as a numpy array. # Then, in that array, set land (0) to NoData. Note that we do not want set # values of grid.Data itself (e.g. grid.Data[...] = ...) because this will # write values into the raster file, which we do not want to change. Also, # Grid does not support indexing with numpy arrays (e.g. data == 0). data = grid.Data[:] data[data == 0] = grid.NoDataValue # We are now representing land as NoData, which FastMarchingDistanceGrid # expects. Now set several cells that are currently marked as 1 to 0. These # new 0 cells represent the feature we will computing distances to. We know # the approximate coordinates of the centers of these cells and use a Grid # function to look up their integer indices, and then use those to set the # cell values. (These are in a projected coordinate system with meters as the # linear unit, which is why they are very large.) cellCoordsYX = [(1607000, 2882000), (1602000, 2882000), (1597000, 2882000), (1592000, 2882000), (1587000, 2882000), (1582000, 2882000), (1577000, 2877000), (1572000, 2877000), (1567000, 2877000), (1562000, 2877000),] cellIndicesYX = [grid.GetIndicesForCoords(coords) for coords in cellCoordsYX] for indices in cellIndicesYX: data[indices[0], indices[1]] = -1 # Create a NumpyGrid from the original band, then write our new data into it. from GeoEco.Datasets import NumpyGrid numpyGrid = NumpyGrid.CreateFromGrid(grid) numpyGrid.Data[:] = data # Construct the FastMarchingDistanceGrid. Set minDist to 0, so that cells # within the feature we defined above will have a distance of 0, rather than a # negative distance (from the cell center to the edge). Constructing this grid # does not perform the calculation; that is deferred until the grid's Data # property is accessed. from GeoEco.Datasets.Virtual import FastMarchingDistanceGrid fmdGrid = FastMarchingDistanceGrid(numpyGrid, minDist=0) # We want to write the FastMarchingDistanceGrid out as a raster with GDAL. # Construct a DirectoryTree that we can import it into. Note that because we # are just going to import this single raster, we can specify the destination # file name in pathCreationExpressions, and not have to define a # QueryableAttribute that gives the file name. from GeoEco.Datasets.Collections import DirectoryTree dirTree = DirectoryTree(path='/home/jason/fast_marching_test', datasetType=GDALDataset, pathCreationExpressions=['Distance.img']) # Import the grid into the DirectoryTree. dirTree.ImportDatasets([fmdGrid], mode='Replace', calculateStatistics=True)
Requires: Python skfmm module.
- Parameters:
grid (
Grid) –Gridfor which distances should be computed. Typically, it has 2 dimensions. If it has 3 or 4 dimensions, distances will be computed for each 2D slice.minDist (
float, optional) – Minimum allowed distance. If it is provided, distances less than this will be rounded up to it.maxDist (
float, optional) – Maximum allowed distance. If it is provided, distances greater than this will be rounded down to it.
- Returns:
FastMarchingDistanceGridinstance.- Return type:
Properties
- property CenterCoords
(
object) Coordinates of the grid cell centers, indexed using the 1-character dimension of interest and optionally arangeto retrieve anumpy.ndarrayof coordinates (e.g.CenterCoords['x', 0:4]) or an integer to retrieve afloatfor a single coordinate (e.g.CenterCoords['x', 10]). Coordinates for thetdimension are returned asdatetimeinstances. Read only.
- property CoordDependencies
(
tupleofstr) Same length asDimensions. Dimensions that each dimension depends on for determining its coordinates.Nonefor dimensions that have a constant coordinate increment. Read only.
- property CoordIncrements
(
tupleoffloat) Same length asDimensions. Coordinate increment for each dimension.Nonefor dimensions that do not have a constant coordinate increment. Read only.
- property Data
(
object) This grid’s data, indexable using slices (e.g.grid.Data[:, 5:10, -10:]) or integers (e.g.grid.Data[0,1,-2]) or both in combination. Strides and negative indexes are supported in the traditional manner. If the grid is writable,Datacan be assigned to write values to the grid, e.g.grid.Data[0,1] = 5orgrid.Data[:,:] = numpy.zeros(grid.Shape). Returns and acceptsnumpy.ndarray,float, andint. Read only.
- property DataIsScaled
(
bool) If True, the underlying raw data are stored as theUnscaledDataTypeto save storage space and then transformed by a scaling equation on the fly when they are returned byData. The raw data can be accessed withUnscaledData. If False, the raw data are returned as is, with no transformation needed, andUnscaledDataTypeandDataTypeare the same, andUnscaledDatareturns the same values asData. Read only.
- property DataType
(
str) Numeric data type of the grid, after the scaling function (if any) has been applied to the raw data.numpy.ndarrays returned byDatahave this type. Read only. Allowed values꞉'int8','uint8','int16','uint16','int32','uint32','float32','float64'. Case sensitive.
- property Dimensions
(
str) Dimensions of this grid. Read only. Allowed values꞉'yx','zyx','tyx','tzyx'. Case sensitive.
- property DisplayName
(
str) Informal name of this object, suitable to be displayed to the user. Read only. Minimum length꞉ 1.
- property MaxCoords
(
object) Maximum coordinate value for each cell (i.e., the coordinates of the cells’ right edges), indexed using the 1-character dimension of interest and optionally arangeto retrieve anumpy.ndarrayof coordinates (e.g.MaxCoords['x', 0:4]) or an integer to retrieve afloatfor a single coordinate (e.g.MaxCoords['x', 10]). Coordinates for thetdimension are returned asdatetimeinstances. Read only.
- property MaxDist
(
floatorNone) Maximum allowed distance. If it is provided, distances greater than this will be rounded down to it. Read only.
- property MinCoords
(
object) Minimum coordinate value for each cell (i.e., the coordinates of the cells’ left edges), indexed using the 1-character dimension of interest and optionally arangeto retrieve anumpy.ndarrayof coordinates (e.g.MinCoords['x', 0:4]) or an integer to retrieve afloatfor a single coordinate (e.g.MinCoords['x', 10]). Coordinates for thetdimension are returned asdatetimeinstances. Read only.
- property MinDist
(
floatorNone) Minimum allowed distance. If it is provided, distances less than this will be rounded up to it. Read only.
- property NoDataValue
(
objectorNone)int,float, or single-element numpy array giving the value that indicates that cells ofDatashould be interpreted as having no data (these are also known as missing, NA, or NULL cells), orNoneif all cells must have data. Read only.
- property ParentCollection
(
DatasetCollectionorNone) ParentDatasetCollectionthat this object is part of (if any). Read only.
- property Shape
(
tupleofint) Same length asDimensions. Length (number of grid cells) of each dimension. Read only.
- property TCountPerSemiRegularPeriod
(
intorNone) Number of time slices per semi-regular period (i.e. per year).Noneif the grid’s dimensions do not contain atcoordinate or thetcoordinate is not semi-regular. Read only.
- property TIncrementUnit
(
strorNone) Unit of thetcoordinate.Noneif the grid’s dimensions do not contain atcoordinate. Read only. Allowed values꞉'year','month','day','hour','minute','second'. Case sensitive.
- property TSemiRegularity
(
strorNone) Type of semi-regularity used for thetcoordinate.Noneif the grid’s dimensions do not contain atcoordinate or thetcoordinate is not semi-regular. Read only. Allowed values꞉'annual'. Case sensitive.
- property UnscaledData
(
object) This grid’s data underlying raw data, before it has been transformed by a scaling equation.UnscaledDatais indexable using slices (e.g.grid.UnscaledData[:, 5:10, -10:]) or integers (e.g.grid.UnscaledData[0,1,-2]) or both in combination. Strides and negative indexes are supported in the traditional manner. If the grid is writable,UnscaledDatacan be assigned to write values to the grid, e.g.grid.UnscaledData[0,1] = 5orgrid.UnscaledData[:,:] = numpy.zeros(grid.Shape). Returns and acceptsnumpy.ndarray,float, andint. Read only.
- property UnscaledDataType
(
str) Numeric data type of the grid’s raw data, before it has been transformed by a scaling equation.numpy.ndarrays returned byUnscaledDatahave this type. If no transformation is needed (DataIsScaledis False), thenUnscaledDataTypeandScaledDataTypeare the same, andUnscaledDatareturns the same values asData. Read only. Allowed values꞉'int8','uint8','int16','uint16','int32','uint32','float32','float64'. Case sensitive.
- property UnscaledNoDataValue
(
objectorNone)intorfloatvalue that indicates that cells ofUnscaledDatashould be interpreted as having no data (these are also known as missing, NA, or NULL cells), orNoneif all cells must have data. Read only.
Methods
Closes any open files or connections associated with this object and releases any other resources allocated to access it.
Converts a spatial reference from one format to another, such as an OGC WKT string to a Proj4 string.
Deletes the lazy property with the specified name.
Returns a list of all queryable attributes.
Given a
tupleorlistof coordinates, returns alistofintindices intoDatafor the cell that contains the coordinates.Returns the value of the lazy property with the specified name.
Returns the queryable attribute with the specified name.
Returns the value of the queryable attribute with the specified name.
Returns a list queryable attributes having the specified data type.
Returns the spatial reference of this dataset.
Returns True if the specified lazy property has a value.
Sets the lazy property with the specified name to the specified value.
Sets the spatial reference of this dataset.
Tests whether a capability is supported by this class or an instance of it.