Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3e5f9ef

Browse files
nyalldawsongithub-actions[bot]
authored andcommittedJun 10, 2021
Fix zonal statistics does not correctly handle coordinate transforms
Fixes #26858
1 parent 607b44b commit 3e5f9ef

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed
 
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "FeatureCollection",
3+
"name": "dem_zones",
4+
"features": [
5+
{ "type": "Feature", "properties": { "zone": "zone 1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 18.670940771920002, 45.789720648616203 ], [ 18.670601888719101, 45.7786128103632 ], [ 18.680241233101299, 45.778386888229299 ], [ 18.680580116302298, 45.790134839195098 ], [ 18.670940771920002, 45.789720648616203 ] ] ] } },
6+
{ "type": "Feature", "properties": { "zone": "zone 2" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 18.696733548880299, 45.804631509457501 ], [ 18.6881861614789, 45.8046691631464 ], [ 18.6969218173253, 45.787687349410596 ], [ 18.696733548880299, 45.804631509457501 ] ] ] } },
7+
{ "type": "Feature", "properties": { "zone": "zone 3" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 18.688731551630401, 45.791979281616896 ], [ 18.688842747680699, 45.791921624405603 ], [ 18.6888309809029, 45.791878087327703 ], [ 18.6887509668137, 45.791858672144301 ], [ 18.688731551630401, 45.791979281616896 ] ] ] } }
8+
]
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"type": "FeatureCollection",
3+
"name": "zonal_stats_crs",
4+
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
5+
"features": [
6+
{ "type": "Feature", "properties": { "zone": "zone 1", "_count": 11016.0, "_sum": 1717611.2636108398, "_mean": 155.91968623918299 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 18.670940771920002, 45.789720648616203 ], [ 18.670601888719101, 45.7786128103632 ], [ 18.680241233101299, 45.778386888229299 ], [ 18.680580116302298, 45.790134839195098 ], [ 18.670940771920002, 45.789720648616203 ] ] ] } },
7+
{ "type": "Feature", "properties": { "zone": "zone 2", "_count": 7239.0, "_sum": 1348285.545715332, "_mean": 186.25301087378534 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 18.696733548880299, 45.804631509457501 ], [ 18.6881861614789, 45.8046691631464 ], [ 18.6969218173253, 45.787687349410596 ], [ 18.696733548880299, 45.804631509457501 ] ] ] } },
8+
{ "type": "Feature", "properties": { "zone": "zone 3", "_count": 0.77731787596712487, "_sum": 128.34554148638338, "_mean": 165.11332809206039 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 18.688731551630401, 45.791979281616896 ], [ 18.688842747680699, 45.791921624405603 ], [ 18.6888309809029, 45.791878087327703 ], [ 18.6887509668137, 45.791858672144301 ], [ 18.688731551630401, 45.791979281616896 ] ] ] } }
9+
]
10+
}

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests3.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,34 @@ tests:
118118
geometry:
119119
precision: 5
120120

121+
- algorithm: native:zonalstatisticsfb
122+
name: Zonal stats (feature based, with transform)
123+
params:
124+
COLUMN_PREFIX: _
125+
INPUT:
126+
name: custom/dem_zones.geojson
127+
type: vector
128+
INPUT_RASTER:
129+
name: custom/dem_crs.tif
130+
type: raster
131+
RASTER_BAND: 1
132+
STATISTICS:
133+
- 0
134+
- 1
135+
- 2
136+
results:
137+
OUTPUT:
138+
name: expected/zonal_stats_crs.geojson
139+
type: vector
140+
compare:
141+
fields:
142+
_count:
143+
precision: 3
144+
_sum:
145+
precision: 3
146+
_mean:
147+
precision: 3
148+
121149
- algorithm: qgis:zonalhistogram
122150
name: Zonal histogram
123151
params:

‎src/analysis/processing/qgsalgorithmzonalstatisticsfeaturebased.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,28 @@ bool QgsZonalStatisticsFeatureBasedAlgorithm::prepareAlgorithm( const QVariantMa
158158

159159
QgsFeatureList QgsZonalStatisticsFeatureBasedAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
160160
{
161-
Q_UNUSED( context )
161+
if ( !mCreatedTransform )
162+
{
163+
mCreatedTransform = true;
164+
mFeatureToRasterTransform = QgsCoordinateTransform( sourceCrs(), mCrs, context.transformContext() );
165+
}
166+
162167
Q_UNUSED( feedback )
163168
QgsAttributes attributes = feature.attributes();
164169
attributes.resize( mOutputFields.size() );
165170

166-
QMap<QgsZonalStatistics::Statistic, QVariant> results = QgsZonalStatistics::calculateStatistics( mRaster.get(), feature.geometry(), mPixelSizeX, mPixelSizeY, mBand, mStats );
171+
QgsGeometry geometry = feature.geometry();
172+
try
173+
{
174+
geometry.transform( mFeatureToRasterTransform );
175+
}
176+
catch ( QgsCsException & )
177+
{
178+
if ( feedback )
179+
feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
180+
}
181+
182+
QMap<QgsZonalStatistics::Statistic, QVariant> results = QgsZonalStatistics::calculateStatistics( mRaster.get(), geometry, mPixelSizeX, mPixelSizeY, mBand, mStats );
167183
for ( auto result = results.constBegin(); result != results.constEnd(); ++result )
168184
{
169185
attributes.replace( mStatFieldsMapping.value( result.key() ), result.value() );

‎src/analysis/processing/qgsalgorithmzonalstatisticsfeaturebased.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class QgsZonalStatisticsFeatureBasedAlgorithm : public QgsProcessingFeatureBased
6262
QString mPrefix;
6363
QgsZonalStatistics::Statistics mStats = QgsZonalStatistics::All;
6464
QgsCoordinateReferenceSystem mCrs;
65+
bool mCreatedTransform = false;
66+
QgsCoordinateTransform mFeatureToRasterTransform;
6567
double mPixelSizeX;
6668
double mPixelSizeY;
6769
QgsFields mOutputFields;

0 commit comments

Comments
 (0)
Please sign in to comment.