QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
32 class QgsGeometry;
33 class QgsVectorLayer;
34 class QgsRasterLayer;
35 class QgsRasterInterface;
37 class QgsRectangle;
38 class QgsField;
39 class QgsFeatureSink;
40 class QgsFeatureSource;
41 
46 class ANALYSIS_EXPORT QgsZonalStatistics
47 {
48  public:
49 
51  enum Statistic
52  {
53  Count = 1,
54  Sum = 2,
55  Mean = 4,
56  Median = 8,
57  StDev = 16,
58  Min = 32,
59  Max = 64,
60  Range = 128,
61  Minority = 256,
62  Majority = 512,
63  Variety = 1024,
64  Variance = 2048,
65  All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | Variance
66  };
67  Q_DECLARE_FLAGS( Statistics, Statistic )
68 
69 
70  enum Result
71  {
72  Success = 0,
73  LayerTypeWrong = 1,
77  FailedToCreateField = 8,
78  Canceled = 9
79  };
80 
89  QgsZonalStatistics( QgsVectorLayer *polygonLayer,
90  QgsRasterLayer *rasterLayer,
91  const QString &attributePrefix = QString(),
92  int rasterBand = 1,
93  QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics( QgsZonalStatistics::Count | QgsZonalStatistics::Sum | QgsZonalStatistics::Mean ) );
94 
121  QgsZonalStatistics( QgsVectorLayer *polygonLayer,
122  QgsRasterInterface *rasterInterface,
123  const QgsCoordinateReferenceSystem &rasterCrs,
124  double rasterUnitsPerPixelX,
125  double rasterUnitsPerPixelY,
126  const QString &attributePrefix = QString(),
127  int rasterBand = 1,
128  QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics( QgsZonalStatistics::Count | QgsZonalStatistics::Sum | QgsZonalStatistics::Mean ) );
129 
130 
134  QgsZonalStatistics::Result calculateStatistics( QgsFeedback *feedback );
135 
141  static QString displayName( QgsZonalStatistics::Statistic statistic );
142 
148  static QString shortName( QgsZonalStatistics::Statistic statistic );
149 
158 #ifndef SIP_RUN
159  static QMap<QgsZonalStatistics::Statistic, QVariant> calculateStatistics( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, QgsZonalStatistics::Statistics statistics );
160 #endif
161 
163  // Required to fix https://github.com/qgis/QGIS/issues/43245 (SIP is failing to convert the enum to values)
164 
173  static QMap<int, QVariant> calculateStatisticsInt( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, QgsZonalStatistics::Statistics statistics ) SIP_PYNAME( calculateStatistics );
175 
176  private:
177  QgsZonalStatistics() = default;
178 
179  class FeatureStats
180  {
181  public:
182  FeatureStats( bool storeValues = false, bool storeValueCounts = false )
183  : mStoreValues( storeValues )
184  , mStoreValueCounts( storeValueCounts )
185  {
186  }
187 
188  void reset()
189  {
190  sum = 0;
191  count = 0;
192  max = std::numeric_limits<double>::lowest();
193  min = std::numeric_limits<double>::max();
194  valueCount.clear();
195  values.clear();
196  }
197 
198  void addValue( double value, double weight = 1.0 )
199  {
200  if ( weight < 1.0 )
201  {
202  sum += value * weight;
203  count += weight;
204  }
205  else
206  {
207  sum += value;
208  ++count;
209  }
210  min = std::min( min, value );
211  max = std::max( max, value );
212  if ( mStoreValueCounts )
213  valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
214  if ( mStoreValues )
215  values.append( value );
216  }
217  double sum = 0.0;
218  double count = 0.0;
219  double max = std::numeric_limits<double>::lowest();
220  double min = std::numeric_limits<double>::max();
221  QMap< double, int > valueCount;
222  QList< double > values;
223 
224  private:
225  bool mStoreValues = false;
226  bool mStoreValueCounts = false;
227  };
228 
229  QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
230 
231  QgsRasterInterface *mRasterInterface = nullptr;
232  QgsCoordinateReferenceSystem mRasterCrs;
233 
234  double mCellSizeX = 0;
235  double mCellSizeY = 0;
236 
238  int mRasterBand = 0;
239  QgsVectorLayer *mPolygonLayer = nullptr;
240  QString mAttributePrefix;
241  Statistics mStatistics = QgsZonalStatistics::All;
242 };
243 
244 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsZonalStatistics::Statistics )
245 
246 // clazy:excludeall=qstring-allocations
247 
248 #endif // QGSZONALSTATISTICS_H
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
qgsfields.h
QgsZonalStatistics::RasterInvalid
@ RasterInvalid
Raster layer is invalid.
Definition: qgszonalstatistics.h:75
QgsZonalStatistics::All
@ All
Definition: qgszonalstatistics.h:65
QgsZonalStatistics
A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and ...
Definition: qgszonalstatistics.h:46
QgsZonalStatistics::Mean
@ Mean
Mean of pixel values.
Definition: qgszonalstatistics.h:55
QgsFeatureSource
An interface for objects which provide features via a getFeatures method.
Definition: qgsfeaturesource.h:37
QgsZonalStatistics::Count
@ Count
Pixel count.
Definition: qgszonalstatistics.h:53
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsZonalStatistics::LayerInvalid
@ LayerInvalid
Layer is invalid.
Definition: qgszonalstatistics.h:74
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
Q_DECLARE_OPERATORS_FOR_FLAGS
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsZonalStatistics::Sum
@ Sum
Sum of pixel values.
Definition: qgszonalstatistics.h:54
QgsZonalStatistics::Result
Result
Error codes for calculation.
Definition: qgszonalstatistics.h:70
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:76
QgsZonalStatistics::RasterBandInvalid
@ RasterBandInvalid
The raster band does not exist on the raster layer.
Definition: qgszonalstatistics.h:76
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsRasterInterface
Base class for processing filters like renderers, reprojector, resampler etc.
Definition: qgsrasterinterface.h:135
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
qgsfeedback.h
QgsZonalStatistics::Statistic
Statistic
Enumeration of flags that specify statistics to be calculated.
Definition: qgszonalstatistics.h:51
qgscoordinatereferencesystem.h
QgsRasterDataProvider
Base class for raster data providers.
Definition: qgsrasterdataprovider.h:88
QgsFeatureSink
An interface for objects which accept features via addFeature(s) methods.
Definition: qgsfeaturesink.h:33
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50