23
23
#include " cpl_string.h"
24
24
#include < QProgressDialog>
25
25
26
- QgsZonalStatistics::QgsZonalStatistics ( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand ) :
27
- mRasterFilePath( rasterFile ), mRasterBand( rasterBand ), mPolygonLayer( polygonLayer ), mAttributePrefix( attributePrefix ), mInputNodataValue( -1 )
26
+ QgsZonalStatistics::QgsZonalStatistics ( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand )
27
+ : mRasterFilePath( rasterFile )
28
+ , mRasterBand( rasterBand )
29
+ , mPolygonLayer( polygonLayer )
30
+ , mAttributePrefix( attributePrefix )
31
+ , mInputNodataValue( -1 )
28
32
{
29
33
30
34
}
31
35
32
- QgsZonalStatistics::QgsZonalStatistics (): mRasterBand( 0 ), mPolygonLayer( 0 )
36
+ QgsZonalStatistics::QgsZonalStatistics ()
37
+ : mRasterBand( 0 )
38
+ , mPolygonLayer( 0 )
33
39
{
34
40
35
41
}
@@ -162,13 +168,13 @@ int QgsZonalStatistics::calculateStatistics( QProgressDialog* p )
162
168
continue ;
163
169
}
164
170
165
- statisticsFromMiddlePointTest_improved ( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY, \
171
+ statisticsFromMiddlePointTest_improved ( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
166
172
rasterBBox, sum, count );
167
173
168
174
if ( count <= 1 )
169
175
{
170
176
// the cell resolution is probably larger than the polygon area. We switch to precise pixel - polygon intersection in this case
171
- statisticsFromPreciseIntersection ( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY, \
177
+ statisticsFromPreciseIntersection ( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
172
178
rasterBBox, sum, count );
173
179
}
174
180
@@ -227,7 +233,7 @@ int QgsZonalStatistics::cellInfoForBBox( const QgsRectangle& rasterBBox, const Q
227
233
return 0 ;
228
234
}
229
235
230
- void QgsZonalStatistics::statisticsFromMiddlePointTest ( void * band, QgsGeometry* poly, int pixelOffsetX, \
236
+ void QgsZonalStatistics::statisticsFromMiddlePointTest ( void * band, QgsGeometry* poly, int pixelOffsetX,
231
237
int pixelOffsetY, int nCellsX, int nCellsY, double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double & sum, double & count )
232
238
{
233
239
double cellCenterX, cellCenterY;
@@ -260,20 +266,18 @@ void QgsZonalStatistics::statisticsFromMiddlePointTest( void* band, QgsGeometry*
260
266
CPLFree ( scanLine );
261
267
}
262
268
263
- void QgsZonalStatistics::statisticsFromPreciseIntersection ( void * band, QgsGeometry* poly, int pixelOffsetX, \
269
+ void QgsZonalStatistics::statisticsFromPreciseIntersection ( void * band, QgsGeometry* poly, int pixelOffsetX,
264
270
int pixelOffsetY, int nCellsX, int nCellsY, double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double & sum, double & count )
265
271
{
266
272
sum = 0 ;
267
273
count = 0 ;
268
274
double currentY = rasterBBox.yMaximum () - pixelOffsetY * cellSizeY - cellSizeY / 2 ;
269
275
float * pixelData = ( float * ) CPLMalloc ( sizeof ( float ) );
270
276
QgsGeometry* pixelRectGeometry = 0 ;
271
- QgsGeometry* intersectGeometry = 0 ;
272
277
273
278
double hCellSizeX = cellSizeX / 2.0 ;
274
279
double hCellSizeY = cellSizeY / 2.0 ;
275
280
double pixelArea = cellSizeX * cellSizeY;
276
- double intersectionArea = 0 ;
277
281
double weight = 0 ;
278
282
279
283
for ( int row = 0 ; row < nCellsY; ++row )
@@ -286,10 +290,11 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeome
286
290
if ( pixelRectGeometry )
287
291
{
288
292
// intersection
289
- intersectGeometry = pixelRectGeometry->intersection ( poly );
293
+ QgsGeometry * intersectGeometry = pixelRectGeometry->intersection ( poly );
290
294
if ( intersectGeometry )
291
295
{
292
- if ( GEOSArea ( intersectGeometry->asGeos (), &intersectionArea ) )
296
+ double intersectionArea = intersectGeometry->area ();
297
+ if ( intersectionArea >= 0.0 )
293
298
{
294
299
weight = intersectionArea / pixelArea;
295
300
count += weight;
@@ -305,7 +310,7 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeome
305
310
CPLFree ( pixelData );
306
311
}
307
312
308
- void QgsZonalStatistics::statisticsFromMiddlePointTest_improved ( void * band, QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY, \
313
+ void QgsZonalStatistics::statisticsFromMiddlePointTest_improved ( void * band, QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
309
314
double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double & sum, double & count )
310
315
{
311
316
double cellCenterX, cellCenterY;
0 commit comments