[gdal-dev] Mask value data type in Python wrappings

Dave Welch dwelch at urthecast.com
Thu Feb 18 07:44:27 PST 2016


Hi all,

I’m trying to mask a band from a GeoTiff file like so:

>>> from osgeo import gdal, ogr, osr
>>> from gdalconst import *
>>> import numpy as np
>>> import matplotlib as mpl
>>> dataset = gdal.Open('001.tif')
>>> band = dataset.GetRasterBand(1)
>>> ba = band.ReadAsArray()

>>> print "Origin:", ba[0, 0], type(ba[0, 0])  # NODATA value in image == zero

Origin: 0 <type 'numpy.uint16'>

>>> print "(5, 5):", ba[5, 5], type(ba[5, 5])

(5, 5): 42271 <type 'numpy.uint16'>

>>> print "Nodata:", band.GetNoDataValue()     # NODATA from file == None

Nodata: None

>>> print "Mask flags:", band.GetMaskFlags()

Mask flags: 1


>>> band.SetNoDataValue(0)                     # Set the NODATA value to zero
>>> print "Nodata:", band.GetNoDataValue(), type(band.GetNoDataValue())

Nodata: 0.0 <type 'float’>


>>> mb = band.GetMaskBand()
>>> ma = mb.ReadAsArray()


>>> print "Origin:", ma[0, 0], type(ma[0, 0])  # *** SHOULD BE ZERO!!! ***


Origin: 255 <type 'numpy.uint8'>


>>> print "(5, 5):", ma[5, 5], type(ma[5, 5])  # Correct

(5, 5): 255 <type 'numpy.uint8'>


>>> print np.any(ma==0)                        # Mask band is entirely 255

False

As you can see, the mask covers the entire image and not the zeros on the edge.

The band.GetNoDataValue() is returning a Python float() which leads me to suspect that there is incorrect type handling within GDAL’s Python wrapping such that the mask generation is trying to compare the band’s raster values (GDAL_UInt16) to whatever the NoDataValue type is stored internally.  Am I way off base or is there something I’m missing?

Cheers!
______________________________________________
Dave Welch Software Engineer
URTHECAST

111 West Port Plaza Suite 300  l  St. Louis, MO l USA l 63146

urthecast.com<http://www.urthecast.com/>  l  Connect @urthecast<https://twitter.com/urthecast>

This e-mail and any attachments are for the use of the intended recipient(s) and may contain legally privileged, proprietary and/or confidential information.
Any use or disclosure of this e-mail (including attachments) for any purpose other than those specifically authorized is prohibited.
If you are not the intended recipient, please immediately notify the sender and permanently delete all copies.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20160218/233037ec/attachment-0001.html>


More information about the gdal-dev mailing list