QGIS API Documentation  2.99.0-Master (9f5e33a)
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 QProgressDialog;
35 class QgsRectangle;
36 class QgsField;
37 
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  All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety
59  };
60  Q_DECLARE_FLAGS( Statistics, Statistic )
61 
62 
65  QgsZonalStatistics( QgsVectorLayer *polygonLayer,
66  QgsRasterLayer *rasterLayer,
67  const QString &attributePrefix = "",
68  int rasterBand = 1,
69  QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics( QgsZonalStatistics::Count | QgsZonalStatistics::Sum | QgsZonalStatistics::Mean ) );
70 
73  int calculateStatistics( QgsFeedback *feedback );
74 
75  private:
76  QgsZonalStatistics() = default;
77 
78  class FeatureStats
79  {
80  public:
81  FeatureStats( bool storeValues = false, bool storeValueCounts = false )
82  : mStoreValues( storeValues )
83  , mStoreValueCounts( storeValueCounts )
84  {
85  reset();
86  }
87  void reset() { sum = 0; count = 0; max = -FLT_MAX; min = FLT_MAX; valueCount.clear(); values.clear(); }
88  void addValue( float value, double weight = 1.0 )
89  {
90  if ( weight < 1.0 )
91  {
92  sum += value * weight;
93  count += weight;
94  }
95  else
96  {
97  sum += value;
98  ++count;
99  }
100  min = qMin( min, value );
101  max = qMax( max, value );
102  if ( mStoreValueCounts )
103  valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
104  if ( mStoreValues )
105  values.append( value );
106  }
107  double sum;
108  double count;
109  float max;
110  float min;
111  QMap< float, int > valueCount;
112  QList< float > values;
113 
114  private:
115  bool mStoreValues;
116  bool mStoreValueCounts;
117  };
118 
121  int cellInfoForBBox( const QgsRectangle &rasterBBox, const QgsRectangle &featureBBox, double cellSizeX, double cellSizeY,
122  int &offsetX, int &offsetY, int &nCellsX, int &nCellsY ) const;
123 
125  void statisticsFromMiddlePointTest( const QgsGeometry &poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
126  double cellSizeX, double cellSizeY, const QgsRectangle &rasterBBox, FeatureStats &stats );
127 
129  void statisticsFromPreciseIntersection( const QgsGeometry &poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
130  double cellSizeX, double cellSizeY, const QgsRectangle &rasterBBox, FeatureStats &stats );
131 
133  bool validPixel( float value ) const;
134 
135  QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
136 
137  QgsRasterLayer *mRasterLayer = nullptr;
138  QgsRasterDataProvider *mRasterProvider = nullptr;
140  int mRasterBand = 0;
141  QgsVectorLayer *mPolygonLayer = nullptr;
142  QString mAttributePrefix;
144  float mInputNodataValue = -1;
145  Statistics mStatistics = QgsZonalStatistics::All;
146 };
147 
148 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsZonalStatistics::Statistics )
149 
150 #endif // QGSZONALSTATISTICS_H
A rectangle specified with double values.
Definition: qgsrectangle.h:38
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:96
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:43
Sum of pixel values.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:46
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
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.