2323#include " cpl_string.h"
2424#include < QProgressDialog>
2525
26- QgsZonalStatistics::QgsZonalStatistics ( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand ) :
27- mRasterFilePath( rasterFile ), mRasterBand( rasterBand ), mPolygonLayer( polygonLayer ), mAttributePrefix( attributePrefix ), mInputNodataValue( -1 )
26+ QgsZonalStatistics::QgsZonalStatistics ( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand )
27+ : mRasterFilePath( rasterFile )
28+ , mRasterBand( rasterBand )
29+ , mPolygonLayer( polygonLayer )
30+ , mAttributePrefix( attributePrefix )
31+ , mInputNodataValue( -1 )
2832{
2933
3034}
3135
32- QgsZonalStatistics::QgsZonalStatistics (): mRasterBand( 0 ), mPolygonLayer( 0 )
36+ QgsZonalStatistics::QgsZonalStatistics ()
37+ : mRasterBand( 0 )
38+ , mPolygonLayer( 0 )
3339{
3440
3541}
@@ -162,13 +168,13 @@ int QgsZonalStatistics::calculateStatistics( QProgressDialog* p )
162168 continue ;
163169 }
164170
165- statisticsFromMiddlePointTest_improved ( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY, \
171+ statisticsFromMiddlePointTest_improved ( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
166172 rasterBBox, sum, count );
167173
168174 if ( count <= 1 )
169175 {
170176 // the cell resolution is probably larger than the polygon area. We switch to precise pixel - polygon intersection in this case
171- statisticsFromPreciseIntersection ( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY, \
177+ statisticsFromPreciseIntersection ( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
172178 rasterBBox, sum, count );
173179 }
174180
@@ -227,7 +233,7 @@ int QgsZonalStatistics::cellInfoForBBox( const QgsRectangle& rasterBBox, const Q
227233 return 0 ;
228234}
229235
230- void QgsZonalStatistics::statisticsFromMiddlePointTest ( void * band, QgsGeometry* poly, int pixelOffsetX, \
236+ void QgsZonalStatistics::statisticsFromMiddlePointTest ( void * band, QgsGeometry* poly, int pixelOffsetX,
231237 int pixelOffsetY, int nCellsX, int nCellsY, double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double & sum, double & count )
232238{
233239 double cellCenterX, cellCenterY;
@@ -260,20 +266,18 @@ void QgsZonalStatistics::statisticsFromMiddlePointTest( void* band, QgsGeometry*
260266 CPLFree ( scanLine );
261267}
262268
263- void QgsZonalStatistics::statisticsFromPreciseIntersection ( void * band, QgsGeometry* poly, int pixelOffsetX, \
269+ void QgsZonalStatistics::statisticsFromPreciseIntersection ( void * band, QgsGeometry* poly, int pixelOffsetX,
264270 int pixelOffsetY, int nCellsX, int nCellsY, double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double & sum, double & count )
265271{
266272 sum = 0 ;
267273 count = 0 ;
268274 double currentY = rasterBBox.yMaximum () - pixelOffsetY * cellSizeY - cellSizeY / 2 ;
269275 float * pixelData = ( float * ) CPLMalloc ( sizeof ( float ) );
270276 QgsGeometry* pixelRectGeometry = 0 ;
271- QgsGeometry* intersectGeometry = 0 ;
272277
273278 double hCellSizeX = cellSizeX / 2.0 ;
274279 double hCellSizeY = cellSizeY / 2.0 ;
275280 double pixelArea = cellSizeX * cellSizeY;
276- double intersectionArea = 0 ;
277281 double weight = 0 ;
278282
279283 for ( int row = 0 ; row < nCellsY; ++row )
@@ -286,10 +290,11 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeome
286290 if ( pixelRectGeometry )
287291 {
288292 // intersection
289- intersectGeometry = pixelRectGeometry->intersection ( poly );
293+ QgsGeometry * intersectGeometry = pixelRectGeometry->intersection ( poly );
290294 if ( intersectGeometry )
291295 {
292- if ( GEOSArea ( intersectGeometry->asGeos (), &intersectionArea ) )
296+ double intersectionArea = intersectGeometry->area ();
297+ if ( intersectionArea >= 0.0 )
293298 {
294299 weight = intersectionArea / pixelArea;
295300 count += weight;
@@ -305,7 +310,7 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeome
305310 CPLFree ( pixelData );
306311}
307312
308- void QgsZonalStatistics::statisticsFromMiddlePointTest_improved ( void * band, QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY, \
313+ void QgsZonalStatistics::statisticsFromMiddlePointTest_improved ( void * band, QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
309314 double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double & sum, double & count )
310315{
311316 double cellCenterX, cellCenterY;
0 commit comments