The following dependencies are required to build the Python bindings:
Unlike the command-line interface, GDAL is not a requirement, although the Python bindings can work with the GDAL Python bindings if they are available.
To install the Python bindings, run pip install . from the root of the source tree.
In most cases, the simplest usage is the exact_extract function:
from exactextract import exact_extract
rast = "elevation.tif"
polys = "watersheds.shp"
exact_extract(rast, polys, ["mean", "min", "max"])This will return a list of GeoJSON-like features with properties containing the
values of the requested stats. exact_extract can understand several different
types of inputs:
-
raster inputs:
- a GDAL
Dataset - a rasterio
DatasetReader - an xarray
DataArray - a path to a file that can be opened with one of the above packages
- a
RasterSourceobject (such as aNumPyRasterSourcethat wraps anumpyarray)
- a GDAL
-
vector inputs:
- a GDAL/OGR
DatasetorDataSource - a fiona
Collection - a geopandas
GeoDataFrame - a path to a file that can be opened with one of the above packages
- a list of GeoJSON-like features
- a
FeatureSourceobject - a
QgsVectorLayerobject
- a GDAL/OGR
The following additional arguments are understood by exact_extract:
weights: a raster representing weights to use for stats such asweighted_meaninclude_cols: a list of columns from the input features to be copied into the output featuresinclude_geom: a flag indicating that the input geometry should be copied into the output featuresstrategy: a processing strategy to use, eitherfeature-sequential(iterate over features and read the pixels that intersect each feature) orraster-sequential(iterate over chunks of the raster and process the features that intersect each chunk)max_cells_in_memory: used to control the number of cells that are loaded at one time, for large featuresoutput: used to control the output format: eithergeojson,pandas,qgisor the name of an output file to write to using GDAL.