Skip to content

Commit

Permalink
Minor refactoring, avoid use of old style cast
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 8, 2018
1 parent ff9e7ba commit 8391396
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
34 changes: 11 additions & 23 deletions src/analysis/vector/qgszonalstatistics.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand All @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions src/analysis/vector/qgszonalstatistics.h
Expand Up @@ -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,
Expand Down

0 comments on commit 8391396

Please sign in to comment.