@@ -246,21 +246,7 @@ int QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback )
246
246
}
247
247
248
248
int offsetX, offsetY, nCellsX, nCellsY;
249
- if ( cellInfoForBBox ( rasterBBox, featureRect, cellsizeX, cellsizeY, offsetX, offsetY, nCellsX, nCellsY ) != 0 )
250
- {
251
- ++featureCounter;
252
- continue ;
253
- }
254
-
255
- // avoid access to cells outside of the raster (may occur because of rounding)
256
- if ( ( offsetX + nCellsX ) > nCellsXProvider )
257
- {
258
- nCellsX = nCellsXProvider - offsetX;
259
- }
260
- if ( ( offsetY + nCellsY ) > nCellsYProvider )
261
- {
262
- nCellsY = nCellsYProvider - offsetY;
263
- }
249
+ cellInfoForBBox ( rasterBBox, featureRect, cellsizeX, cellsizeY, offsetX, offsetY, nCellsX, nCellsY, nCellsXProvider, nCellsYProvider );
264
250
265
251
statisticsFromMiddlePointTest ( featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
266
252
rasterBBox, featureStats );
@@ -362,8 +348,8 @@ int QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback )
362
348
return 0 ;
363
349
}
364
350
365
- int QgsZonalStatistics::cellInfoForBBox ( const QgsRectangle &rasterBBox, const QgsRectangle &featureBBox, double cellSizeX, double cellSizeY,
366
- int &offsetX, int &offsetY, int &nCellsX, int &nCellsY ) const
351
+ void QgsZonalStatistics::cellInfoForBBox ( const QgsRectangle &rasterBBox, const QgsRectangle &featureBBox, double cellSizeX, double cellSizeY,
352
+ int &offsetX, int &offsetY, int &nCellsX, int &nCellsY, int rasterWidth, int rasterHeight ) const
367
353
{
368
354
// get intersecting bbox
369
355
QgsRectangle intersectBox = rasterBBox.intersect ( &featureBBox );
@@ -373,20 +359,22 @@ int QgsZonalStatistics::cellInfoForBBox( const QgsRectangle &rasterBBox, const Q
373
359
nCellsY = 0 ;
374
360
offsetX = 0 ;
375
361
offsetY = 0 ;
376
- return 0 ;
362
+ return ;
377
363
}
378
364
379
365
// get offset in pixels in x- and y- direction
380
- offsetX = ( int )( ( intersectBox.xMinimum () - rasterBBox.xMinimum () ) / cellSizeX );
381
- offsetY = ( int )( ( rasterBBox.yMaximum () - intersectBox.yMaximum () ) / cellSizeY );
366
+ offsetX = static_cast < int >( std::floor ( ( intersectBox.xMinimum () - rasterBBox.xMinimum () ) / cellSizeX ) );
367
+ offsetY = static_cast < int >( std::floor ( ( rasterBBox.yMaximum () - intersectBox.yMaximum () ) / cellSizeY ) );
382
368
383
- int maxColumn = ( int )( ( intersectBox.xMaximum () - rasterBBox.xMinimum () ) / cellSizeX ) + 1 ;
384
- int maxRow = ( int )( ( rasterBBox.yMaximum () - intersectBox.yMinimum () ) / cellSizeY ) + 1 ;
369
+ int maxColumn = static_cast < int >( std::floor ( ( intersectBox.xMaximum () - rasterBBox.xMinimum () ) / cellSizeX ) ) + 1 ;
370
+ int maxRow = static_cast < int >( std::floor ( ( rasterBBox.yMaximum () - intersectBox.yMinimum () ) / cellSizeY ) ) + 1 ;
385
371
386
372
nCellsX = maxColumn - offsetX;
387
373
nCellsY = maxRow - offsetY;
388
374
389
- return 0 ;
375
+ // avoid access to cells outside of the raster (may occur because of rounding)
376
+ nCellsX = std::min ( offsetX + nCellsX, rasterWidth ) - offsetX;
377
+ nCellsY = std::min ( offsetY + nCellsY, rasterHeight ) - offsetY;
390
378
}
391
379
392
380
void QgsZonalStatistics::statisticsFromMiddlePointTest ( const QgsGeometry &poly, int pixelOffsetX,
0 commit comments