QGIS API Documentation
4.3.0-Master (52aa6503bed)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
analysis
vector
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"
26
#include "
qgscoordinatereferencesystem.h
"
27
#include "
qgsfeedback.h
"
28
#include "
qgsfields.h
"
29
30
#include <QMap>
31
#include <QString>
32
33
class
QgsGeometry
;
34
class
QgsVectorLayer
;
35
class
QgsRasterLayer
;
36
class
QgsRasterInterface
;
37
class
QgsRasterDataProvider
;
38
class
QgsRectangle
;
39
class
QgsField
;
40
class
QgsFeatureSink
;
41
class
QgsFeatureSource
;
42
47
class
ANALYSIS_EXPORT
QgsZonalStatistics
48
{
49
public
:
61
QgsZonalStatistics
(
62
QgsVectorLayer
*polygonLayer,
63
QgsRasterLayer
*rasterLayer,
64
const
QString &attributePrefix = QString(),
65
int
rasterBand = 1,
66
Qgis::ZonalStatistics
stats =
Qgis::ZonalStatistic::Default
,
67
bool
overwriteExistingFields =
false
68
);
69
99
QgsZonalStatistics
(
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,
107
Qgis::ZonalStatistics
stats =
Qgis::ZonalStatistic::Default
,
108
bool
overwriteExistingFields =
false
109
);
110
111
115
Qgis::ZonalStatisticResult
calculateStatistics
(
QgsFeedback
*feedback );
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
158
)
SIP_PYNAME
(
calculateStatistics
);
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;
223
struct
CompareNaNAware
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
;
238
QgsCoordinateReferenceSystem
mRasterCrs;
239
240
double
mCellSizeX = 0;
241
double
mCellSizeY = 0;
242
244
int
mRasterBand = 0;
245
QgsVectorLayer
*mPolygonLayer =
nullptr
;
246
QString mAttributePrefix;
247
Qgis::ZonalStatistics
mStatistics =
Qgis::ZonalStatistic::All
;
248
bool
mOverwriteExistingFields =
false
;
249
#endif
250
};
251
252
// clazy:excludeall=qstring-allocations
253
254
#endif
// QGSZONALSTATISTICS_H
Qgis::ZonalStatistic
ZonalStatistic
Statistics to be calculated during a zonal statistics operation.
Definition
qgis.h:6583
Qgis::ZonalStatistic::Default
@ Default
Default statistics.
Definition
qgis.h:6602
Qgis::ZonalStatistic::All
@ All
All statistics. For QGIS 3.x this includes ONLY numeric statistics, but for 4.0 this will be extended...
Definition
qgis.h:6599
Qgis::ZonalStatisticResult
ZonalStatisticResult
Zonal statistics result codes.
Definition
qgis.h:6620
Qgis::ZonalStatistics
QFlags< ZonalStatistic > ZonalStatistics
Statistics to be calculated during a zonal statistics operation.
Definition
qgis.h:6611
QgsCoordinateReferenceSystem
Represents a coordinate reference system (CRS).
Definition
qgscoordinatereferencesystem.h:216
QgsFeatureSink
An interface for objects which accept features via addFeature(s) methods.
Definition
qgsfeaturesink.h:33
QgsFeatureSource
An interface for objects which provide features via a getFeatures method.
Definition
qgsfeaturesource.h:37
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition
qgsfeedback.h:44
QgsField
Encapsulate a field in an attribute table or data source.
Definition
qgsfield.h:56
QgsGeometry
A geometry is the spatial representation of a feature.
Definition
qgsgeometry.h:267
QgsPointXY
Represents a 2D point.
Definition
qgspointxy.h:62
QgsRasterDataProvider
Base class for raster data providers.
Definition
qgsrasterdataprovider.h:94
QgsRasterInterface
Base class for processing filters like renderers, reprojector, resampler etc.
Definition
qgsrasterinterface.h:140
QgsRasterLayer
Represents a raster layer.
Definition
qgsrasterlayer.h:82
QgsRectangle
A rectangle specified with double values.
Definition
qgsrectangle.h:47
QgsVectorLayer
Represents a vector layer which manages a vector based dataset.
Definition
qgsvectorlayer.h:414
QgsZonalStatistics::calculateStatistics
Qgis::ZonalStatisticResult calculateStatistics(QgsFeedback *feedback)
Runs the calculation.
Definition
qgszonalstatistics.cpp:71
QgsZonalStatistics::displayName
static QString displayName(Qgis::ZonalStatistic statistic)
Returns the friendly display name for a statistic.
Definition
qgszonalstatistics.cpp:240
QgsZonalStatistics::QgsZonalStatistics
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.
Definition
qgszonalstatistics.cpp:35
QgsZonalStatistics::shortName
static QString shortName(Qgis::ZonalStatistic statistic)
Returns a short, friendly display name for a statistic, suitable for use in a field name.
Definition
qgszonalstatistics.cpp:279
SIP_PYNAME
#define SIP_PYNAME(name)
Definition
qgis_sip.h:88
qgscoordinatereferencesystem.h
qgsfeedback.h
qgsfields.h
QgsZonalStatistics::FeatureStats::CompareNaNAware
Definition
qgszonalstatistics.h:224
QgsZonalStatistics::FeatureStats::CompareNaNAware::operator()
bool operator()(double lhs, double rhs) const
Definition
qgszonalstatistics.h:225
Generated on
for QGIS API Documentation by
1.17.0