QgsZonalStatistics class

A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and appends the results as attributes.

Public types

enum Statistic { Count = 1, Sum = 2, Mean = 4, Median = 8, StDev = 16, Min = 32, Max = 64, Range = 128, Minority = 256, Majority = 512, Variety = 1024, Variance = 2048, All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | Variance }
Enumeration of flags that specify statistics to be calculated.

Constructors, destructors, conversion operators

QgsZonalStatistics(QgsVectorLayer* polygonLayer, QgsRasterLayer* rasterLayer, const QString& attributePrefix = QString(), int rasterBand = 1, QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics(QgsZonalStatistics::Count|QgsZonalStatistics::Sum|QgsZonalStatistics::Mean))
Convenience constructor for QgsZonalStatistics, using an input raster layer.
QgsZonalStatistics(QgsVectorLayer* polygonLayer, QgsRasterInterface* rasterInterface, const QgsCoordinateReferenceSystem& rasterCrs, double rasterUnitsPerPixelX, double rasterUnitsPerPixelY, const QString& attributePrefix = QString(), int rasterBand = 1, QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics(QgsZonalStatistics::Count|QgsZonalStatistics::Sum|QgsZonalStatistics::Mean))
Constructor for QgsZonalStatistics, using a QgsRasterInterface.

Public functions

auto calculateStatistics(QgsFeedback* feedback) -> int
Starts the calculation.

Enum documentation

enum QgsZonalStatistics::Statistic

Enumeration of flags that specify statistics to be calculated.

Enumerators
Count

Pixel count.

Sum

Sum of pixel values.

Mean

Mean of pixel values.

Median

Median of pixel values.

StDev

Standard deviation of pixel values.

Min

Min of pixel values.

Max

Max of pixel values.

Range

Range of pixel values (max - min)

Minority

Minority of pixel values.

Majority

Majority of pixel values.

Variety

Variety (count of distinct) pixel values.

Variance

Variance of pixel values.

All

Function documentation

QgsZonalStatistics::QgsZonalStatistics(QgsVectorLayer* polygonLayer, QgsRasterLayer* rasterLayer, const QString& attributePrefix = QString(), int rasterBand = 1, QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics(QgsZonalStatistics::Count|QgsZonalStatistics::Sum|QgsZonalStatistics::Mean))

Convenience constructor for QgsZonalStatistics, using an input raster layer.

The raster layer must exist for the lifetime of the zonal statistics calculation.

QgsZonalStatistics::QgsZonalStatistics(QgsVectorLayer* polygonLayer, QgsRasterInterface* rasterInterface, const QgsCoordinateReferenceSystem& rasterCrs, double rasterUnitsPerPixelX, double rasterUnitsPerPixelY, const QString& attributePrefix = QString(), int rasterBand = 1, QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics(QgsZonalStatistics::Count|QgsZonalStatistics::Sum|QgsZonalStatistics::Mean))

Constructor for QgsZonalStatistics, using a QgsRasterInterface.

The polygonLayer gives the vector layer containing the (multi)polygon features corresponding to the different zones. This layer will be modified, adding extra attributes for each of the zonal statistics calculated.

Pixel values for each zone are taken from the raster rasterInterface. The constructor must also be given various properties relating to the input raster, such as the raster CRS (rasterCrs), and the size (X and Y) in map units for each raster pixel. The source raster band is specified via rasterBand, where a value of 1 corresponds to the first band.

If the CRS of the polygonLayer and rasterCrs differ, the calculation will automatically reproject the zones to ensure valid results are calculated.

The attributePrefix argument specifies an optional prefix to use when creating the new fields for each calculated statistic.

Finally, the calculated statistics can be set via the stats argument. A new field will be added to polygonLayer for each statistic calculated.

int QgsZonalStatistics::calculateStatistics(QgsFeedback* feedback)

Starts the calculation.

Returns 0 in case of success