QGIS API Documentation  2.15.0-Master (972fc9f)
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  const Statistics& stats = Statistics( Count | Sum | Mean ) );
53 
56  int calculateStatistics( QProgressDialog* p );
57 
58  private:
60 
61  class FeatureStats
62  {
63  public:
64  FeatureStats( bool storeValues = false, bool storeValueCounts = false )
65  : mStoreValues( storeValues )
66  , mStoreValueCounts( storeValueCounts )
67  {
68  reset();
69  }
70  void reset() { sum = 0; count = 0; max = -FLT_MAX; min = FLT_MAX; valueCount.clear(); values.clear(); }
71  void addValue( float value, double weight = 1.0 )
72  {
73  if ( weight < 1.0 )
74  {
75  sum += value * weight;
76  count += weight;
77  }
78  else
79  {
80  sum += value;
81  ++count;
82  }
83  min = qMin( min, value );
84  max = qMax( max, value );
85  if ( mStoreValueCounts )
86  valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
87  if ( mStoreValues )
88  values.append( value );
89  }
90  double sum;
91  double count;
92  float max;
93  float min;
94  QMap< float, int > valueCount;
95  QList< float > values;
96 
97  private:
98  bool mStoreValues;
99  bool mStoreValueCounts;
100  };
101 
104  int cellInfoForBBox( const QgsRectangle& rasterBBox, const QgsRectangle& featureBBox, double cellSizeX, double cellSizeY,
105  int& offsetX, int& offsetY, int& nCellsX, int& nCellsY ) const;
106 
108  void statisticsFromMiddlePointTest( void* band, const QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
109  double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, FeatureStats& stats );
110 
112  void statisticsFromPreciseIntersection( void* band, const QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
113  double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, FeatureStats& stats );
114 
116  bool validPixel( float value ) const;
117 
118  QString getUniqueFieldName( const QString& fieldName );
119 
120  QString mRasterFilePath;
122  int mRasterBand;
123  QgsVectorLayer* mPolygonLayer;
124  QString mAttributePrefix;
126  float mInputNodataValue;
127  Statistics mStatistics;
128 };
129 
130 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsZonalStatistics::Statistics )
131 
132 #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:76
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and ...
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
Represents a vector layer which manages a vector based data sets.