QGIS API Documentation 4.3.0-Master (52aa6503bed)
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 <cfloat>
22#include <limits>
23#include <map>
24
25#include "qgis_analysis.h"
27#include "qgsfeedback.h"
28#include "qgsfields.h"
29
30#include <QMap>
31#include <QString>
32
33class QgsGeometry;
34class QgsVectorLayer;
35class QgsRasterLayer;
38class QgsRectangle;
39class QgsField;
40class QgsFeatureSink;
42
47class ANALYSIS_EXPORT QgsZonalStatistics
48{
49 public:
62 QgsVectorLayer *polygonLayer,
63 QgsRasterLayer *rasterLayer,
64 const QString &attributePrefix = QString(),
65 int rasterBand = 1,
67 bool overwriteExistingFields = false
68 );
69
100 QgsVectorLayer *polygonLayer,
101 QgsRasterInterface *rasterInterface,
102 const QgsCoordinateReferenceSystem &rasterCrs,
103 double rasterUnitsPerPixelX,
104 double rasterUnitsPerPixelY,
105 const QString &attributePrefix = QString(),
106 int rasterBand = 1,
108 bool overwriteExistingFields = false
109 );
110
111
116
122 static QString displayName( Qgis::ZonalStatistic statistic );
123
129 static QString shortName( Qgis::ZonalStatistic statistic );
130
139#ifndef SIP_RUN
140 static QMap<Qgis::ZonalStatistic, QVariant> calculateStatistics(
141 QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics
142 );
143#endif
144
146 // Required to fix https://github.com/qgis/QGIS/issues/43245 (SIP is failing to convert the enum to values)
147
156 static QMap<int, QVariant> calculateStatisticsInt(
157 QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics
160
161 private:
162 QgsZonalStatistics() = default;
163
164#ifndef SIP_RUN
165 class FeatureStats
166 {
167 public:
168 FeatureStats( bool storeValues = false, bool storeValueCounts = false )
169 : mStoreValues( storeValues )
170 , mStoreValueCounts( storeValueCounts )
171 {}
172
173 void reset()
174 {
175 sum = 0;
176 count = 0;
177 max = std::numeric_limits<double>::lowest();
178 min = std::numeric_limits<double>::max();
179 valueCount.clear();
180 values.clear();
181 }
182
183 void addValue( double value, const QgsPointXY &point, double weight = 1.0 )
184 {
185 if ( !std::isnan( value ) )
186 {
187 if ( weight < 1.0 )
188 {
189 sum += value * weight;
190 count += weight;
191 }
192 else
193 {
194 sum += value;
195 ++count;
196 }
197 if ( value < min )
198 {
199 min = value;
200 minPoint = point;
201 }
202 if ( value > max )
203 {
204 max = value;
205 maxPoint = point;
206 }
207 }
208 else
209 {
210 ++count;
211 }
212 if ( mStoreValueCounts )
213 ++valueCount[value];
214 if ( mStoreValues )
215 values.append( value );
216 }
217 double sum = 0.0;
218 double count = 0.0;
219 double max = std::numeric_limits<double>::lowest();
220 double min = std::numeric_limits<double>::max();
221 QgsPointXY minPoint;
222 QgsPointXY maxPoint;
224 {
225 bool operator()( double lhs, double rhs ) const { return ( std::isnan( lhs ) && !std::isnan( rhs ) ) || lhs < rhs; }
226 };
227 std::map<double, int64_t, CompareNaNAware> valueCount;
228 QList<double> values;
229
230 private:
231 bool mStoreValues = false;
232 bool mStoreValueCounts = false;
233 };
234
235 QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
236
237 QgsRasterInterface *mRasterInterface = nullptr;
239
240 double mCellSizeX = 0;
241 double mCellSizeY = 0;
242
244 int mRasterBand = 0;
245 QgsVectorLayer *mPolygonLayer = nullptr;
246 QString mAttributePrefix;
248 bool mOverwriteExistingFields = false;
249#endif
250};
251
252// clazy:excludeall=qstring-allocations
253
254#endif // QGSZONALSTATISTICS_H
ZonalStatistic
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:6583
@ Default
Default statistics.
Definition qgis.h:6602
@ All
All statistics. For QGIS 3.x this includes ONLY numeric statistics, but for 4.0 this will be extended...
Definition qgis.h:6599
ZonalStatisticResult
Zonal statistics result codes.
Definition qgis.h:6620
QFlags< ZonalStatistic > ZonalStatistics
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:6611
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:56
A geometry is the spatial representation of a feature.
Represents a 2D point.
Definition qgspointxy.h:62
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 dataset.
Qgis::ZonalStatisticResult calculateStatistics(QgsFeedback *feedback)
Runs the calculation.
static QString displayName(Qgis::ZonalStatistic statistic)
Returns the friendly display name for a statistic.
QgsZonalStatistics(QgsVectorLayer *polygonLayer, QgsRasterLayer *rasterLayer, const QString &attributePrefix=QString(), int rasterBand=1, Qgis::ZonalStatistics stats=Qgis::ZonalStatistic::Default, bool overwriteExistingFields=false)
Convenience constructor for QgsZonalStatistics, using an input raster layer.
static QString shortName(Qgis::ZonalStatistic statistic)
Returns a short, friendly display name for a statistic, suitable for use in a field name.
#define SIP_PYNAME(name)
Definition qgis_sip.h:88