After doing calculations on a raster and saving it, the image statistics are wrong and need updating.

I find examples saying that this should do the job, like in this example:


However this does not seem to update my band statistics correctly, they seem ok in Python with data.max() and data.min(), but opening the file in QuantumGis (image properties>style>load min/max) shows the wrong stats. I only get new image statistics correctly by separately opening and setting statistics like this:

driver = gdal.GetDriverByName('GTiff')
ds = gdal.Open(infile, gdal.GA_Update)
dsband = ds.GetRasterBand(1)
(newmin, newmax)= dsband.ComputeRasterMinMax(0)
(newmean, newstdv) = dsband.ComputeBandStats(1)
dsband.SetStatistics(newmin, newmax,newmean, newstdv)
dsband = None  
ds = None

So what's the difference between both and why does QGis only show the correct stats with the second code?

thanks for any advice!


The following code works for me (in that QGis reads the statistics correctly). Note use of ComputeStatistics instead of GetStatistics as that will force gdal to recalculate band statistics.

driver = gdal.GetDriverByName( 'GTiff' )
dst_ds = driver.Create( dst_filename, 512,512, 1, gdal.GDT_UInt16 )
raster = somenumpy.calculation(etc...)
rb.WriteArray( raster )
dst_ds = None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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