QGIS API Documentation  2.11.0-Master
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 "qgsrectangle.h"
22 #include <QString>
23 
24 class QgsGeometry;
25 class QgsVectorLayer;
26 class QProgressDialog;
27 
29 class ANALYSIS_EXPORT QgsZonalStatistics
30 {
31  public:
32 
34  enum Statistic
35  {
36  Count = 1,
37  Sum = 2,
38  Mean = 4,
39  Median = 8,
40  StDev = 16,
41  Min = 32,
42  Max = 64,
43  Range = 128,
44  Minority = 256,
45  Majority = 512,
46  Variety = 1024,
47  All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety
48  };
49  Q_DECLARE_FLAGS( Statistics, Statistic )
50 
51  QgsZonalStatistics( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix = "", int rasterBand = 1,
52  Statistics stats = Statistics( Count | Sum | Mean ) );
54 
57  int calculateStatistics( QProgressDialog* p );
58 
59  private:
61 
62  class FeatureStats
63  {
64  public:
65  FeatureStats( bool storeValues = false, bool storeValueCounts = false )
66  : mStoreValues( storeValues )
67  , mStoreValueCounts( storeValueCounts )
68  {
69  reset();
70  }
71  void reset() { sum = 0; count = 0; max = FLT_MIN; min = FLT_MAX; valueCount.clear(); values.clear(); }
72  void addValue( float value, double weight = 1.0 )
73  {
74  if ( weight < 1.0 )
75  {
76  sum += value * weight;
77  count += weight;
78  }
79  else
80  {
81  sum += value;
82  ++count;
83  }
84  min = qMin( min, value );
85  max = qMax( max, value );
86  if ( mStoreValueCounts )
87  valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
88  if ( mStoreValues )
89  values.append( value );
90  }
91  double sum;
92  double count;
93  float max;
94  float min;
95  QMap< float, int > valueCount;
96  QList< float > values;
97 
98  private:
99  bool mStoreValues;
100  bool mStoreValueCounts;
101  };
102 
105  int cellInfoForBBox( const QgsRectangle& rasterBBox, const QgsRectangle& featureBBox, double cellSizeX, double cellSizeY,
106  int& offsetX, int& offsetY, int& nCellsX, int& nCellsY ) const;
107 
109  void statisticsFromMiddlePointTest( void* band, const QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
110  double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, FeatureStats& stats );
111 
113  void statisticsFromPreciseIntersection( void* band, const QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
114  double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, FeatureStats& stats );
115 
117  bool validPixel( float value ) const;
118 
119  QString getUniqueFieldName( QString fieldName );
120 
121  QString mRasterFilePath;
123  int mRasterBand;
124  QgsVectorLayer* mPolygonLayer;
125  QString mAttributePrefix;
127  float mInputNodataValue;
128  Statistics mStatistics;
129 };
130 
131 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsZonalStatistics::Statistics )
132 
133 #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:75
int min(int a, int b)
Definition: util.h:88
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.
int max(int a, int b)
Definition: util.h:82