Skip to content
Permalink
Browse files

Minor refactoring, avoid use of old style cast

  • Loading branch information
nyalldawson committed Jun 8, 2018
1 parent ff9e7ba commit 839139619cf5ef8868c77bed0dd1a910871754c3
Showing with 16 additions and 27 deletions.
  1. +11 −23 src/analysis/vector/qgszonalstatistics.cpp
  2. +5 −4 src/analysis/vector/qgszonalstatistics.h
@@ -246,21 +246,7 @@ int QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback )
}

int offsetX, offsetY, nCellsX, nCellsY;
if ( cellInfoForBBox( rasterBBox, featureRect, cellsizeX, cellsizeY, offsetX, offsetY, nCellsX, nCellsY ) != 0 )
{
++featureCounter;
continue;
}

//avoid access to cells outside of the raster (may occur because of rounding)
if ( ( offsetX + nCellsX ) > nCellsXProvider )
{
nCellsX = nCellsXProvider - offsetX;
}
if ( ( offsetY + nCellsY ) > nCellsYProvider )
{
nCellsY = nCellsYProvider - offsetY;
}
cellInfoForBBox( rasterBBox, featureRect, cellsizeX, cellsizeY, offsetX, offsetY, nCellsX, nCellsY, nCellsXProvider, nCellsYProvider );

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

int QgsZonalStatistics::cellInfoForBBox( const QgsRectangle &rasterBBox, const QgsRectangle &featureBBox, double cellSizeX, double cellSizeY,
int &offsetX, int &offsetY, int &nCellsX, int &nCellsY ) const
void QgsZonalStatistics::cellInfoForBBox( const QgsRectangle &rasterBBox, const QgsRectangle &featureBBox, double cellSizeX, double cellSizeY,
int &offsetX, int &offsetY, int &nCellsX, int &nCellsY, int rasterWidth, int rasterHeight ) const
{
//get intersecting bbox
QgsRectangle intersectBox = rasterBBox.intersect( &featureBBox );
@@ -373,20 +359,22 @@ int QgsZonalStatistics::cellInfoForBBox( const QgsRectangle &rasterBBox, const Q
nCellsY = 0;
offsetX = 0;
offsetY = 0;
return 0;
return;
}

//get offset in pixels in x- and y- direction
offsetX = ( int )( ( intersectBox.xMinimum() - rasterBBox.xMinimum() ) / cellSizeX );
offsetY = ( int )( ( rasterBBox.yMaximum() - intersectBox.yMaximum() ) / cellSizeY );
offsetX = static_cast< int >( std::floor( ( intersectBox.xMinimum() - rasterBBox.xMinimum() ) / cellSizeX ) );
offsetY = static_cast< int >( std::floor( ( rasterBBox.yMaximum() - intersectBox.yMaximum() ) / cellSizeY ) );

int maxColumn = ( int )( ( intersectBox.xMaximum() - rasterBBox.xMinimum() ) / cellSizeX ) + 1;
int maxRow = ( int )( ( rasterBBox.yMaximum() - intersectBox.yMinimum() ) / cellSizeY ) + 1;
int maxColumn = static_cast< int >( std::floor( ( intersectBox.xMaximum() - rasterBBox.xMinimum() ) / cellSizeX ) ) + 1;
int maxRow = static_cast< int >( std::floor( ( rasterBBox.yMaximum() - intersectBox.yMinimum() ) / cellSizeY ) ) + 1;

nCellsX = maxColumn - offsetX;
nCellsY = maxRow - offsetY;

return 0;
//avoid access to cells outside of the raster (may occur because of rounding)
nCellsX = std::min( offsetX + nCellsX, rasterWidth ) - offsetX;
nCellsY = std::min( offsetY + nCellsY, rasterHeight ) - offsetY;
}

void QgsZonalStatistics::statisticsFromMiddlePointTest( const QgsGeometry &poly, int pixelOffsetX,
@@ -128,10 +128,11 @@ class ANALYSIS_EXPORT QgsZonalStatistics
};

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

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

0 comments on commit 8391396

Please sign in to comment.
You can’t perform that action at this time.