QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgszonalstatistics.h
Go to the documentation of this file.
1/***************************************************************************
2 qgszonalstatistics.h - description
3 ----------------------------
4 begin : August 29th, 2009
5 copyright : (C) 2009 by Marco Hugentobler
6 email : marco at hugis dot net
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#ifndef QGSZONALSTATISTICS_H
19#define QGSZONALSTATISTICS_H
20
21#include <QString>
22#include <QMap>
23
24#include <limits>
25#include <cfloat>
26
27#include "qgis_analysis.h"
28#include "qgsfeedback.h"
30#include "qgsfields.h"
31
32class QgsGeometry;
33class QgsVectorLayer;
34class QgsRasterLayer;
37class QgsRectangle;
38class QgsField;
39class QgsFeatureSink;
41
46class ANALYSIS_EXPORT QgsZonalStatistics
47{
48 public:
49
59 QgsRasterLayer *rasterLayer,
60 const QString &attributePrefix = QString(),
61 int rasterBand = 1,
63
91 QgsRasterInterface *rasterInterface,
92 const QgsCoordinateReferenceSystem &rasterCrs,
93 double rasterUnitsPerPixelX,
94 double rasterUnitsPerPixelY,
95 const QString &attributePrefix = QString(),
96 int rasterBand = 1,
98
99
103 Qgis::ZonalStatisticResult calculateStatistics( QgsFeedback *feedback );
104
110 static QString displayName( Qgis::ZonalStatistic statistic );
111
117 static QString shortName( Qgis::ZonalStatistic statistic );
118
127#ifndef SIP_RUN
128 static QMap<Qgis::ZonalStatistic, QVariant> calculateStatistics( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics );
129#endif
130
132 // Required to fix https://github.com/qgis/QGIS/issues/43245 (SIP is failing to convert the enum to values)
133
142 static QMap<int, QVariant> calculateStatisticsInt( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics ) SIP_PYNAME( calculateStatistics );
144
145 private:
146 QgsZonalStatistics() = default;
147
148 class FeatureStats
149 {
150 public:
151 FeatureStats( bool storeValues = false, bool storeValueCounts = false )
152 : mStoreValues( storeValues )
153 , mStoreValueCounts( storeValueCounts )
154 {
155 }
156
157 void reset()
158 {
159 sum = 0;
160 count = 0;
161 max = std::numeric_limits<double>::lowest();
162 min = std::numeric_limits<double>::max();
163 valueCount.clear();
164 values.clear();
165 }
166
167 void addValue( double value, double weight = 1.0 )
168 {
169 if ( weight < 1.0 )
170 {
171 sum += value * weight;
172 count += weight;
173 }
174 else
175 {
176 sum += value;
177 ++count;
178 }
179 min = std::min( min, value );
180 max = std::max( max, value );
181 if ( mStoreValueCounts )
182 valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
183 if ( mStoreValues )
184 values.append( value );
185 }
186 double sum = 0.0;
187 double count = 0.0;
188 double max = std::numeric_limits<double>::lowest();
189 double min = std::numeric_limits<double>::max();
190 QMap< double, int > valueCount;
191 QList< double > values;
192
193 private:
194 bool mStoreValues = false;
195 bool mStoreValueCounts = false;
196 };
197
198 QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
199
200 QgsRasterInterface *mRasterInterface = nullptr;
202
203 double mCellSizeX = 0;
204 double mCellSizeY = 0;
205
207 int mRasterBand = 0;
208 QgsVectorLayer *mPolygonLayer = nullptr;
209 QString mAttributePrefix;
211};
212
213// clazy:excludeall=qstring-allocations
214
215#endif // QGSZONALSTATISTICS_H
ZonalStatistic
Statistics to be calculated during a zonal statistics operation.
Definition: qgis.h:4665
@ Default
Default statistics.
@ All
All statistics.
ZonalStatisticResult
Zonal statistics result codes.
Definition: qgis.h:4697
QFlags< ZonalStatistic > ZonalStatistics
Statistics to be calculated during a zonal statistics operation.
Definition: qgis.h:4688
This class represents a coordinate reference system (CRS).
An interface for objects which accept features via addFeature(s) methods.
An interface for objects which provide features via a getFeatures method.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:53
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:162
Base class for raster data providers.
Base class for processing filters like renderers, reprojector, resampler etc.
Represents a raster layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Represents a vector layer which manages a vector based data sets.
A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and ...
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81