@@ -2032,7 +2032,15 @@ bool QgsGdalProvider::hasStatistics( int theBandNo,
2032
2032
if ( !( theStats & QgsRasterBandStats::StdDev ) ) pdfStdDev = NULL ;
2033
2033
2034
2034
// 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 );
2036
2044
2037
2045
if ( CE_None == myerval ) // CE_Warning if cached not found
2038
2046
{
@@ -2106,12 +2114,17 @@ QgsRasterBandStats QgsGdalProvider::bandStatistics( int theBandNo, int theStats,
2106
2114
myProg.provider = this ;
2107
2115
2108
2116
// 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
+
2109
2121
CPLErr myerval =
2110
- GDALGetRasterStatistics ( myGdalBand, bApproxOK, false , &pdfMin, &pdfMax, &pdfMean, &pdfStdDev );
2122
+ GDALGetRasterStatistics ( myGdalBand, bApproxOK, true , &pdfMin, &pdfMax, &pdfMean, &pdfStdDev );
2111
2123
2112
2124
QgsDebugMsg ( QString ( " myerval = %1" ).arg ( myerval ) );
2125
+
2113
2126
// if cached stats are not found, compute them
2114
- if ( CE_None != myerval )
2127
+ if ( !bApproxOK || CE_None != myerval )
2115
2128
{
2116
2129
QgsDebugMsg ( " Calculating statistics by GDAL" );
2117
2130
myerval = GDALComputeRasterStatistics ( myGdalBand, bApproxOK,
0 commit comments