QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
24#include "qgis_analysis.h"
26#include "qgsfeedback.h"
27#include "qgsfields.h"
28
29#include <QMap>
30#include <QString>
31
32class QgsGeometry;
33class QgsVectorLayer;
34class QgsRasterLayer;
37class QgsRectangle;
38class QgsField;
39class QgsFeatureSink;
41
46class ANALYSIS_EXPORT QgsZonalStatistics
47{
48 public:
58 QgsVectorLayer *polygonLayer, QgsRasterLayer *rasterLayer, const QString &attributePrefix = QString(), int rasterBand = 1, Qgis::ZonalStatistics stats = Qgis::ZonalStatistic::Default
59 );
60
88 QgsVectorLayer *polygonLayer,
89 QgsRasterInterface *rasterInterface,
90 const QgsCoordinateReferenceSystem &rasterCrs,
91 double rasterUnitsPerPixelX,
92 double rasterUnitsPerPixelY,
93 const QString &attributePrefix = QString(),
94 int rasterBand = 1,
96 );
97
98
103
109 static QString displayName( Qgis::ZonalStatistic statistic );
110
116 static QString shortName( Qgis::ZonalStatistic statistic );
117
126#ifndef SIP_RUN
127 static QMap<Qgis::ZonalStatistic, QVariant> calculateStatistics(
128 QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics
129 );
130#endif
131
133 // Required to fix https://github.com/qgis/QGIS/issues/43245 (SIP is failing to convert the enum to values)
134
143 static QMap<int, QVariant> calculateStatisticsInt(
144 QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics
147
148 private:
149 QgsZonalStatistics() = default;
150
151 class FeatureStats
152 {
153 public:
154 FeatureStats( bool storeValues = false, bool storeValueCounts = false )
155 : mStoreValues( storeValues )
156 , mStoreValueCounts( storeValueCounts )
157 {}
158
159 void reset()
160 {
161 sum = 0;
162 count = 0;
163 max = std::numeric_limits<double>::lowest();
164 min = std::numeric_limits<double>::max();
165 valueCount.clear();
166 values.clear();
167 }
168
169 void addValue( double value, const QgsPointXY &point, double weight = 1.0 )
170 {
171 if ( weight < 1.0 )
172 {
173 sum += value * weight;
174 count += weight;
175 }
176 else
177 {
178 sum += value;
179 ++count;
180 }
181 if ( value < min )
182 {
183 min = value;
184 minPoint = point;
185 }
186 if ( value > max )
187 {
188 max = value;
189 maxPoint = point;
190 }
191 if ( mStoreValueCounts )
192 valueCount.insert( value, valueCount.value( value, 0 ) + 1 );
193 if ( mStoreValues )
194 values.append( value );
195 }
196 double sum = 0.0;
197 double count = 0.0;
198 double max = std::numeric_limits<double>::lowest();
199 double min = std::numeric_limits<double>::max();
200 QgsPointXY minPoint;
201 QgsPointXY maxPoint;
202 QMap<double, int> valueCount;
203 QList<double> values;
204
205 private:
206 bool mStoreValues = false;
207 bool mStoreValueCounts = false;
208 };
209
210 QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
211
212 QgsRasterInterface *mRasterInterface = nullptr;
214
215 double mCellSizeX = 0;
216 double mCellSizeY = 0;
217
219 int mRasterBand = 0;
220 QgsVectorLayer *mPolygonLayer = nullptr;
221 QString mAttributePrefix;
223};
224
225// clazy:excludeall=qstring-allocations
226
227#endif // QGSZONALSTATISTICS_H
ZonalStatistic
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:6119
@ Default
Default statistics.
Definition qgis.h:6138
@ All
All statistics. For QGIS 3.x this includes ONLY numeric statistics, but for 4.0 this will be extended...
Definition qgis.h:6135
ZonalStatisticResult
Zonal statistics result codes.
Definition qgis.h:6156
QFlags< ZonalStatistic > ZonalStatistics
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:6147
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.
QgsZonalStatistics(QgsVectorLayer *polygonLayer, QgsRasterLayer *rasterLayer, const QString &attributePrefix=QString(), int rasterBand=1, Qgis::ZonalStatistics stats=Qgis::ZonalStatistic::Default)
Convenience constructor for QgsZonalStatistics, using an input raster layer.
Qgis::ZonalStatisticResult calculateStatistics(QgsFeedback *feedback)
Runs the calculation.
static QString displayName(Qgis::ZonalStatistic statistic)
Returns the friendly display name for a statistic.
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