QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  static QMap<QgsZonalStatistics::Statistic, QVariant> calculateStatistics( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, QgsZonalStatistics::Statistics statistics );
159 
160  private:
161  QgsZonalStatistics() = default;
162 
163  class FeatureStats
164  {
165  public:
166  FeatureStats( bool storeValues = false, bool storeValueCounts = false )
167  : mStoreValues( storeValues )
168  , mStoreValueCounts( storeValueCounts )
169  {
170  }
171 
172  void reset()
173  {
174  sum = 0;
175  count = 0;
176  max = std::numeric_limits<double>::lowest();
177  min = std::numeric_limits<double>::max();
178  valueCount.clear();
179  values.clear();
180  }
181 
182  void addValue( double value, double weight = 1.0 )
183  {
184  if ( weight < 1.0 )
185  {
186  sum += value * weight;
187  count += weight;
188  }
189  else
190  {
191  sum += value;
192  ++count;
193  }
194  min = std::min( min, value );
195  max = std::max( max, value );
196  if ( mStoreValueCounts )
197  valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
198  if ( mStoreValues )
199  values.append( value );
200  }
201  double sum = 0.0;
202  double count = 0.0;
203  double max = std::numeric_limits<double>::lowest();
204  double min = std::numeric_limits<double>::max();
205  QMap< double, int > valueCount;
206  QList< double > values;
207 
208  private:
209  bool mStoreValues = false;
210  bool mStoreValueCounts = false;
211  };
212 
213  QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
214 
215  QgsRasterInterface *mRasterInterface = nullptr;
216  QgsCoordinateReferenceSystem mRasterCrs;
217 
218  double mCellSizeX = 0;
219  double mCellSizeY = 0;
220 
222  int mRasterBand = 0;
223  QgsVectorLayer *mPolygonLayer = nullptr;
224  QString mAttributePrefix;
225  Statistics mStatistics = QgsZonalStatistics::All;
226 };
227 
228 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsZonalStatistics::Statistics )
229 
230 // clazy:excludeall=qstring-allocations
231 
232 #endif // QGSZONALSTATISTICS_H
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:47
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:38
QgsZonalStatistics::Count
@ Count
Pixel count.
Definition: qgszonalstatistics.h:53
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
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:71
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:71
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:206
QgsRasterInterface
Base class for processing filters like renderers, reprojector, resampler etc.
Definition: qgsrasterinterface.h:117
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:387
qgsfeedback.h
QgsZonalStatistics::Statistic
Statistic
Enumeration of flags that specify statistics to be calculated.
Definition: qgszonalstatistics.h:52
qgscoordinatereferencesystem.h
QgsRasterDataProvider
Base class for raster data providers.
Definition: qgsrasterdataprovider.h:89
QgsFeatureSink
An interface for objects which accept features via addFeature(s) methods.
Definition: qgsfeaturesink.h:34
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50