QGIS API Documentation  2.99.0-Master (cb63e82)
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 #include <limits>
24 #include <cfloat>
25 #include "qgis_analysis.h"
26 
27 class QgsGeometry;
28 class QgsVectorLayer;
29 class QgsRasterLayer;
31 class QProgressDialog;
32 class QgsRectangle;
33 class QgsField;
34 
37 class ANALYSIS_EXPORT QgsZonalStatistics
38 {
39  public:
40 
42  enum Statistic
43  {
44  Count = 1,
45  Sum = 2,
46  Mean = 4,
47  Median = 8,
48  StDev = 16,
49  Min = 32,
50  Max = 64,
51  Range = 128,
52  Minority = 256,
53  Majority = 512,
54  Variety = 1024,
55  All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety
56  };
57  Q_DECLARE_FLAGS( Statistics, Statistic )
58 
59 
62  QgsZonalStatistics( QgsVectorLayer *polygonLayer, QgsRasterLayer *rasterLayer, const QString &attributePrefix = "", int rasterBand = 1,
63  Statistics stats = Statistics( Count | Sum | Mean ) );
64 
67  int calculateStatistics( QProgressDialog *p );
68 
69  private:
70  QgsZonalStatistics() = default;
71 
72  class FeatureStats
73  {
74  public:
75  FeatureStats( bool storeValues = false, bool storeValueCounts = false )
76  : mStoreValues( storeValues )
77  , mStoreValueCounts( storeValueCounts )
78  {
79  reset();
80  }
81  void reset() { sum = 0; count = 0; max = -FLT_MAX; min = FLT_MAX; valueCount.clear(); values.clear(); }
82  void addValue( float value, double weight = 1.0 )
83  {
84  if ( weight < 1.0 )
85  {
86  sum += value * weight;
87  count += weight;
88  }
89  else
90  {
91  sum += value;
92  ++count;
93  }
94  min = qMin( min, value );
95  max = qMax( max, value );
96  if ( mStoreValueCounts )
97  valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
98  if ( mStoreValues )
99  values.append( value );
100  }
101  double sum;
102  double count;
103  float max;
104  float min;
105  QMap< float, int > valueCount;
106  QList< float > values;
107 
108  private:
109  bool mStoreValues;
110  bool mStoreValueCounts;
111  };
112 
115  int cellInfoForBBox( const QgsRectangle &rasterBBox, const QgsRectangle &featureBBox, double cellSizeX, double cellSizeY,
116  int &offsetX, int &offsetY, int &nCellsX, int &nCellsY ) const;
117 
119  void statisticsFromMiddlePointTest( const QgsGeometry &poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
120  double cellSizeX, double cellSizeY, const QgsRectangle &rasterBBox, FeatureStats &stats );
121 
123  void statisticsFromPreciseIntersection( const QgsGeometry &poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
124  double cellSizeX, double cellSizeY, const QgsRectangle &rasterBBox, FeatureStats &stats );
125 
127  bool validPixel( float value ) const;
128 
129  QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
130 
131  QgsRasterLayer *mRasterLayer = nullptr;
132  QgsRasterDataProvider *mRasterProvider = nullptr;
134  int mRasterBand = 0;
135  QgsVectorLayer *mPolygonLayer = nullptr;
136  QString mAttributePrefix;
138  float mInputNodataValue = -1;
139  Statistics mStatistics = QgsZonalStatistics::All;
140 };
141 
142 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsZonalStatistics::Statistics )
143 
144 #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:79
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:45
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:452
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:437
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.