QGIS Script · Python
Zonal Statistics Calculator
Aggregates raster pixel values (mean, median, min, or max) within each polygon zone and writes the result as a new attribute column on each feature. Works with any single-band raster and handles CRS mismatches automatically.
What you get
zonal_statistics.pyWhat 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").
Any single-band raster
Works with NDVI, CHM, elevation, or any single-band raster — not limited to specific layer types or naming conventions beyond what you set.
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
Load your polygon vector layer and check (tick) it first in the Layers panel.
- 2
Load your raster and check (tick) it second in the Layers panel.
- 3
Open Plugins → Python Console → Show Editor.
- 4
Paste the script. At the bottom set
data_type(e.g."ndvi") andaggregation("mean","median","min", or"max"). - 5
Click Run.
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 $40 — save $20 vs buying individually.