Reputation: 6312
I am trying to read the band of a TIFF as an array. The problem is GDAL does not ignore the NoData values. Is there a way to tell GDAL to ignore it?
When I compute statistics, GDAL ignores the NoData values.
import os, sys, gdal, ogr, numpy
from gdalconst import *
# register all of the drivers
gdal.AllRegister()
# open the image
ds = gdal.Open('test_slope.tif', GA_ReadOnly)
# get image size
rows = ds.RasterYSize
cols = ds.RasterXSize
bands = ds.RasterCount
# Set NoData Value
band = ds.GetRasterBand(1)
ndv = -3.4028230607371e+38
band.SetNoDataValue(ndv)
# Get Statistics
stats = band.ComputeStatistics(0)
print stats
# read in band as array
bandList = []
band.GetNoDataValue()
data = band.ReadAsArray(0, 0, cols, rows)
print data
>>>
[0.0, 126.59918975830078, 25.757117870945123, 15.399812314100501]
[[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]
[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]
[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]
...,
[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]
[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]
[ -3.40282306e+38 -3.40282306e+38 -3.40282306e+38 ..., -3.40282306e+38
-3.40282306e+38 -3.40282306e+38]]
>>>
Upvotes: 4
Views: 7752
Reputation: 2881
I believe you could create a numpy.MaskedArray
from your numpy.ndarray
:
import numpy as np
import numpy.ma as ma
ndv = -3.40282306e+38
data = np.array([[0.0, 126.59918975830078, 25.757117870945123, 15.399812314100501],
[-3.40282306e+38, -3.40282306e+38, -3.40282306e+38, -3.40282306e+38]])
masked_data = ma.masked_where(data == ndv, data)
print masked_data
Result:
[[0.0 126.599189758 25.7571178709 15.3998123141]
[-- -- -- --]]
In numpy
, a combination of a numpy.ndarray
and a mask is used to allow for handling of missing data.
Upvotes: 4