Scroll to navigation

GDAL-RASTER-ZONAL-STATS(1) GDAL GDAL-RASTER-ZONAL-STATS(1)

NAME

gdal-raster-zonal-stats - Compute raster zonal statistics.

Added in version 3.12.

SYNOPSIS

Usage: gdal raster zonal-stats [OPTIONS] <INPUT> <OUTPUT>
Calculate raster zonal statistics
Positional arguments:

-i, --input <INPUT> Input raster datasets [required]
-o, --output <OUTPUT> Output vector dataset [required] Common Options:
-h, --help Display help message and exit
--json-usage Display usage as JSON document and exit
--config <KEY>=<VALUE> Configuration option [may be repeated]
-q, --quiet Quiet mode (no progress bar) Options:
-f, --of, --format, --output-format <OUTPUT-FORMAT> Output format ("GDALG" allowed)
--co, --creation-option <KEY>=<VALUE> Creation option [may be repeated]
--lco, --layer-creation-option <KEY>=<VALUE> Layer creation option [may be repeated]
--overwrite Whether overwriting existing output is allowed
--update Whether to open existing dataset in update mode
--overwrite-layer Whether overwriting existing output is allowed
--append Whether appending to existing layer is allowed
Mutually exclusive with --upsert
--output-layer <OUTPUT-LAYER> Output layer name
--skip-errors Skip errors when writing features
-b, --band <BAND> Input band(s) (1-based index) [may be repeated]
--zones <ZONES> Dataset containing zone definitions [required]
--zones-band <ZONES-BAND> Band from which zones should be read
Mutually exclusive with --zones-layer
--zones-layer <ZONES-LAYER> Layer from which zones should be read
Mutually exclusive with --zones-band
--weights <WEIGHTS> Weighting raster dataset
--weights-band <WEIGHTS-BAND> Band from which weights should be read (default: 1)
--pixels <PIXELS> Method to determine which pixels are included in stat calculation.. PIXELS=default|fractional|all-touched
--stat <STAT> Statistic(s) to compute for each zone. STAT=center_x|center_y|count|coverage|frac|max|max_center_x|max_center_y|mean|median|min|minority|min_center_x|min_center_y|mode|stdev|sum|unique|values|variance|variety|weighted_mean|weighted_stdev|weighted_sum|weighted_variance|weights [may be repeated] [required]
--include-field <INCLUDE-FIELD> Fields from polygon zones to include in output [may be repeated]
--strategy <STRATEGY> For polygon zones, whether to iterate over input features or raster chunks. STRATEGY=feature|raster (default: feature)
--chunk-size <CHUNK-SIZE> Maximum size of raster chunks read into memory (default: 5%) Advanced Options:
--if, --input-format <INPUT-FORMAT> Input formats [may be repeated]
--oo, --open-option <KEY>=<VALUE> Open options [may be repeated]
--output-oo, --output-open-option <KEY>=<VALUE> Output open options [may be repeated]
--upsert Upsert features (implies 'append')
Mutually exclusive with --append


DESCRIPTION

gdal raster zonal-stats computes raster zonal statistics -- a summary of pixel values within zones specified either polygon features or a categorical raster.

[image] Mean elevation calculated for each polygon from an elevation raster..UNINDENT

A weighting raster may be provided in addition to the source raster, allowing calculation of statistics such as population-weighted mean weather conditions.

All inputs should be provided in the same spatial reference system. If input rasters use different resolutions they will be resampled to a consistent resolution using average resampling. However, no reprojection is performed.

Pixel inclusion and weighting

When zones are defined using polygons, three methods are available for determining which pixels to include in the calculation. By default, the geometries are rasterized using default parameters, and pixels included in the rasterization are used in the calculation with equal weighting. By using --pixels all-touched, all pixels touched by the polygon are used in the calculation, again with equal weighting. If using --pixels fractional, the fraction of each pixel that is covered by the polygon is computed, and pixels are weighted according to this fraction. This option requires GDAL be built against version 3.14 or later of the GEOS library.

Supported stats

Name Description
center_x Array with cell center x-coordinate for each cell that intersects the polygon. Each cell center may or may not be inside the polygon.
center_y Array with cell center y-coordinate for each cell that intersects the polygon. Each cell center may or may not be inside the polygon.
count Sum of all cell coverage fractions.
coverage Array with coverage fraction of each cell that intersects the polygon
frac Fraction of covered cells that are occupied by each distinct raster value, as provided by unique.
majority The raster value occupying the greatest number of cells, taking into account cell coverage fractions but not weighting raster values.
max Maximum value of cells that intersect the polygon, not taking coverage fractions or weighting raster values into account.
max_center_x Cell center x-coordinate for the cell containing the maximum value intersected by the polygon. The center of this cell may or may not be inside the polygon.
max_center_y Cell center y-coordinate for the cell containing the maximum value intersected by the polygon. The center of this cell may or may not be inside the polygon.
mean Mean value of cells that intersect the polygon, weighted by the percent of each cell that is covered.
min Minimum value of cells that intersect the polygon, not taking coverage fractions or weighting raster values into account.
min_center_x Cell center x-coordinate for the cell containing the minimum value intersected by the polygon. The center of this cell may or may not be inside the polygon.
min_center_y Cell center y-coordinate for the cell containing the minimum value intersected by the polygon. The center of this cell may or may not be inside the polygon.
minority The raster value occupying the least number of cells, taking into account cell coverage fractions but not weighting raster values.
stdev Population standard deviation of cell values that intersect the polygon, taking into account coverage fraction.
sum Sum of values of raster cells that intersect the polygon, with each raster value weighted by its coverage fraction.
unique Array of unique raster values for cells that intersect the polygon
values Array of raster values for each cell that intersects the polygon
variance Population variance of cell values that intersect the polygon, taking into account coverage fraction.
variety The number of distinct raster values in cells wholly or partially covered by the polygon.
weighted_frac Fraction of covered cells that are occupied by each distinct raster value, weighted by the value of a second weighting raster. Order corresponds to values returned by unique.
weighted_mean Mean value of cells that intersect the polygon, weighted by the product over the coverage fraction and the weighting raster.
weighted_stdev Weighted version of stdev.
weighted_variance Weighted version of variance
weights Array of weight values for each cell that intersects the polygon

This algorithm can be part of a gdal pipeline.

The following options are available:

Specifies one or more of the Supported stats to compute for each zone.

The path to a dataset that defines the zones. If the dataset has more than 1 raster band or feature layer, --zones-band or --zones-layer must be specified.

Specifies the raster band from which zones can be read.

Specifies the feature layer from which zones can be read.

Optional path to a dataset to use for weighting.

Specifies the raster band from which weights can be read.

Method to determine which pixels should be included in the calculation: default, all-touched, or fractional.

Defines the maximum size of raster chunks to read. May be expressed as either an amount of memory (500 MB, 2 GB, etc.) or as a percentage of available RAM (e.g. 10%).

Specifies the the processing strategy (raster or feature), when vector zones are used. In the default strategy (--strategy feature), GDAL will iterate over the features in the zone dataset, read the corresponding pixels from the raster, and write the statistics for that feature. This avoids the need to read the entire feature dataset into memory at once, but may cause the same pixels to be read multiple times if the polygon features are large or not ordered spatially. If --strategy raster is used, GDAL will iterate over chunks of the raster dataset, find corresponding polygon zones, and update the statistics for those features. (The size of the raster chunks can be controlled using :option:--chunk-size.) This ensures that raster pixels are only read once, but may cause the same features to be processed multiple times.

Specifies one or more fields from the zones to be copied to the output. Only available when vector zones are used.

EXAMPLES

Example 1: Summarize mean elevation within 200m of points of interest

gdal pipeline read dem.tif ! \

zonal-stats \
--zones [ read points.geojson ! buffer 200 ] \
--stat mean ! \
write \
--output-format CSV \
--output /vsistdout/


Example 2: Create a layer with the highest points in each watershed

gdal pipeline read dem.tif !

zonal-stats \
--zones watersheds.shp \
--stat max_center_x \
--stat max_center_y !
make-point \
--x max_center_x \
--y max_center_y \
--dst-crs EPSG:4326 !
write out.geojson


AUTHOR

Dan Baston <dbaston@gmail.com>

COPYRIGHT

1998-2025

November 7, 2025