QGIS API Documentation  2.99.0-Master (c42dad3)
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 
26 class QgsGeometry;
27 class QgsVectorLayer;
28 class QProgressDialog;
29 class QgsRectangle;
30 
33 class ANALYSIS_EXPORT QgsZonalStatistics
34 {
35  public:
36 
38  enum Statistic
39  {
40  Count = 1,
41  Sum = 2,
42  Mean = 4,
43  Median = 8,
44  StDev = 16,
45  Min = 32,
46  Max = 64,
47  Range = 128,
48  Minority = 256,
49  Majority = 512,
50  Variety = 1024,
51  All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety
52  };
53  Q_DECLARE_FLAGS( Statistics, Statistic )
54 
55 
58  QgsZonalStatistics( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix = "", int rasterBand = 1,
59  Statistics stats = Statistics( Count | Sum | Mean ) );
60 
63  int calculateStatistics( QProgressDialog* p );
64 
65  private:
67 
68  class FeatureStats
69  {
70  public:
71  FeatureStats( bool storeValues = false, bool storeValueCounts = false )
72  : mStoreValues( storeValues )
73  , mStoreValueCounts( storeValueCounts )
74  {
75  reset();
76  }
77  void reset() { sum = 0; count = 0; max = -FLT_MAX; min = FLT_MAX; valueCount.clear(); values.clear(); }
78  void addValue( float value, double weight = 1.0 )
79  {
80  if ( weight < 1.0 )
81  {
82  sum += value * weight;
83  count += weight;
84  }
85  else
86  {
87  sum += value;
88  ++count;
89  }
90  min = qMin( min, value );
91  max = qMax( max, value );
92  if ( mStoreValueCounts )
93  valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
94  if ( mStoreValues )
95  values.append( value );
96  }
97  double sum;
98  double count;
99  float max;
100  float min;
101  QMap< float, int > valueCount;
102  QList< float > values;
103 
104  private:
105  bool mStoreValues;
106  bool mStoreValueCounts;
107  };
108 
111  int cellInfoForBBox( const QgsRectangle& rasterBBox, const QgsRectangle& featureBBox, double cellSizeX, double cellSizeY,
112  int& offsetX, int& offsetY, int& nCellsX, int& nCellsY ) const;
113 
115  void statisticsFromMiddlePointTest( void* band, const QgsGeometry& poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
116  double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, FeatureStats& stats );
117 
119  void statisticsFromPreciseIntersection( void* band, const QgsGeometry& poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
120  double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, FeatureStats& stats );
121 
123  bool validPixel( float value ) const;
124 
125  QString getUniqueFieldName( const QString& fieldName );
126 
127  QString mRasterFilePath;
129  int mRasterBand;
130  QgsVectorLayer* mPolygonLayer;
131  QString mAttributePrefix;
133  float mInputNodataValue;
134  Statistics mStatistics;
135 };
136 
137 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsZonalStatistics::Statistics )
138 
139 #endif // QGSZONALSTATISTICS_H
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Statistic
Enumeration of flags that specify statistics to be calculated.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:78
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.