GDAL Raster – NoData Value Ignored Issue

gdalrasterstatistics

I am trying to calcualate the statistics of a TIFF. The script should ignore the NoData value, but doesn't. What causes this?

import gdal
from gdalconst import *

# register all of the drivers
gdal.AllRegister()

# open the image
ds = gdal.Open('test_slope.tif', GA_ReadOnly)

# get raster band 1
band = ds.GetRasterBand(1)

# set NoData value
band.SetNoDataValue(-3.4028230607371e+38)

# Calculate and print Statistics
stats = band.GetStatistics(0,1)
print stats

The results look like this

>>> 
[-3.4028230607371e+38, 126.5991897583, -4.6432918375055e+37, 1.1680875238428e+38]
>>> 

Best Answer

GetStatistics will reuse previously computed statistics if they exist (i.e computed before you set the NoData value). You can use stats = band.ComputeStatistics(0) instead of GetStatistics to force the statistics to be recomputed.

Related Question