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