QGIS API Documentation  2.99.0-Master (f867b65)
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"
29 
30 class QgsGeometry;
31 class QgsVectorLayer;
32 class QgsRasterLayer;
34 class QProgressDialog;
35 class QgsRectangle;
36 class QgsField;
37 
40 class ANALYSIS_EXPORT QgsZonalStatistics
41 {
42  public:
43 
45  enum Statistic
46  {
47  Count = 1,
48  Sum = 2,
49  Mean = 4,
50  Median = 8,
51  StDev = 16,
52  Min = 32,
53  Max = 64,
54  Range = 128,
55  Minority = 256,
56  Majority = 512,
57  Variety = 1024,
58  Variance = 2048,
59  All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | Variance
60  };
61  Q_DECLARE_FLAGS( Statistics, Statistic )
62 
63 
66  QgsZonalStatistics( QgsVectorLayer *polygonLayer,
67  QgsRasterLayer *rasterLayer,
68  const QString &attributePrefix = "",
69  int rasterBand = 1,
70  QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics( QgsZonalStatistics::Count | QgsZonalStatistics::Sum | QgsZonalStatistics::Mean ) );
71 
74  int calculateStatistics( QgsFeedback *feedback );
75 
76  private:
77  QgsZonalStatistics() = default;
78 
79  class FeatureStats
80  {
81  public:
82  FeatureStats( bool storeValues = false, bool storeValueCounts = false )
83  : mStoreValues( storeValues )
84  , mStoreValueCounts( storeValueCounts )
85  {
86  reset();
87  }
88  void reset() { sum = 0; count = 0; max = -FLT_MAX; min = FLT_MAX; valueCount.clear(); values.clear(); }
89  void addValue( float value, double weight = 1.0 )
90  {
91  if ( weight < 1.0 )
92  {
93  sum += value * weight;
94  count += weight;
95  }
96  else
97  {
98  sum += value;
99  ++count;
100  }
101  min = qMin( min, value );
102  max = qMax( max, value );
103  if ( mStoreValueCounts )
104  valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
105  if ( mStoreValues )
106  values.append( value );
107  }
108  double sum;
109  double count;
110  float max;
111  float min;
112  QMap< float, int > valueCount;
113  QList< float > values;
114 
115  private:
116  bool mStoreValues;
117  bool mStoreValueCounts;
118  };
119 
122  int cellInfoForBBox( const QgsRectangle &rasterBBox, const QgsRectangle &featureBBox, double cellSizeX, double cellSizeY,
123  int &offsetX, int &offsetY, int &nCellsX, int &nCellsY ) const;
124 
126  void statisticsFromMiddlePointTest( const QgsGeometry &poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
127  double cellSizeX, double cellSizeY, const QgsRectangle &rasterBBox, FeatureStats &stats );
128 
130  void statisticsFromPreciseIntersection( const QgsGeometry &poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
131  double cellSizeX, double cellSizeY, const QgsRectangle &rasterBBox, FeatureStats &stats );
132 
134  bool validPixel( float value ) const;
135 
136  QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
137 
138  QgsRasterLayer *mRasterLayer = nullptr;
139  QgsRasterDataProvider *mRasterProvider = nullptr;
141  int mRasterBand = 0;
142  QgsVectorLayer *mPolygonLayer = nullptr;
143  QString mAttributePrefix;
145  float mInputNodataValue = -1;
146  Statistics mStatistics = QgsZonalStatistics::All;
147 };
148 
149 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsZonalStatistics::Statistics )
150 
151 #endif // QGSZONALSTATISTICS_H
A rectangle specified with double values.
Definition: qgsrectangle.h:38
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:96
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:43
Sum of pixel values.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:46
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
Definition: MathUtils.cc:451
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
Definition: MathUtils.cc:436
Mean of pixel values.
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.