QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 
31 class QgsGeometry;
32 class QgsVectorLayer;
33 class QgsRasterLayer;
34 class QgsRasterInterface;
36 class QgsRectangle;
37 class QgsField;
38 
43 class ANALYSIS_EXPORT QgsZonalStatistics
44 {
45  public:
46 
48  enum Statistic
49  {
50  Count = 1,
51  Sum = 2,
52  Mean = 4,
53  Median = 8,
54  StDev = 16,
55  Min = 32,
56  Max = 64,
57  Range = 128,
58  Minority = 256,
59  Majority = 512,
60  Variety = 1024,
61  Variance = 2048,
62  All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | Variance
63  };
64  Q_DECLARE_FLAGS( Statistics, Statistic )
65 
66 
74  QgsZonalStatistics( QgsVectorLayer *polygonLayer,
75  QgsRasterLayer *rasterLayer,
76  const QString &attributePrefix = QString(),
77  int rasterBand = 1,
78  QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics( QgsZonalStatistics::Count | QgsZonalStatistics::Sum | QgsZonalStatistics::Mean ) );
79 
106  QgsZonalStatistics( QgsVectorLayer *polygonLayer,
107  QgsRasterInterface *rasterInterface,
108  const QgsCoordinateReferenceSystem &rasterCrs,
109  double rasterUnitsPerPixelX,
110  double rasterUnitsPerPixelY,
111  const QString &attributePrefix = QString(),
112  int rasterBand = 1,
113  QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics( QgsZonalStatistics::Count | QgsZonalStatistics::Sum | QgsZonalStatistics::Mean ) );
114 
119  int calculateStatistics( QgsFeedback *feedback );
120 
126  static QString displayName( QgsZonalStatistics::Statistic statistic );
127 
133  static QString shortName( QgsZonalStatistics::Statistic statistic );
134 
135  private:
136  QgsZonalStatistics() = default;
137 
138  class FeatureStats
139  {
140  public:
141  FeatureStats( bool storeValues = false, bool storeValueCounts = false )
142  : mStoreValues( storeValues )
143  , mStoreValueCounts( storeValueCounts )
144  {
145  }
146 
147  void reset()
148  {
149  sum = 0;
150  count = 0;
151  max = std::numeric_limits<double>::lowest();
152  min = std::numeric_limits<double>::max();
153  valueCount.clear();
154  values.clear();
155  }
156 
157  void addValue( double value, double weight = 1.0 )
158  {
159  if ( weight < 1.0 )
160  {
161  sum += value * weight;
162  count += weight;
163  }
164  else
165  {
166  sum += value;
167  ++count;
168  }
169  min = std::min( min, value );
170  max = std::max( max, value );
171  if ( mStoreValueCounts )
172  valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
173  if ( mStoreValues )
174  values.append( value );
175  }
176  double sum = 0.0;
177  double count = 0.0;
178  double max = std::numeric_limits<double>::lowest();
179  double min = std::numeric_limits<double>::max();
180  QMap< double, int > valueCount;
181  QList< double > values;
182 
183  private:
184  bool mStoreValues = false;
185  bool mStoreValueCounts = false;
186  };
187 
188  QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
189 
190  QgsRasterInterface *mRasterInterface = nullptr;
191  QgsCoordinateReferenceSystem mRasterCrs;
192 
193  double mCellSizeX = 0;
194  double mCellSizeY = 0;
195 
197  int mRasterBand = 0;
198  QgsVectorLayer *mPolygonLayer = nullptr;
199  QString mAttributePrefix;
200  Statistics mStatistics = QgsZonalStatistics::All;
201 };
202 
203 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsZonalStatistics::Statistics )
204 
205 // clazy:excludeall=qstring-allocations
206 
207 #endif // QGSZONALSTATISTICS_H
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Statistic
Enumeration of flags that specify statistics to be calculated.
Represents a raster layer.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:122
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:45
Sum of pixel values.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:49
Base class for processing filters like renderers, reprojector, resampler etc.
Mean of pixel values.
This class represents a coordinate reference system (CRS).
A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and ...
Represents a vector layer which manages a vector based data sets.
Base class for raster data providers.