1

I want to know the basic stats (max, min, mean, std) of my raster file. I use two different methods and get similar, but different results. I assume the results should be exactly the same with any method, is that right? What am I doing wrong?

Let' use this single band raster as test file.

Method 1 (gdal on python)

from osgeo import gdal

myRaster = 'path/to/input.tiff'
gtif = gdal.Open(myRaster)

srcband = gtif.GetRasterBand(1)
stats = srcband.GetStatistics(True, True)
print ('Minimum: %.3f\nMaximum: %.3f\nMean: %.3f\nStDev: %.3f' % (stats[0], stats[1], stats[2], stats[3]))

With Method 1 I get the following values:
Minimum: -63.630
Maximum: 132.033
Mean: 37.191
StDev: 42.380

Method 2 (gdalinfo from command line)
I simply use the command gdalinfo input.tiff -stats

I get the whole stats including:
Minimum=-63.630
Maximum=147.225
Mean=33.218
StdDev=37.375

This means that both methods result in the same Minimum value (-63.630 in both cases), but differ in the Maximum value (with concomitant differences in Mean and Standard Deviation). I read somewhere that there can be some problems with how to treat NaN values, but I am not sure this is the problem here.

Edit 00: I just tried the same using rasterio and it gives exactly the same values (for all 4 stats) as the command-line gdalinfo

1
  • 1
    Gdalinfo reads the statistics from the image metadata if possible. Check if you have an aux.xml file and delete it first. Then use gdal_edit with unsetstats option. That way it is sure that gdalinfo -stats is computing the statistics from the pixel data. Maybe the gdalinfo option -mm has the same effect but I am not sure gdal.org/programs/gdalinfo.html#cmdoption-gdalinfo-mm.
    – user30184
    Sep 28 at 15:09

1 Answer 1

1

With your Python method by using the first "True" you ask GDAL to compute approximated statistics .

stats = srcband.GetStatistics(True, True)

See the documentation https://gdal.org/doxygen/classGDALRasterBand.html#a6aa58b6f0a0c17722b9bf763a96ff069

**GetStatistics()**

CPLErr GDALRasterBand::GetStatistics    (   int     bApproxOK,
int     bForce,
double *    pdfMin,
double *    pdfMax,
double *    pdfMean,
double *    pdfStdDev 
)       
virtual
Fetch image statistics.

Returns the minimum, maximum, mean and standard deviation
of all pixel values in this band. If approximate statistics
are sufficient, the bApproxOK flag can be set to true in which
case overviews, or a subset of image tiles may be used in 
computing the statistics.

The gdalinfo command gdalinfo input.tiff -approx_stats returns the same numbers than what you got with Python, so obviously it is using the same method for computing the approximated statistics.

STATISTICS_APPROXIMATE=YES
STATISTICS_MAXIMUM=132.03327655792
STATISTICS_MEAN=37.1910253144
STATISTICS_MINIMUM=-63.630443811417
STATISTICS_STDDEV=42.380388396356
STATISTICS_VALID_PERCENT=100

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.