QGIS API Documentation 4.1.0-Master (ca2ac17535b)
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:
59 QgsVectorLayer *polygonLayer, QgsRasterLayer *rasterLayer, const QString &attributePrefix = QString(), int rasterBand = 1, Qgis::ZonalStatistics stats = Qgis::ZonalStatistic::Default
60 );
61
89 QgsVectorLayer *polygonLayer,
90 QgsRasterInterface *rasterInterface,
91 const QgsCoordinateReferenceSystem &rasterCrs,
92 double rasterUnitsPerPixelX,
93 double rasterUnitsPerPixelY,
94 const QString &attributePrefix = QString(),
95 int rasterBand = 1,
97 );
98
99
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(
129 QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics
130 );
131#endif
132
134 // Required to fix https://github.com/qgis/QGIS/issues/43245 (SIP is failing to convert the enum to values)
135
144 static QMap<int, QVariant> calculateStatisticsInt(
145 QgsRasterInterface *rasterInterface, const QgsGeometry &geometry, double cellSizeX, double cellSizeY, int rasterBand, Qgis::ZonalStatistics statistics
148
149 private:
150 QgsZonalStatistics() = default;
151
152#ifndef SIP_RUN
153 class FeatureStats
154 {
155 public:
156 FeatureStats( bool storeValues = false, bool storeValueCounts = false )
157 : mStoreValues( storeValues )
158 , mStoreValueCounts( storeValueCounts )
159 {}
160
161 void reset()
162 {
163 sum = 0;
164 count = 0;
165 max = std::numeric_limits<double>::lowest();
166 min = std::numeric_limits<double>::max();
167 valueCount.clear();
168 values.clear();
169 }
170
171 void addValue( double value, const QgsPointXY &point, double weight = 1.0 )
172 {
173 if ( !std::isnan( value ) )
174 {
175 if ( weight < 1.0 )
176 {
177 sum += value * weight;
178 count += weight;
179 }
180 else
181 {
182 sum += value;
183 ++count;
184 }
185 if ( value < min )
186 {
187 min = value;
188 minPoint = point;
189 }
190 if ( value > max )
191 {
192 max = value;
193 maxPoint = point;
194 }
195 }
196 else
197 {
198 ++count;
199 }
200 if ( mStoreValueCounts )
201 ++valueCount[value];
202 if ( mStoreValues )
203 values.append( value );
204 }
205 double sum = 0.0;
206 double count = 0.0;
207 double max = std::numeric_limits<double>::lowest();
208 double min = std::numeric_limits<double>::max();
209 QgsPointXY minPoint;
210 QgsPointXY maxPoint;
212 {
213 bool operator()( double lhs, double rhs ) const { return ( std::isnan( lhs ) && !std::isnan( rhs ) ) || lhs < rhs; }
214 };
215 std::map<double, int64_t, CompareNaNAware> valueCount;
216 QList<double> values;
217
218 private:
219 bool mStoreValues = false;
220 bool mStoreValueCounts = false;
221 };
222
223 QString getUniqueFieldName( const QString &fieldName, const QList<QgsField> &newFields );
224
225 QgsRasterInterface *mRasterInterface = nullptr;
227
228 double mCellSizeX = 0;
229 double mCellSizeY = 0;
230
232 int mRasterBand = 0;
233 QgsVectorLayer *mPolygonLayer = nullptr;
234 QString mAttributePrefix;
236#endif
237};
238
239// clazy:excludeall=qstring-allocations
240
241#endif // QGSZONALSTATISTICS_H
ZonalStatistic
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:6345
@ Default
Default statistics.
Definition qgis.h:6364
@ All
All statistics. For QGIS 3.x this includes ONLY numeric statistics, but for 4.0 this will be extended...
Definition qgis.h:6361
ZonalStatisticResult
Zonal statistics result codes.
Definition qgis.h:6382
QFlags< ZonalStatistic > ZonalStatistics
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:6373
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