@@ -302,27 +302,24 @@ void QgsZonalStatistics::statisticsFromMiddlePointTest( void* band, QgsGeometry*
302
302
cellCenterX = rasterBBox.xMinimum () + pixelOffsetX * cellSizeX + cellSizeX / 2 ;
303
303
for ( int j = 0 ; j < nCellsX; ++j )
304
304
{
305
- GEOSGeom_destroy_r ( geosctxt, currentCellCenter );
306
- cellCenterCoords = GEOSCoordSeq_create_r ( geosctxt, 1 , 2 );
307
- GEOSCoordSeq_setX_r ( geosctxt, cellCenterCoords, 0 , cellCenterX );
308
- GEOSCoordSeq_setY_r ( geosctxt, cellCenterCoords, 0 , cellCenterY );
309
- currentCellCenter = GEOSGeom_createPoint_r ( geosctxt, cellCenterCoords );
310
-
311
- if ( scanLine[j] != mInputNodataValue ) // don't consider nodata values
305
+ if ( validPixel ( scanLine[j] ) )
312
306
{
307
+ GEOSGeom_destroy_r ( geosctxt, currentCellCenter );
308
+ cellCenterCoords = GEOSCoordSeq_create_r ( geosctxt, 1 , 2 );
309
+ GEOSCoordSeq_setX_r ( geosctxt, cellCenterCoords, 0 , cellCenterX );
310
+ GEOSCoordSeq_setY_r ( geosctxt, cellCenterCoords, 0 , cellCenterY );
311
+ currentCellCenter = GEOSGeom_createPoint_r ( geosctxt, cellCenterCoords );
313
312
if ( GEOSPreparedContains_r ( geosctxt, polyGeosPrepared, currentCellCenter ) )
314
313
{
315
- if ( !qIsNaN ( scanLine[j] ) )
316
- {
317
- sum += scanLine[j];
318
- }
314
+ sum += scanLine[j];
319
315
++count;
320
316
}
321
317
}
322
318
cellCenterX += cellSizeX;
323
319
}
324
320
cellCenterY -= cellSizeY;
325
321
}
322
+ GEOSGeom_destroy_r ( geosctxt, currentCellCenter );
326
323
CPLFree ( scanLine );
327
324
GEOSPreparedGeom_destroy_r ( geosctxt, polyGeosPrepared );
328
325
}
@@ -347,6 +344,9 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeome
347
344
for ( int col = 0 ; col < nCellsX; ++col )
348
345
{
349
346
GDALRasterIO ( band, GF_Read, pixelOffsetX + col, pixelOffsetY + row, nCellsX, 1 , pixelData, 1 , 1 , GDT_Float32, 0 , 0 );
347
+ if ( !validPixel ( *pixelData ) )
348
+ continue ;
349
+
350
350
pixelRectGeometry = QgsGeometry::fromRect ( QgsRectangle ( currentX - hCellSizeX, currentY - hCellSizeY, currentX + hCellSizeX, currentY + hCellSizeY ) );
351
351
if ( pixelRectGeometry )
352
352
{
@@ -373,6 +373,15 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeome
373
373
CPLFree ( pixelData );
374
374
}
375
375
376
+ bool QgsZonalStatistics::validPixel ( float value ) const
377
+ {
378
+ if ( value == mInputNodataValue || qIsNaN ( value ) )
379
+ {
380
+ return false ;
381
+ }
382
+ return true ;
383
+ }
384
+
376
385
QString QgsZonalStatistics::getUniqueFieldName ( QString fieldName )
377
386
{
378
387
QgsVectorDataProvider* dp = mPolygonLayer ->dataProvider ();
0 commit comments