Monday 17 July 2017

raster - GDAL does not ignore NoData value


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]

>>>

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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...