Skip to content

Commit 9ad9f8c

Browse files
committed
fixed #6496 - wrong GDAL exact stats
1 parent f0aca15 commit 9ad9f8c

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/providers/gdal/qgsgdalprovider.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -2032,7 +2032,15 @@ bool QgsGdalProvider::hasStatistics( int theBandNo,
20322032
if ( !( theStats & QgsRasterBandStats::StdDev ) ) pdfStdDev = NULL;
20332033

20342034
// try to fetch the cached stats (bForce=FALSE)
2035-
CPLErr myerval = GDALGetRasterStatistics( myGdalBand, bApproxOK, false, pdfMin, pdfMax, pdfMean, pdfStdDev );
2035+
// Unfortunately GDALGetRasterStatistics() does not work as expected acording to
2036+
// API doc, if bApproxOK=false and bForce=false/true and exact statistics
2037+
// (from all raster pixels) are not available/cached, it should return CE_Warning.
2038+
// Instead, it is giving estimated (from sample) cached statistics and it returns CE_None.
2039+
// see above and https://trac.osgeo.org/gdal/ticket/4857
2040+
// -> Cannot used cached GDAL stats for exact
2041+
if ( !bApproxOK ) return false;
2042+
2043+
CPLErr myerval = GDALGetRasterStatistics( myGdalBand, bApproxOK, true, pdfMin, pdfMax, pdfMean, pdfStdDev );
20362044

20372045
if ( CE_None == myerval ) // CE_Warning if cached not found
20382046
{
@@ -2106,12 +2114,17 @@ QgsRasterBandStats QgsGdalProvider::bandStatistics( int theBandNo, int theStats,
21062114
myProg.provider = this;
21072115

21082116
// try to fetch the cached stats (bForce=FALSE)
2117+
// GDALGetRasterStatistics() do not work correctly with bApproxOK=false and bForce=false/true
2118+
// see above and https://trac.osgeo.org/gdal/ticket/4857
2119+
// -> Cannot used cached GDAL stats for exact
2120+
21092121
CPLErr myerval =
2110-
GDALGetRasterStatistics( myGdalBand, bApproxOK, false, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev );
2122+
GDALGetRasterStatistics( myGdalBand, bApproxOK, true, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev );
21112123

21122124
QgsDebugMsg( QString( "myerval = %1" ).arg( myerval ) );
2125+
21132126
// if cached stats are not found, compute them
2114-
if ( CE_None != myerval )
2127+
if ( !bApproxOK || CE_None != myerval )
21152128
{
21162129
QgsDebugMsg( "Calculating statistics by GDAL" );
21172130
myerval = GDALComputeRasterStatistics( myGdalBand, bApproxOK,

0 commit comments

Comments
 (0)