QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
QgsZonalStatistics Class Reference

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

#include <qgszonalstatistics.h>

Public Types

enum  Result {
  Success = 0 , LayerTypeWrong = 1 , LayerInvalid , RasterInvalid ,
  RasterBandInvalid , FailedToCreateField = 8 , Canceled = 9
}
 Error codes for calculation. More...
 
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. More...
 

Public Member Functions

 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. More...
 
 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. More...
 
QgsZonalStatistics::Result calculateStatistics (QgsFeedback *feedback)
 Runs the calculation. More...
 

Static Public Member Functions

static QMap< QgsZonalStatistics::Statistic, QVariant > calculateStatistics (QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, QgsZonalStatistics::Statistics statistics)
 Calculates the specified statistics for the pixels of rasterBand in rasterInterface (a raster layer dataProvider() ) within polygon geometry. More...
 
static QString displayName (QgsZonalStatistics::Statistic statistic)
 Returns the friendly display name for a statistic. More...
 
static QString shortName (QgsZonalStatistics::Statistic statistic)
 Returns a short, friendly display name for a statistic, suitable for use in a field name. More...
 

Detailed Description

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

Definition at line 46 of file qgszonalstatistics.h.

Member Enumeration Documentation

◆ Result

Error codes for calculation.

Enumerator
Success 

Success.

LayerTypeWrong 

Layer is not a polygon layer.

LayerInvalid 

Layer is invalid.

RasterInvalid 

Raster layer is invalid.

RasterBandInvalid 

The raster band does not exist on the raster layer.

FailedToCreateField 

Output fields could not be created.

Canceled 

Algorithm was canceled.

Definition at line 70 of file qgszonalstatistics.h.

◆ Statistic

Enumeration of flags that specify statistics to be calculated.

Enumerator
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 

Definition at line 51 of file qgszonalstatistics.h.

Constructor & Destructor Documentation

◆ QgsZonalStatistics() [1/2]

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.

Warning
Constructing QgsZonalStatistics using this method is not thread safe, and the constructor which accepts a QgsRasterInterface should be used instead.

Definition at line 33 of file qgszonalstatistics.cpp.

◆ QgsZonalStatistics() [2/2]

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.

Warning
The raster interface must exist for the lifetime of the zonal statistics calculation. For thread safe use, always use a cloned raster interface.
Since
QGIS 3.2

Definition at line 45 of file qgszonalstatistics.cpp.

Member Function Documentation

◆ calculateStatistics() [1/2]

QgsZonalStatistics::Result QgsZonalStatistics::calculateStatistics ( QgsFeedback feedback)

Runs the calculation.

Definition at line 58 of file qgszonalstatistics.cpp.

◆ calculateStatistics() [2/2]

QMap< QgsZonalStatistics::Statistic, QVariant > QgsZonalStatistics::calculateStatistics ( QgsRasterInterface rasterInterface,
const QgsGeometry geometry,
double  cellSizeX,
double  cellSizeY,
int  rasterBand,
QgsZonalStatistics::Statistics  statistics 
)
static

Calculates the specified statistics for the pixels of rasterBand in rasterInterface (a raster layer dataProvider() ) within polygon geometry.

Returns a map of statistic to result value.

Since
QGIS 3.16

Definition at line 303 of file qgszonalstatistics.cpp.

◆ displayName()

QString QgsZonalStatistics::displayName ( QgsZonalStatistics::Statistic  statistic)
static

Returns the friendly display name for a statistic.

See also
shortName()
Since
QGIS 3.12

Definition at line 222 of file qgszonalstatistics.cpp.

◆ shortName()

QString QgsZonalStatistics::shortName ( QgsZonalStatistics::Statistic  statistic)
static

Returns a short, friendly display name for a statistic, suitable for use in a field name.

See also
displayName()
Since
QGIS 3.12

Definition at line 256 of file qgszonalstatistics.cpp.


The documentation for this class was generated from the following files: