QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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"
30#include "qgsfields.h"
31
32class QgsGeometry;
33class QgsVectorLayer;
34class QgsRasterLayer;
37class QgsRectangle;
38class QgsField;
39class QgsFeatureSink;
41
46class ANALYSIS_EXPORT QgsZonalStatistics
47{
48 public:
49
59 QgsRasterLayer *rasterLayer,
60 const QString &attributePrefix = QString(),
61 int rasterBand = 1,
63
91 QgsRasterInterface *rasterInterface,
92 const QgsCoordinateReferenceSystem &rasterCrs,
93 double rasterUnitsPerPixelX,
94 double rasterUnitsPerPixelY,
95 const QString &attributePrefix = QString(),
96 int rasterBand = 1,
98
99
103 Qgis::ZonalStatisticResult calculateStatistics( QgsFeedback *feedback );
104
110 static QString displayName( Qgis::ZonalStatistic statistic );
111
117 static QString shortName( Qgis::ZonalStatistic statistic );
118
127#ifndef SIP_RUN
128 static QMap<Qgis::ZonalStatistic, QVariant> calculateStatistics( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics );
129#endif
130
132 // Required to fix https://github.com/qgis/QGIS/issues/43245 (SIP is failing to convert the enum to values)
133
142 static QMap<int, QVariant> calculateStatisticsInt( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics ) SIP_PYNAME( calculateStatistics );
144
145 private:
146 QgsZonalStatistics() = default;
147
148 class FeatureStats
149 {
150 public:
151 FeatureStats( bool storeValues = false, bool storeValueCounts = false )
152 : mStoreValues( storeValues )
153 , mStoreValueCounts( storeValueCounts )
154 {
155 }
156
157 void reset()
158 {
159 sum = 0;
160 count = 0;
161 max = std::numeric_limits<double>::lowest();
162 min = std::numeric_limits<double>::max();
163 valueCount.clear();
164 values.clear();
165 }
166
167 void addValue( double value, const QgsPointXY &point, double weight = 1.0 )
168 {
169 if ( weight < 1.0 )
170 {
171 sum += value * weight;
172 count += weight;
173 }
174 else
175 {
176 sum += value;
177 ++count;
178 }
179 if ( value < min )
180 {
181 min = value;
182 minPoint = point;
183 }
184 if ( value > max )
185 {
186 max = value;
187 maxPoint = point;
188 }
189 if ( mStoreValueCounts )
190 valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
191 if ( mStoreValues )
192 values.append( value );
193 }
194 double sum = 0.0;
195 double count = 0.0;
196 double max = std::numeric_limits<double>::lowest();
197 double min = std::numeric_limits<double>::max();
198 QgsPointXY minPoint;
199 QgsPointXY maxPoint;
200 QMap< double, int > valueCount;
201 QList< double > values;
202
203 private:
204 bool mStoreValues = false;
205 bool mStoreValueCounts = false;
206 };
207
208 QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
209
210 QgsRasterInterface *mRasterInterface = nullptr;
212
213 double mCellSizeX = 0;
214 double mCellSizeY = 0;
215
217 int mRasterBand = 0;
218 QgsVectorLayer *mPolygonLayer = nullptr;
219 QString mAttributePrefix;
221};
222
223// clazy:excludeall=qstring-allocations
224
225#endif // QGSZONALSTATISTICS_H
ZonalStatistic
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:5361
@ Default
Default statistics.
@ All
All statistics. For QGIS 3.x this includes ONLY numeric statistics, but for 4.0 this will be extended...
ZonalStatisticResult
Zonal statistics result codes.
Definition qgis.h:5396
QFlags< ZonalStatistic > ZonalStatistics
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:5387
This class represents a coordinate reference system (CRS).
An interface for objects which accept features via addFeature(s) methods.
An interface for objects which provide features via a getFeatures method.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
A geometry is the spatial representation of a feature.
A class to represent a 2D point.
Definition qgspointxy.h:60
Base class for raster data providers.
Base class for processing filters like renderers, reprojector, resampler etc.
Represents a raster layer.
A rectangle specified with double values.
Represents a vector layer which manages a vector based data sets.
A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and ...
#define SIP_PYNAME(name)
Definition qgis_sip.h:81