[gdal-dev] Setting nodata tag with Python API

Frank Warmerdam warmerdam at pobox.com
Thu Dec 31 11:27:46 EST 2009


Roger André wrote:
> Hi All,
> 
> I'm using the Python API to build some GeoTIFFs and would like to set
> the nodata value to -9999 at creation time.  How do I do that?  Below
> is a sample of my code
> 
>   def makeRaster(self):
>     out_drv = gdal.GetDriverByName('GTiff')
>     out_ds = out_drv.Create(self.raster_name, 4320, 2160 , 1, gdal.GDT_Float32)
>     out_ds.SetGeoTransform([-180, 0.0833330, 0, 90, 0, -0.0833330])
>     out_band = numpy.zeros([out_ds.RasterYSize, out_ds.RasterXSize])
>     for iY in range(out_ds.RasterYSize):
>       for iX in range(out_ds.RasterXSize):
>         alloc_key = self.makeAllocKey(iY + 1, iX + 1)
>         pixel_value = self.checkPixel(alloc_key)
>         out_band[iY][iX] = pixel_value
>     out_ds.GetRasterBand(1).WriteArray(out_band)
>     out_ds = None

Roger,

Try adding:

   out_ds.GetRasterBand(1).SetNoDataValue( -9999 )

after the WriteArray() (or anywhere after the Create() call I suppose).

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent



More information about the gdal-dev mailing list