Skip to content

Commit

Permalink
Use doubles, not float values, within zonal stats
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 8, 2018
1 parent 919f558 commit 1631a82
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
14 changes: 8 additions & 6 deletions src/analysis/vector/qgszonalstatistics.cpp
Expand Up @@ -328,12 +328,12 @@ int QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback )
std::sort( vals.begin(), vals.end() );
if ( mStatistics & QgsZonalStatistics::Minority )
{
float minorityKey = featureStats.valueCount.key( vals.first() );
double minorityKey = featureStats.valueCount.key( vals.first() );
changeAttributeMap.insert( minorityIndex, QVariant( minorityKey ) );
}
if ( mStatistics & QgsZonalStatistics::Majority )
{
float majKey = featureStats.valueCount.key( vals.last() );
double majKey = featureStats.valueCount.key( vals.last() );
changeAttributeMap.insert( majorityIndex, QVariant( majKey ) );
}
}
Expand Down Expand Up @@ -422,15 +422,16 @@ void QgsZonalStatistics::statisticsFromMiddlePointTest( const QgsGeometry &poly,
cellCenterX = rasterBBox.xMinimum() + pixelOffsetX * cellSizeX + cellSizeX / 2;
for ( int j = 0; j < nCellsX; ++j )
{
if ( validPixel( block->value( i, j ) ) )
double pixelValue = block->value( i, j );
if ( validPixel( pixelValue ) )
{
cellCenterCoords = GEOSCoordSeq_create_r( geosctxt, 1, 2 );
GEOSCoordSeq_setX_r( geosctxt, cellCenterCoords, 0, cellCenterX );
GEOSCoordSeq_setY_r( geosctxt, cellCenterCoords, 0, cellCenterY );
currentCellCenter.reset( GEOSGeom_createPoint_r( geosctxt, cellCenterCoords ) );
if ( GEOSPreparedContains_r( geosctxt, polyGeosPrepared.get(), currentCellCenter.get() ) )
{
stats.addValue( block->value( i, j ) );
stats.addValue( pixelValue );
}
}
cellCenterX += cellSizeX;
Expand Down Expand Up @@ -461,7 +462,8 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( const QgsGeometry &p
double currentX = rasterBBox.xMinimum() + cellSizeX / 2.0 + pixelOffsetX * cellSizeX;
for ( int j = 0; j < nCellsX; ++j )
{
if ( !validPixel( block->value( i, j ) ) )
double pixelValue = block->value( i, j );
if ( !validPixel( pixelValue ) )
{
continue;
}
Expand All @@ -477,7 +479,7 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( const QgsGeometry &p
if ( intersectionArea >= 0.0 )
{
weight = intersectionArea / pixelArea;
stats.addValue( block->value( i, j ), weight );
stats.addValue( pixelValue, weight );
}
}
pixelRectGeometry = QgsGeometry();
Expand Down
31 changes: 20 additions & 11 deletions src/analysis/vector/qgszonalstatistics.h
Expand Up @@ -84,10 +84,19 @@ class ANALYSIS_EXPORT QgsZonalStatistics
: mStoreValues( storeValues )
, mStoreValueCounts( storeValueCounts )
{
reset();
}
void reset() { sum = 0; count = 0; max = -FLT_MAX; min = FLT_MAX; valueCount.clear(); values.clear(); }
void addValue( float value, double weight = 1.0 )

void reset()
{
sum = 0;
count = 0;
max = std::numeric_limits<double>::lowest();
min = std::numeric_limits<double>::max();
valueCount.clear();
values.clear();
}

void addValue( double value, double weight = 1.0 )
{
if ( weight < 1.0 )
{
Expand All @@ -106,16 +115,16 @@ class ANALYSIS_EXPORT QgsZonalStatistics
if ( mStoreValues )
values.append( value );
}
double sum;
double count;
float max;
float min;
QMap< float, int > valueCount;
QList< float > values;
double sum = 0.0;
double count = 0.0;
double max = std::numeric_limits<double>::lowest();
double min = std::numeric_limits<double>::max();
QMap< double, int > valueCount;
QList< double > values;

private:
bool mStoreValues;
bool mStoreValueCounts;
bool mStoreValues = false;
bool mStoreValueCounts = false;
};

/**
Expand Down

0 comments on commit 1631a82

Please sign in to comment.