geofunctions
Objects
ProcessOptions
@dataclass
class ProcessOptions()
Data class to store all the options for processing.
Attributes:
appendbool - append data to existing records by column, otherwise create new records. Defaults to True.bufferfloat | int - buffer around centre to search pixels,boundsList[float], optional - bbox of raster data will convert buffer value if inputcrsis 4326. Defaults to 1.crsint, optional - input CRS to use for transforming.datestr - input date in YYYY-mm-DD format.geotransformtuple - with transform matrix.id_columnstr, optional - column that is used to identify unique geometries.parametersstr - name of the attribute describing a distance. Can be used instead ofbufferto have a more dynamic search.projectionstr - projection string in proj4 format.simplifyfloat | int - tolerance of simplification of geometry. Defaults to 0.thresholdfloat | int, optional - set athresholdwhich will cause the creation of a mask layer to mask values underneath thethresholdvalue.overwritebool - overwrite existing records. Defaults to False.XSizeint - X dimension of tile in pixels.YSizeint - Y dimension of tile in pixels.ALL_TOUCHEDstr - special GDAL parameter to regard all pixels touching the geometry. Defaults to True
get_bounds
def get_bounds(src: gdal.Dataset) -> List[float]
GIS function to calculate outer bounds of a raster dataset
Arguments:
srcgdal.Dataset - Input raster dataset
Returns:
List[float]- bbox describing outer edge of raster dataset
add_raster_options
def add_raster_options(tile: gdal.Dataset | str)
Add specific GDAL raster options to ProcessOptions
Arguments:
tilegdal.Dataset | str - Input dataset or str pointer to raster file.
Functions
clean_polygons
def clean_polygons(polygons: List[dict], columns: dict | list) -> list
Filters data based on a whether records exists in the columns lookup table.
Arguments:
polygonsList[dict] - input list of polygon recordscolumnsdict | list - snippet of lookup table. Can also be an array of columns.
Returns:
list- filtered polygons
dissolve
def dissolve(geometries: List[str]) -> List[str]
GIS function to dissolve overlapping polygons into larger polygons. Attribute table is not guarded and one polygon will take precidence.
Arguments:
geometriesList[str] - list of wkt strings
Returns:
List[str]- list of dissolved wkt strings length of list might have changed from input
Examples:
dissolve(
geometries=[
"POLYGON((804562.4672512376 688315.5459430797,804562.2603874448 688316.5859175948,804562.4150336159 688316.07611749,804562.4672512376 688315.5459430797))",
"POLYGON((804535.3859031041 688334.3479520245,804535.1279857255 688335.6445902472,804535.3207982739 688335.0089724582,804535.3859031041 688334.3479520245))"
]
)
Notes:
Example geometries aren't necessarilly valid.
polygonize_array
def polygonize_array(data: np.ndarray,
transformation: tuple,
projection: str,
dn_value: int = 1) -> List[str]
GIS function to polygonize a numpy array to geometries
Arguments:
datanp.ndarray - input data array.transformationtuple - transformation matrix of feature.projectionstr - input GDAL projection string src_ds.GetProjection().dn_valueint, optional - input burn value. Defaults to 1.
Returns:
List[str]- list of polygonized WKT strings
Examples:
polygonize_array(
data=numpy.array([[1, 2, 3], [4, 5, 6]]),
transformation=(399960.0, 30.0, 0.0, -2400000.0, 0.0, -30.0),
projection='PROJCS["WGS 84 / UTM zone 21N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-57],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32621"]]',
dn_value=1
)
generate_bounding_box
def generate_bounding_box(envelope: tuple,
options: ProcessOptions) -> dict | None
Function to generate
Arguments:
envelopetuple - source envelope coordinates.optionsProcessOptions - processing options.
Returns:
dict| None: an object holding the transformation matrix and bbox
Examples:
generate_bounding_box(
envelope=(1038796.0904789402, 1038798.0904789402, 1122432.7277924367, 1122434.7277924367),
options=ProcessOptions(
geotransform=(1038604.4586980281, 0.46867100000000056, 0.0, 1123215.2035654117, 0.0, -0.46867100000000894)
XSize=4065,
YSize=5000
)
)
get_dtype
def get_dtype(polygon: dict) -> int
Estimate the numpy array dtype from GDAL or string representing a digit.
Arguments:
polygondict - a polygon record.
Raises:
TypeError- raised if you try to burn funny types.
Returns:
int- the numpy dtype number.
zonal_stats
def zonal_stats(tile: gdal.Dataset,
polygon: dict,
options: ProcessOptions,
attribute_name: str = "id") -> dict
GIS function to calculate zonal statistics from a PostGIS vector table
Arguments:
tilegdal.Dataset - input raster dataset.polygondict - input polygon information, representation is a row from a PostGIS table.optionsProcessOptions - input processing options.attribute_namestr, optional - primary key of vector table, or any other unique ID column. Defaults to "id".
Returns:
dict- output geometry object with polygonized WKT string and corresponding zonal stats data. Is empty if no significant pixels values were found.
Examples:
zonal_stats(
tile=gdal.Dataset,
polygon={
"id": 1,
"polygon": "POLYGON((804562.4672512376 688315.5459430797,804562.2603874448 688316.5859175948,804562.4150336159 688316.07611749,804562.4672512376 688315.5459430797))",
"date": "2021-01-01",
"meandist": 0.5
},
geotransform=(399960.0, 30.0, 0.0, -2400000.0, 0.0, -30.0),
projection='PROJCS["WGS 84 / UTM zone 21N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-57],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32621"]]',
options=ProcessOptions(foo="bar"),
attribute_name=id_0
)
zonal_stats_polygons
def zonal_stats_polygons(tile: gdal.Dataset | str, polygons: List[dict],
rasterlayer: str,
options: ProcessOptions) -> List[dict]
Collector function that cleanses the incoming list of polygons and runs the zonal statistics per polygon. It also parses the zonal statistics return into a uncluttered array of objects. Function also checks which geometries might have been missed.
Arguments:
tilegdal.Dataset | str - Input dataset or str pointer to raster file.polygonsList[dict] - Input list of polygon objects as returned from a PostGIS table.rasterlayerstr - Input name of source raster layer.optionsProcessOptions - Input processing options.
Raises:
Exception- panics on generic exceptions.
Returns:
List[dict]- array of polygon objects
Examples:
zonal_stats_polygons(
tile="/mnt/data/some.tiff",
polygons=
[
{
"id": 1,
"polygon": ("POLYGON(
(
804562.4672512376 688315.5459430797,
804562.4150336159 688316.07611749,
804562.4167345229 688316.076381122,
804562.4672512376 688315.5459430797
)
)"),
"date": "2021-01-01",
"meandist": 0.5
}
],
rasterlayer="objectheightmodel",
options={
"foo": "bar"
}
)
generate_bin_mask
def generate_bin_mask(
tile: gdal.Dataset, threshold: float | int,
bbox: list | tuple = ()) -> np.ndarray
Function to generate a binary mask based on a threshold value
Arguments:
tilegdal.Dataset - input raster data.thresholdfloat | int - the threshold value.bboxlist | tuple, optional - an optional bbox, if not given it will use the entire array. Defaults to ().
Returns:
np.ndarray- masked array of the raster data.