Skip to content

Commit 4b4820e

Browse files
committed
[FEATURE]. Retroactive added feature tag to this commit to mark the fact that we now use native provider stats gathering. Updated min max calculation of gdal provider with more efficient implementation. Use updated stats gathering routine kindly provided by Etienne Sky.
1 parent 6a1073f commit 4b4820e

File tree

1 file changed

+59
-61
lines changed

1 file changed

+59
-61
lines changed

src/providers/gdal/qgsgdalprovider.cpp

+59-61
Original file line numberDiff line numberDiff line change
@@ -883,24 +883,18 @@ void QgsGdalProvider::computeMinMax( int theBandNo )
883883
{
884884
return;
885885
}
886-
int bApproxOK=false;
887-
double pdfMin;
888-
double pdfMax;
889-
double pdfMean;
890-
double pdfStdDev;
891886
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, theBandNo );
892-
double myerval = GDALGetRasterStatistics (
893-
myGdalBand,
894-
bApproxOK,
895-
TRUE,
896-
&pdfMin,
897-
&pdfMax,
898-
&pdfMean,
899-
&pdfStdDev
900-
);
901-
Q_UNUSED(myerval);
902-
mMinimum[theBandNo-1] = pdfMin;
903-
mMaximum[theBandNo-1] = pdfMax;
887+
int bApproxOK=false;
888+
int bGotMin, bGotMax;
889+
double adfMinMax[2];
890+
adfMinMax[0] = GDALGetRasterMinimum( myGdalBand, &bGotMin );
891+
adfMinMax[1] = GDALGetRasterMaximum( myGdalBand, &bGotMax );
892+
if( ! ( bGotMin && bGotMax ) )
893+
{
894+
GDALComputeRasterMinMax( myGdalBand, TRUE, adfMinMax );
895+
}
896+
mMinimum[theBandNo-1] = adfMinMax[0];
897+
mMaximum[theBandNo-1] = adfMinMax[1];
904898
}
905899

906900
double QgsGdalProvider::minimumValue( int theBandNo ) const
@@ -1853,59 +1847,63 @@ QgsRasterBandStats QgsGdalProvider::bandStatistics( int theBandNo )
18531847
myProg.type = ProgressHistogram;
18541848
myProg.provider = this;
18551849

1856-
// Suggested by Etienne Sky to use getrasterstatistics instead of compute
1857-
// since computerasterstatistics forces collection each time
1858-
// where as getrasterstatistics uses aux.xml cached copy if available
1859-
// Note: there is currently no progress callback in this method
1860-
double myerval = GDALGetRasterStatistics (
1861-
myGdalBand,
1862-
bApproxOK,
1863-
TRUE,
1864-
&pdfMin,
1865-
&pdfMax,
1866-
&pdfMean,
1867-
&pdfStdDev
1868-
);
1869-
//double myerval = GDALComputeRasterStatistics ( myGdalBand,
1870-
// bApproxOK,
1871-
// &pdfMin,
1872-
// &pdfMax,
1873-
// &pdfMean,
1874-
// &pdfStdDev,
1875-
// progressCallback,
1876-
// &myProg
1877-
//) ;
1878-
//end of first pass through data now calculate the range
1879-
myRasterBandStats.bandName = generateBandName( theBandNo );
1880-
myRasterBandStats.bandNumber = theBandNo;
1881-
myRasterBandStats.range = pdfMax - pdfMin;
1882-
myRasterBandStats.minimumValue = pdfMin;
1883-
myRasterBandStats.maximumValue = pdfMax;
1884-
//calculate the mean
1885-
myRasterBandStats.mean = pdfMean;
1886-
myRasterBandStats.sum = 0; //not available via gdal
1887-
myRasterBandStats.elementCount = mWidth * mHeight;
1888-
myRasterBandStats.sumOfSquares = 0; //not available via gdal
1889-
myRasterBandStats.stdDev = pdfStdDev;
1890-
myRasterBandStats.statsGathered = true;
1850+
// double myerval =
1851+
// GDALComputeRasterStatistics (
1852+
// myGdalBand, bApproxOK, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev,
1853+
// progressCallback, &myProg ) ;
1854+
// double myerval =
1855+
// GDALGetRasterStatistics ( myGdalBand, bApproxOK, TRUE, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev);
1856+
// double myerval =
1857+
// GDALGetRasterStatisticsProgress ( myGdalBand, bApproxOK, TRUE, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev,
1858+
// progressCallback, &myProg );
1859+
1860+
// try to fetch the cached stats (bForce=FALSE)
1861+
CPLErr myerval =
1862+
GDALGetRasterStatistics ( myGdalBand, bApproxOK, FALSE, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev);
1863+
1864+
// if cached stats are not found, compute them
1865+
if ( CE_Warning == myerval )
1866+
{
1867+
myerval = GDALComputeRasterStatistics ( myGdalBand, bApproxOK,
1868+
&pdfMin, &pdfMax, &pdfMean, &pdfStdDev,
1869+
progressCallback, &myProg ) ;
1870+
}
1871+
1872+
// if stats are found populate the QgsRasterBandStats object
1873+
if ( CE_None == myerval )
1874+
{
1875+
1876+
myRasterBandStats.bandName = generateBandName( theBandNo );
1877+
myRasterBandStats.bandNumber = theBandNo;
1878+
myRasterBandStats.range = pdfMax - pdfMin;
1879+
myRasterBandStats.minimumValue = pdfMin;
1880+
myRasterBandStats.maximumValue = pdfMax;
1881+
//calculate the mean
1882+
myRasterBandStats.mean = pdfMean;
1883+
myRasterBandStats.sum = 0; //not available via gdal
1884+
myRasterBandStats.elementCount = mWidth * mHeight;
1885+
myRasterBandStats.sumOfSquares = 0; //not available via gdal
1886+
myRasterBandStats.stdDev = pdfStdDev;
1887+
myRasterBandStats.statsGathered = true;
18911888

18921889
#ifdef QGISDEBUG
1893-
QgsLogger::debug( "************ STATS **************", 1, __FILE__, __FUNCTION__, __LINE__ );
1894-
QgsLogger::debug( "VALID NODATA", mValidNoDataValue, 1, __FILE__, __FUNCTION__, __LINE__ );
1895-
QgsLogger::debug( "MIN", myRasterBandStats.minimumValue, 1, __FILE__, __FUNCTION__, __LINE__ );
1896-
QgsLogger::debug( "MAX", myRasterBandStats.maximumValue, 1, __FILE__, __FUNCTION__, __LINE__ );
1897-
QgsLogger::debug( "RANGE", myRasterBandStats.range, 1, __FILE__, __FUNCTION__, __LINE__ );
1898-
QgsLogger::debug( "MEAN", myRasterBandStats.mean, 1, __FILE__, __FUNCTION__, __LINE__ );
1899-
QgsLogger::debug( "STDDEV", myRasterBandStats.stdDev, 1, __FILE__, __FUNCTION__, __LINE__ );
1890+
QgsLogger::debug( "************ STATS **************", 1, __FILE__, __FUNCTION__, __LINE__ );
1891+
QgsLogger::debug( "VALID NODATA", mValidNoDataValue, 1, __FILE__, __FUNCTION__, __LINE__ );
1892+
QgsLogger::debug( "MIN", myRasterBandStats.minimumValue, 1, __FILE__, __FUNCTION__, __LINE__ );
1893+
QgsLogger::debug( "MAX", myRasterBandStats.maximumValue, 1, __FILE__, __FUNCTION__, __LINE__ );
1894+
QgsLogger::debug( "RANGE", myRasterBandStats.range, 1, __FILE__, __FUNCTION__, __LINE__ );
1895+
QgsLogger::debug( "MEAN", myRasterBandStats.mean, 1, __FILE__, __FUNCTION__, __LINE__ );
1896+
QgsLogger::debug( "STDDEV", myRasterBandStats.stdDev, 1, __FILE__, __FUNCTION__, __LINE__ );
19001897
#endif
19011898

1902-
myRasterBandStats.statsGathered = true;
1899+
myRasterBandStats.statsGathered = true;
1900+
1901+
}
19031902

19041903
return myRasterBandStats;
19051904

19061905
} // QgsGdalProvider::bandStatistics
19071906

1908-
19091907
/**
19101908
Builds the list of file filter strings to later be used by
19111909
QgisApp::addRasterLayer()

0 commit comments

Comments
 (0)