Skip to content

Commit 1631a82

Browse files
committed
Use doubles, not float values, within zonal stats
1 parent 919f558 commit 1631a82

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/analysis/vector/qgszonalstatistics.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,12 @@ int QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback )
328328
std::sort( vals.begin(), vals.end() );
329329
if ( mStatistics & QgsZonalStatistics::Minority )
330330
{
331-
float minorityKey = featureStats.valueCount.key( vals.first() );
331+
double minorityKey = featureStats.valueCount.key( vals.first() );
332332
changeAttributeMap.insert( minorityIndex, QVariant( minorityKey ) );
333333
}
334334
if ( mStatistics & QgsZonalStatistics::Majority )
335335
{
336-
float majKey = featureStats.valueCount.key( vals.last() );
336+
double majKey = featureStats.valueCount.key( vals.last() );
337337
changeAttributeMap.insert( majorityIndex, QVariant( majKey ) );
338338
}
339339
}
@@ -422,15 +422,16 @@ void QgsZonalStatistics::statisticsFromMiddlePointTest( const QgsGeometry &poly,
422422
cellCenterX = rasterBBox.xMinimum() + pixelOffsetX * cellSizeX + cellSizeX / 2;
423423
for ( int j = 0; j < nCellsX; ++j )
424424
{
425-
if ( validPixel( block->value( i, j ) ) )
425+
double pixelValue = block->value( i, j );
426+
if ( validPixel( pixelValue ) )
426427
{
427428
cellCenterCoords = GEOSCoordSeq_create_r( geosctxt, 1, 2 );
428429
GEOSCoordSeq_setX_r( geosctxt, cellCenterCoords, 0, cellCenterX );
429430
GEOSCoordSeq_setY_r( geosctxt, cellCenterCoords, 0, cellCenterY );
430431
currentCellCenter.reset( GEOSGeom_createPoint_r( geosctxt, cellCenterCoords ) );
431432
if ( GEOSPreparedContains_r( geosctxt, polyGeosPrepared.get(), currentCellCenter.get() ) )
432433
{
433-
stats.addValue( block->value( i, j ) );
434+
stats.addValue( pixelValue );
434435
}
435436
}
436437
cellCenterX += cellSizeX;
@@ -461,7 +462,8 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( const QgsGeometry &p
461462
double currentX = rasterBBox.xMinimum() + cellSizeX / 2.0 + pixelOffsetX * cellSizeX;
462463
for ( int j = 0; j < nCellsX; ++j )
463464
{
464-
if ( !validPixel( block->value( i, j ) ) )
465+
double pixelValue = block->value( i, j );
466+
if ( !validPixel( pixelValue ) )
465467
{
466468
continue;
467469
}
@@ -477,7 +479,7 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( const QgsGeometry &p
477479
if ( intersectionArea >= 0.0 )
478480
{
479481
weight = intersectionArea / pixelArea;
480-
stats.addValue( block->value( i, j ), weight );
482+
stats.addValue( pixelValue, weight );
481483
}
482484
}
483485
pixelRectGeometry = QgsGeometry();

src/analysis/vector/qgszonalstatistics.h

+20-11
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,19 @@ class ANALYSIS_EXPORT QgsZonalStatistics
8484
: mStoreValues( storeValues )
8585
, mStoreValueCounts( storeValueCounts )
8686
{
87-
reset();
8887
}
89-
void reset() { sum = 0; count = 0; max = -FLT_MAX; min = FLT_MAX; valueCount.clear(); values.clear(); }
90-
void addValue( float value, double weight = 1.0 )
88+
89+
void reset()
90+
{
91+
sum = 0;
92+
count = 0;
93+
max = std::numeric_limits<double>::lowest();
94+
min = std::numeric_limits<double>::max();
95+
valueCount.clear();
96+
values.clear();
97+
}
98+
99+
void addValue( double value, double weight = 1.0 )
91100
{
92101
if ( weight < 1.0 )
93102
{
@@ -106,16 +115,16 @@ class ANALYSIS_EXPORT QgsZonalStatistics
106115
if ( mStoreValues )
107116
values.append( value );
108117
}
109-
double sum;
110-
double count;
111-
float max;
112-
float min;
113-
QMap< float, int > valueCount;
114-
QList< float > values;
118+
double sum = 0.0;
119+
double count = 0.0;
120+
double max = std::numeric_limits<double>::lowest();
121+
double min = std::numeric_limits<double>::max();
122+
QMap< double, int > valueCount;
123+
QList< double > values;
115124

116125
private:
117-
bool mStoreValues;
118-
bool mStoreValueCounts;
126+
bool mStoreValues = false;
127+
bool mStoreValueCounts = false;
119128
};
120129

121130
/**

0 commit comments

Comments
 (0)