compute_zonal_stats

eolab.georastertools.processing.stats.compute_zonal_stats(geoms: GeoDataFrame, image: str, bands: List[int] = [1], stats: List[str] = ['min', 'max', 'mean', 'std'], categorical: bool = False) List[List[Dict[str, float]]][source]

Compute zonal statistics for an input raster image over specified geometries.

This function calculates statistical summaries (e.g., min, max, mean, standard deviation) for each feature in the provided geometries (GeoDataFrame) using the specified raster image. If the raster is categorical, the function can compute counts of unique values.

Parameters:
  • geoms (GeoDataFrame) – A GeoDataFrame containing geometries (e.g., polygons) for which statistics will be computed.

  • image (str) – Path to the raster image file to process.

  • bands (List[int], optional) – A list of raster band indices to process. Defaults to [1] (the first band).

  • stats (List[str], optional) – A list of statistical metrics to compute. Possible values include: - “min”: Minimum value within the geometry. - “max”: Maximum value within the geometry. - “mean”: Mean (average) value within the geometry. - “std”: Standard deviation within the geometry. - Other metrics may be supported based on the implementation of _compute_stats. Defaults to [“min”, “max”, “mean”, “std”].

  • categorical (bool, optional) – If True, treats the raster as categorical, computing counts of unique values within each geometry. Defaults to False.

Returns:

A nested list where: - The outer list corresponds to each geometry in the GeoDataFrame. - The inner list corresponds to each band processed. - Each dictionary contains the computed statistics, with stat names as keys and their values as values.

Return type:

List[List[Dict[str, float]]]

Example

``` import geopandas as gpd from georastertools import compute_zonal_stats

geoms = gpd.read_file(“polygons.shp”) stats = compute_zonal_stats(geoms, “input_image.tif”, bands=[1, 2], stats=[“mean”, “std”]) print(stats) ```