Skip to content

Commit 8391396

Browse files
committed
Minor refactoring, avoid use of old style cast
1 parent ff9e7ba commit 8391396

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

src/analysis/vector/qgszonalstatistics.cpp

+11-23
Original file line numberDiff line numberDiff line change
@@ -246,21 +246,7 @@ int QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback )
246246
}
247247

248248
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 );
264250

265251
statisticsFromMiddlePointTest( featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
266252
rasterBBox, featureStats );
@@ -362,8 +348,8 @@ int QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback )
362348
return 0;
363349
}
364350

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
367353
{
368354
//get intersecting bbox
369355
QgsRectangle intersectBox = rasterBBox.intersect( &featureBBox );
@@ -373,20 +359,22 @@ int QgsZonalStatistics::cellInfoForBBox( const QgsRectangle &rasterBBox, const Q
373359
nCellsY = 0;
374360
offsetX = 0;
375361
offsetY = 0;
376-
return 0;
362+
return;
377363
}
378364

379365
//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 ) );
382368

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;
385371

386372
nCellsX = maxColumn - offsetX;
387373
nCellsY = maxRow - offsetY;
388374

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;
390378
}
391379

392380
void QgsZonalStatistics::statisticsFromMiddlePointTest( const QgsGeometry &poly, int pixelOffsetX,

src/analysis/vector/qgszonalstatistics.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ class ANALYSIS_EXPORT QgsZonalStatistics
128128
};
129129

130130
/**
131-
* Analysis what cells need to be considered to cover the bounding box of a feature
132-
\returns 0 in case of success*/
133-
int cellInfoForBBox( const QgsRectangle &rasterBBox, const QgsRectangle &featureBBox, double cellSizeX, double cellSizeY,
134-
int &offsetX, int &offsetY, int &nCellsX, int &nCellsY ) const;
131+
* Analyzes which cells need to be considered to completely cover the bounding box of a feature.
132+
*/
133+
void cellInfoForBBox( const QgsRectangle &rasterBBox, const QgsRectangle &featureBBox, double cellSizeX, double cellSizeY,
134+
int &offsetX, int &offsetY, int &nCellsX, int &nCellsY,
135+
int rasterWidth, int rasterHeight ) const;
135136

136137
//! Returns statistics by considering the pixels where the center point is within the polygon (fast)
137138
void statisticsFromMiddlePointTest( const QgsGeometry &poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,

0 commit comments

Comments
 (0)