QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
121  private:
122  QgsZonalStatistics() = default;
123 
124  class FeatureStats
125  {
126  public:
127  FeatureStats( bool storeValues = false, bool storeValueCounts = false )
128  : mStoreValues( storeValues )
129  , mStoreValueCounts( storeValueCounts )
130  {
131  }
132 
133  void reset()
134  {
135  sum = 0;
136  count = 0;
137  max = std::numeric_limits<double>::lowest();
138  min = std::numeric_limits<double>::max();
139  valueCount.clear();
140  values.clear();
141  }
142 
143  void addValue( double value, double weight = 1.0 )
144  {
145  if ( weight < 1.0 )
146  {
147  sum += value * weight;
148  count += weight;
149  }
150  else
151  {
152  sum += value;
153  ++count;
154  }
155  min = std::min( min, value );
156  max = std::max( max, value );
157  if ( mStoreValueCounts )
158  valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
159  if ( mStoreValues )
160  values.append( value );
161  }
162  double sum = 0.0;
163  double count = 0.0;
164  double max = std::numeric_limits<double>::lowest();
165  double min = std::numeric_limits<double>::max();
166  QMap< double, int > valueCount;
167  QList< double > values;
168 
169  private:
170  bool mStoreValues = false;
171  bool mStoreValueCounts = false;
172  };
173 
174  QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
175 
176  QgsRasterInterface *mRasterInterface = nullptr;
177  QgsCoordinateReferenceSystem mRasterCrs;
178 
179  double mCellSizeX = 0;
180  double mCellSizeY = 0;
181 
183  int mRasterBand = 0;
184  QgsVectorLayer *mPolygonLayer = nullptr;
185  QString mAttributePrefix;
186  Statistics mStatistics = QgsZonalStatistics::All;
187 };
188 
189 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsZonalStatistics::Statistics )
190 
191 // clazy:excludeall=qstring-allocations
192 
193 #endif // QGSZONALSTATISTICS_H
A rectangle specified with double values.
Definition: qgsrectangle.h:40
Statistic
Enumeration of flags that specify statistics to be calculated.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
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:48
Base class for processing filters like renderers, reprojector, resampler etc.
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.