imod.prepare.zonal_aggregate_raster#

imod.prepare.zonal_aggregate_raster(path: Path | str, column: str, raster: DataArray, resolution: float, method: str | Callable, chunksize: int = 10000) DataFrame[source]#

Compute a zonal aggregate of raster data for polygon geometries, e.g. a mean, mode, or percentile.

Parameters:
  • path (str or pathlib.Path) – path to OGR supported vector file (e.g. a shapefile). Defines zones of aggregation.

  • column (str) – column name of path, integer IDs defining zones.

  • raster (xarray.DataArray) – Raster data from which to sample and aggregate data

  • resolution (float) – cellsize at which the rasterization of polygons and sampling occurs

  • method (Union[str, Callable]) – Aggregation method. Anything that is acceptable by a pandas groupby aggregate: https://pandas.pydata.org/docs/reference/api/pandas.core.groupby.DataFrameGroupBy.aggregate.html

  • chunksize (int, optional) – The size of the chunksize. Used for both x and y dimension.

Returns:

zonal_aggregates

Return type:

pandas.DataFrame

Examples

To compute the mean surface level at polygons of water bodies:

>>> import imod
>>> surface_level = imod.rasterio.open("surface_level.tif")
>>> df = imod.prepare.spatial.zonal_aggregate_raster(
>>>    path="water-bodies.shp",
>>>    column="id",
>>>    raster=surface_level,
>>>    resolution=1.0,
>>>    method="mean",
>>> )

For some functions, like the mode, a function should be passed instead:

>>> import pandas as pd
>>> df = imod.prepare.spatial.zonal_aggregate_raster(
>>>    path="water-bodies.shp",
>>>    column="id",
>>>    raster=surface_level,
>>>    resolution=1.0,
>>>    method=pd.Series.mode,
>>> )