QGIS API Documentation 3.41.0-Master (cea29feecf2)
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:
57 QgsZonalStatistics( QgsVectorLayer *polygonLayer, QgsRasterLayer *rasterLayer, const QString &attributePrefix = QString(), int rasterBand = 1, Qgis::ZonalStatistics stats = Qgis::ZonalStatistic::Default );
58
85 QgsZonalStatistics( QgsVectorLayer *polygonLayer, QgsRasterInterface *rasterInterface, const QgsCoordinateReferenceSystem &rasterCrs, double rasterUnitsPerPixelX, double rasterUnitsPerPixelY, const QString &attributePrefix = QString(), int rasterBand = 1, Qgis::ZonalStatistics stats = Qgis::ZonalStatistic::Default );
86
87
91 Qgis::ZonalStatisticResult calculateStatistics( QgsFeedback *feedback );
92
98 static QString displayName( Qgis::ZonalStatistic statistic );
99
105 static QString shortName( Qgis::ZonalStatistic statistic );
106
115#ifndef SIP_RUN
116 static QMap<Qgis::ZonalStatistic, QVariant> calculateStatistics( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics );
117#endif
118
120 // Required to fix https://github.com/qgis/QGIS/issues/43245 (SIP is failing to convert the enum to values)
121
130 static QMap<int, QVariant> calculateStatisticsInt( QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics ) SIP_PYNAME( calculateStatistics );
132
133 private:
134 QgsZonalStatistics() = default;
135
136 class FeatureStats
137 {
138 public:
139 FeatureStats( bool storeValues = false, bool storeValueCounts = false )
140 : mStoreValues( storeValues )
141 , mStoreValueCounts( storeValueCounts )
142 {
143 }
144
145 void reset()
146 {
147 sum = 0;
148 count = 0;
149 max = std::numeric_limits<double>::lowest();
150 min = std::numeric_limits<double>::max();
151 valueCount.clear();
152 values.clear();
153 }
154
155 void addValue( double value, const QgsPointXY &point, double weight = 1.0 )
156 {
157 if ( weight < 1.0 )
158 {
159 sum += value * weight;
160 count += weight;
161 }
162 else
163 {
164 sum += value;
165 ++count;
166 }
167 if ( value < min )
168 {
169 min = value;
170 minPoint = point;
171 }
172 if ( value > max )
173 {
174 max = value;
175 maxPoint = point;
176 }
177 if ( mStoreValueCounts )
178 valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
179 if ( mStoreValues )
180 values.append( value );
181 }
182 double sum = 0.0;
183 double count = 0.0;
184 double max = std::numeric_limits<double>::lowest();
185 double min = std::numeric_limits<double>::max();
186 QgsPointXY minPoint;
187 QgsPointXY maxPoint;
188 QMap<double, int> valueCount;
189 QList<double> values;
190
191 private:
192 bool mStoreValues = false;
193 bool mStoreValueCounts = false;
194 };
195
196 QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
197
198 QgsRasterInterface *mRasterInterface = nullptr;
200
201 double mCellSizeX = 0;
202 double mCellSizeY = 0;
203
205 int mRasterBand = 0;
206 QgsVectorLayer *mPolygonLayer = nullptr;
207 QString mAttributePrefix;
209};
210
211// clazy:excludeall=qstring-allocations
212
213#endif // QGSZONALSTATISTICS_H
ZonalStatistic
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:5406
@ 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:5441
QFlags< ZonalStatistic > ZonalStatistics
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:5432
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