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

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

  • 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:

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



Return type:



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

>>> import imod
>>> surface_level ="surface_level.tif")
>>> df = imod.prepare.spatial.zonal_aggregate_raster(
>>>    "water-bodies.shp", "id", 1.0, surface_level, "mean"
>>> )

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

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