Products QGIS Scripts Zonal Statistics Calculator
Zonal Statistics Calculator

QGIS Script · Python

Zonal Statistics Calculator

Samples band 1 pixel values from a checked raster within each polygon zone and writes the chosen statistic — mean, median, min, or max — as a new attribute column. The script works with whatever raster you provide: check an NDVI raster and you get NDVI per zone; check a CHM and you get canopy height per zone. CRS mismatches are handled automatically.

$20 USD One-time purchase · Instant download
Buy on Gumroad

What you get

zonal_statistics.py
Free updates via Gumroad
Also in the Vegetation & Field Analysis Pack — 6 scripts for $70 · save $50 vs buying separately

What it does

Mean, median, min, or max

Choose which aggregation to compute. The result is written as a new {data_type}_{aggregation} attribute on each polygon (e.g. "ndvi_mean").

Samples band 1 of any raster

Always reads band 1 of the checked raster. Works with any single-band raster (NDVI, CHM, elevation, NDRE, NDWI). For multi-band rasters, only band 1 is used — generate your index as a single-band raster first.

Auto CRS alignment

If the vector and raster are in different coordinate systems, the vector is automatically reprojected to the raster CRS before sampling.

Named output column

Set data_type to label the column — "ndvi", "height", "elevation" — so you know what the statistic represents without guessing.

How to use it

  1. 1

    Load your polygon zone layer and check (tick) it first in the Layers panel.

  2. 2

    Load your raster (e.g. an NDVI raster) and check (tick) it second.

  3. 3

    Open Plugins → Python Console → Show Editor.

  4. 4

    Paste the script. At the bottom set data_type (e.g. "ndvi") and aggregation ("mean", "median", "min", or "max").

  5. 5

    Click Run. The output GeoJSON is saved and added to the project with the new attribute column.

  6. 6

    To visualise the result on the map, run the Layer Colour Classifier on the output layer — it colour-codes each zone by the statistic value.

Requirements

  • QGIS 3.28 or newer
  • QGIS processing framework — bundled
  • First checked layer: polygon vector (zones)
  • Second checked layer: single-band raster

Also in the Vegetation & Field Analysis Pack

Get all 6 vegetation scripts for $70 — save $50 vs buying individually.

View bundle