Skip to content
Permalink
Browse files

Use doubles, not float values, within zonal stats

  • Loading branch information
nyalldawson committed Jun 8, 2018
1 parent 919f558 commit 1631a8269a679aea0fcc62e09d98620975a4c309
Showing with 28 additions and 17 deletions.
  1. +8 −6 src/analysis/vector/qgszonalstatistics.cpp
  2. +20 −11 src/analysis/vector/qgszonalstatistics.h
@@ -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 ) );
}
}
@@ -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;
@@ -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;
}
@@ -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();
@@ -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 )
{
@@ -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;
};

/**

0 comments on commit 1631a82

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