QGIS API Documentation  3.0.2-Girona (307d082)
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 
24 #include <limits>
25 #include <cfloat>
26 
27 #include "qgis_analysis.h"
28 #include "qgsfeedback.h"
29 
30 class QgsGeometry;
31 class QgsVectorLayer;
32 class QgsRasterLayer;
34 class QgsRectangle;
35 class QgsField;
36 
40 class ANALYSIS_EXPORT QgsZonalStatistics
41 {
42  public:
43 
45  enum Statistic
46  {
47  Count = 1,
48  Sum = 2,
49  Mean = 4,
50  Median = 8,
51  StDev = 16,
52  Min = 32,
53  Max = 64,
54  Range = 128,
55  Minority = 256,
56  Majority = 512,
57  Variety = 1024,
58  Variance = 2048,
59  All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | Variance
60  };
61  Q_DECLARE_FLAGS( Statistics, Statistic )
62 
63 
66  QgsZonalStatistics( QgsVectorLayer *polygonLayer,
67  QgsRasterLayer *rasterLayer,
68  const QString &attributePrefix = QString(),
69  int rasterBand = 1,
70  QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics( QgsZonalStatistics::Count | QgsZonalStatistics::Sum | QgsZonalStatistics::Mean ) );
71 
75  int calculateStatistics( QgsFeedback *feedback );
76 
77  private:
78  QgsZonalStatistics() = default;
79 
80  class FeatureStats
81  {
82  public:
83  FeatureStats( bool storeValues = false, bool storeValueCounts = false )
84  : mStoreValues( storeValues )
85  , mStoreValueCounts( storeValueCounts )
86  {
87  reset();
88  }
89  void reset() { sum = 0; count = 0; max = -FLT_MAX; min = FLT_MAX; valueCount.clear(); values.clear(); }
90  void addValue( float value, double weight = 1.0 )
91  {
92  if ( weight < 1.0 )
93  {
94  sum += value * weight;
95  count += weight;
96  }
97  else
98  {
99  sum += value;
100  ++count;
101  }
102  min = std::min( min, value );
103  max = std::max( max, value );
104  if ( mStoreValueCounts )
105  valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
106  if ( mStoreValues )
107  values.append( value );
108  }
109  double sum;
110  double count;
111  float max;
112  float min;
113  QMap< float, int > valueCount;
114  QList< float > values;
115 
116  private:
117  bool mStoreValues;
118  bool mStoreValueCounts;
119  };
120 
124  int cellInfoForBBox( const QgsRectangle &rasterBBox, const QgsRectangle &featureBBox, double cellSizeX, double cellSizeY,
125  int &offsetX, int &offsetY, int &nCellsX, int &nCellsY ) const;
126 
128  void statisticsFromMiddlePointTest( const QgsGeometry &poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
129  double cellSizeX, double cellSizeY, const QgsRectangle &rasterBBox, FeatureStats &stats );
130 
132  void statisticsFromPreciseIntersection( const QgsGeometry &poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
133  double cellSizeX, double cellSizeY, const QgsRectangle &rasterBBox, FeatureStats &stats );
134 
136  bool validPixel( float value ) const;
137 
138  QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
139 
140  QgsRasterLayer *mRasterLayer = nullptr;
141  QgsRasterDataProvider *mRasterProvider = nullptr;
143  int mRasterBand = 0;
144  QgsVectorLayer *mPolygonLayer = nullptr;
145  QString mAttributePrefix;
147  float mInputNodataValue = -1;
148  Statistics mStatistics = QgsZonalStatistics::All;
149 };
150 
151 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsZonalStatistics::Statistics )
152 
153 // clazy:excludeall=qstring-allocations
154 
155 #endif // QGSZONALSTATISTICS_H
A rectangle specified with double values.
Definition: qgsrectangle.h:39
Statistic
Enumeration of flags that specify statistics to be calculated.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:44
Sum of pixel values.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
Mean of pixel values.
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.
Base class for raster data providers.