[Gdal-dev] tiff creation with a colormap ?

Frank Warmerdam warmerdam at pobox.com
Fri May 5 12:57:06 EDT 2006


Didrik Pinte wrote:
> Hi, 
> 
> Little technical question. I have (x,y,z) raster that i'm outputting to
> a GeoTiff file with the following code :
> 
> driver_options = ['TFW=YES' 'COMPRESS=DEFLATE']
> dst_ds = driver.Create( filename, len(self.idx['X']),\
>                             len(self.idx['Y']), 1, gdal.GDT_Byte,\
>                             driver_options )
> [... coord sys lines ...]
> dst_ds.GetRasterBand(1).WriteArray( numpy.transpose(raster) )
> 
> This creates a great raster in levels of gray.
> 
> Used to matplotlib, how can I output this raster with a colormap [1]? Do
> I have to generate three bands ?

Didrik,

Create a gdal.ColorTable object and then write it with SetRasterColorTable()
on the band.   Makeu sure you do this right after the create, and before
writing the raster data or else it will be too late.

Without testing, this might look something like:

  dst_ds = driver.Create( filename, len(self.idx['X']),\
                             len(self.idx['Y']), 1, gdal.GDT_Byte,\
                             driver_options )

  ct = gdal.ColorTable()
  for i in range(256):
    ct.SetColorEntry( i, (255, 255 - i, i, 255) )
  dst_ds.GetRasterBand(1).SetRasterColorTable( ct )

The tuple passed to SetColorEntry() holds Red,Green,Blue,Alpha values.

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    | President OSGF, http://osgeo.org




More information about the Gdal-dev mailing list