exactextract package
Submodules
exactextract.exact_extract module
- exactextract.exact_extract.exact_extract(rast, vec, ops, *, weights=None, include_cols=None, include_geom=False, strategy: str = 'feature-sequential', max_cells_in_memory: int = 30000000, grid_compat_tol: float = 0.001, output: str = 'geojson', output_options: Mapping | None = None, progress=False)[source]
Calculate zonal statistics.
- Parameters:
rast – One or a list of
RasterSource
object(s) or filename(s) that can be opened by GDAL/rasterio/xarray.vec – A
FeatureSource
or filename that can be opened by GDAL/fionaops – A list of
Operation
objects, or strings that can be used to construct them (e.g.,"mean"
,"quantile(q=0.33)"
). Check out Available operations for more information.weights – An optional
RasterSource
or filename for weights to be used in weighted operations.include_cols – An optional list of columns from the input features to be included into the output.
include_geom – Flag indicating whether the geometry should be copied from the input features into the output.
strategy –
Specifies the strategy to use when processing features. Detailed performance notes are available in Performance - Processing strategies. Must be set to one of:
"feature-sequential"
(the default): iterate over the features invec
, read the corresponding pixels fromrast
/weights
, and compute the summary operations. This offers predictable memory consumption but may be inefficient if the order of features invec
causes the same, relatively large raster blocks to be read and decompressed many times."raster-sequential"
: iterate over chunks of pixels inrast
, identify the intersecting features fromvec
, and compute statistics. This performs better thanstrategy="feature-sequential"
in some cases, but comes at a cost of higher memory usage.
max_cells_in_memory – Indicates the maximum number of raster cells that should be loaded into memory at a given time.
grid_compat_tol – require value and weight grids to align within
grid_compat_tol
times the smaller of the two grid resolutionsoutput –
An
OutputWriter
or one of the following strings:”geojson” (the default): return a list of GeoJSON-like features
- ”pandas”: return a
pandas.DataFrame
orgeopandas.GeoDataFrame
, depending on the value of
include_geom
- ”pandas”: return a
- ”gdal”: write results to disk using GDAL/OGR as they are written. This
option (with
strategy="feature-sequential"
) avoids the need to maintain results for all features in memory at a single time, which may be significant for operations with large result sizes such ascell_id
,values
, etc.
output_options – an optional dictionary of options passed to the
writer.JSONWriter
,writer.PandasWriter
, orwriter.GDALWriter
.progress – if True, a progress bar will be displayed. Alternatively, a function may be provided that will be called with the completion fraction and a status message.
exactextract.feature module
- class exactextract.feature.FeatureSource[source]
Bases:
FeatureSource
Source from which polygon features can be read.
Several implementations are included in exactextract:
- class exactextract.feature.GDALFeatureSource(src)[source]
Bases:
FeatureSource
A FeatureSource using the GDAL/OGR Python interface to provide features.
- class exactextract.feature.GeoPandasFeatureSource(src)[source]
Bases:
FeatureSource
A FeatureSource using GeoPandas GeoDataFrame to provide features.
- class exactextract.feature.JSONFeatureSource(src, *, srs_wkt=None)[source]
Bases:
FeatureSource
A FeatureSource providing GeoJSON-like features.
exactextract.operation module
- class exactextract.operation.Operation(stat_name: str, field_name: str, raster: RasterSource, weights: RasterSource | None = None, options: Mapping | None = None)[source]
Bases:
Operation
Summarize of pixel values using a built-in function
Defines a summary operation to be performed on pixel values intersecting a geometry. May return a scalar (e.g.,
weighted_mean
), or a vector (e.g.,coverage
).- __init__(stat_name: str, field_name: str, raster: RasterSource, weights: RasterSource | None = None, options: Mapping | None = None)[source]
- Parameters:
stat_name – Name of the stat. Refer to docs for options.
field_name – Name of the result field that is assigned by this Operation.
raster – Raster to compute over.
weights – Weight raster to use. Defaults to None.
options – Arguments used to control the behavior of an Operation, e.g.
options={"q": 0.667}
withstat_name = "quantile"
- class exactextract.operation.PythonOperation(function: Callable, field_name: str, raster: RasterSource, weights: RasterSource | None)[source]
Bases:
PythonOperation
Summarize of pixel values using a Python function
Defines a summary operation to be performed on pixel values intersecting a geometry.
- __init__(function: Callable, field_name: str, raster: RasterSource, weights: RasterSource | None)[source]
- Parameters:
function – Function accepting either two arguments (if weights is None), or three arguments. The function will be called with arrays of equal length containing: - pixel values from raster (masked array) - cell coverage fractions - pixel values from weights (masked array)
field_name – Name of the result field that is assigned by this Operation.
raster – Raster to compute over.
weights – Weight raster to use. Defaults to None.
exactextract.processor module
- class exactextract.processor.FeatureSequentialProcessor(ds: FeatureSource, writer: Writer, op_list: List[Operation], include_cols: List[Operation] | None = None)[source]
Bases:
FeatureSequentialProcessor
Binding class around exactextract FeatureSequentialProcessor
- __init__(ds: FeatureSource, writer: Writer, op_list: List[Operation], include_cols: List[Operation] | None = None)[source]
- Parameters:
ds (FeatureSource) – Dataset to use
writer (Writer) – Writer to use
op_list – List of Operations to perform
include_cols – List of columns to copy from input features
- class exactextract.processor.RasterSequentialProcessor(ds: FeatureSource, writer: Writer, op_list: List[Operation], include_cols: List[Operation] | None = None)[source]
Bases:
RasterSequentialProcessor
Binding class around exactextract RasterSequentialProcessor
- __init__(ds: FeatureSource, writer: Writer, op_list: List[Operation], include_cols: List[Operation] | None = None)[source]
- Parameters:
ds (FeatureSource) – Dataset to use
writer (Writer) – Writer to use
op_list (List[Operation]) – List of operations
include_cols – List of columns to copy from input features
exactextract.raster module
- class exactextract.raster.GDALRasterSource(ds, band_idx: int = 1, *, name=None)[source]
Bases:
RasterSource
RasterSource backed by GDAL
- __init__(ds, band_idx: int = 1, *, name=None)[source]
- Parameters:
ds – A
gdal.Dataset
or path from which one can be openedband_idx – 1-based numerical index of band to read
name – source name, to be used in generating field names for results
- class exactextract.raster.NumPyRasterSource(mat, xmin=None, ymin=None, xmax=None, ymax=None, *, nodata=None, name=None, srs_wkt=None)[source]
Bases:
RasterSource
RasterSource backed by a NumPy array
- __init__(mat, xmin=None, ymin=None, xmax=None, ymax=None, *, nodata=None, name=None, srs_wkt=None)[source]
Create a RasterSource that references a NumPy array.
If spatial extent arguments are not provided, the extent will be assumed to be from (0,0) to (nx,ny).
- Parameters:
mat – a two-dimensional NumPy array. Masked arrays are supported.
xmin – x coordinate of left edge
ymin – y coordinate of bottom edge
xmax – x coordinate of right edge
ymax – y coordinate of top edge
nodata – Optional value used to indicate missing data.
name – source name, to be used in generating field names for results
srs_wkt – WKT string indicating the spatial reference system.
- class exactextract.raster.RasterSource[source]
Bases:
RasterSource
Source from which raster data can be read.
A RasterSource provides the ability to read subsets of a single band of raster data. Several implementations are included in exactextract:
- class exactextract.raster.RasterioRasterSource(ds, band_idx=1, *, name=None)[source]
Bases:
RasterSource
RasterSource backed by rasterio
- __init__(ds, band_idx=1, *, name=None)[source]
- Parameters:
ds – A
rasterio.DatasetReader
or path from which one can be openedband_idx – 1-based numerical index of band to read
name – source name, to be used in generating field names for results
- class exactextract.raster.XArrayRasterSource(ds, band_idx=1, *, name=None)[source]
Bases:
RasterSource
RasterSource backed by xarray
The rio-xarray extension is used to retrieve metadata such as the array extent, resolution, and spatial reference system.
- __init__(ds, band_idx=1, *, name=None)[source]
- Parameters:
ds – An xarray
DataArray
or a path from which one can be read.band_idx – 1-based numerical index of band to read
name – source name, to be used in generating field names for results
exactextract.writer module
- class exactextract.writer.GDALWriter(dataset=None, *, filename=None, driver=None, layer_name='', srs_wkt=None)[source]
Bases:
Writer
Writes results using GDAL/OGR
- __init__(dataset=None, *, filename=None, driver=None, layer_name='', srs_wkt=None)[source]
- Parameters:
dataset – a
gdal.Dataset
orogr.DataSource
to which results should be created in a new layerfilename – file to write results to, if
dataset
isNone
driver – driver to use when creating
filename
layer_name – name of new layer to create in output dataset
srs_wkt – spatial reference system to assign to output dataset. No coordinate transformation will be performed.
- class exactextract.writer.JSONWriter(*, array_type: str = 'numpy', map_fields: Mapping[str, Tuple[str]] | None = None)[source]
Bases:
Writer
Creates GeoJSON-like features
- __init__(*, array_type: str = 'numpy', map_fields: Mapping[str, Tuple[str]] | None = None)[source]
- Parameters:
array_type – type that should be used to represent array outputs. either “numpy” (default), “list”, or “set”
map_fields – an optional dictionary of fields to be created by interpreting one field as keys and another as values, in the format
{ dst_field : (src_keys, src_vals) }
. for example, the fields “values” and “frac” would be combined into a field called “frac_map” usingmap_fields = {"frac_map": ("values", "frac")}
.
- class exactextract.writer.PandasWriter(*, srs_wkt=None)[source]
Bases:
Writer
Creates a (Geo)Pandas DataFrame
Module contents
Python bindings for exactextract