QGIS API Documentation  2.18.3-Las Palmas (77b8c3d)
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
30 class ANALYSIS_EXPORT QgsZonalStatistics
31 {
32  public:
33
35  enum Statistic
36  {
37  Count = 1,
38  Sum = 2,
39  Mean = 4,
40  Median = 8,
41  StDev = 16,
42  Min = 32,
43  Max = 64,
44  Range = 128,
45  Minority = 256,
46  Majority = 512,
47  Variety = 1024,
48  All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety
49  };
50  Q_DECLARE_FLAGS( Statistics, Statistic )
51
52  QgsZonalStatistics( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix = "", int rasterBand = 1,
53  const 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_MAX; 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( const 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: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.