Class: QgsZonalStatistics¶
- class qgis.analysis.QgsZonalStatistics(polygonLayer: QgsVectorLayer, rasterLayer: QgsRasterLayer, attributePrefix: str = '', rasterBand: int = 1, stats: Union[QgsZonalStatistics.Statistics, QgsZonalStatistics.Statistic] = QgsZonalStatistics.Statistics(QgsZonalStatistics.Count | QgsZonalStatistics.Sum | QgsZonalStatistics.Mean))¶
Bases:
sip.wrapper
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 aQgsRasterInterface
should be used instead.QgsZonalStatistics(polygonLayer:
QgsVectorLayer
, rasterInterface:QgsRasterInterface
, rasterCrs:QgsCoordinateReferenceSystem
, rasterUnitsPerPixelX: float, rasterUnitsPerPixelY: float, attributePrefix: str = ‘’, rasterBand: int = 1, stats: Union[QgsZonalStatistics.Statistics, QgsZonalStatistics.Statistic] = QgsZonalStatistics.Statistics(QgsZonalStatistics.Count|QgsZonalStatistics.Sum|QgsZonalStatistics.Mean)) Constructor forQgsZonalStatistics
, 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 viarasterBand
, where a value of 1 corresponds to the first band.If the CRS of the
polygonLayer
andrasterCrs
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 topolygonLayer
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.
New in version 3.2.
QgsZonalStatistics(
QgsZonalStatistics
)A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and appends the results as attributes.
Methods
Runs the calculation.
Returns the friendly display name for a
statistic
.Returns a short, friendly display name for a
statistic
, suitable for use in a field name.Attributes
- All = 4095¶
- Canceled = 9¶
- Count = 1¶
- FailedToCreateField = 8¶
- LayerInvalid = 2¶
- LayerTypeWrong = 1¶
- Majority = 512¶
- Max = 64¶
- Mean = 4¶
- Median = 8¶
- Min = 32¶
- Minority = 256¶
- Range = 128¶
- RasterBandInvalid = 4¶
- RasterInvalid = 3¶
- class Result¶
Bases:
int
- StDev = 16¶
- class Statistic¶
Bases:
int
- class Statistics¶
- class Statistics(Union[QgsZonalStatistics.Statistics, QgsZonalStatistics.Statistic]) None
- class Statistics(QgsZonalStatistics.Statistics) None
Bases:
sip.wrapper
- Success = 0¶
- Sum = 2¶
- Variance = 2048¶
- Variety = 1024¶
- calculateStatistics(self, feedback: QgsFeedback) QgsZonalStatistics.Result ¶
Runs the calculation.
calculateStatistics(rasterInterface:
QgsRasterInterface
, geometry:QgsGeometry
, cellSizeX: float, cellSizeY: float, rasterBand: int, statistics: Union[QgsZonalStatistics.Statistics, QgsZonalStatistics.Statistic]) -> Dict[int, Any] Calculates the specifiedstatistics
for the pixels ofrasterBand
inrasterInterface
(a raster layerdataProvider()
) within polygongeometry
.Returns a map of statistic to result value.
New in version 3.16.
- Parameters
feedback (QgsFeedback) –
- Return type
- displayName(statistic: QgsZonalStatistics.Statistic) str ¶
Returns the friendly display name for a
statistic
.See also
New in version 3.12.
- Parameters
statistic (QgsZonalStatistics.Statistic) –
- Return type
str
- shortName(statistic: QgsZonalStatistics.Statistic) str ¶
Returns a short, friendly display name for a
statistic
, suitable for use in a field name.See also
New in version 3.12.
- Parameters
statistic (QgsZonalStatistics.Statistic) –
- Return type
str