[gdal-dev] Re: Convert vector to raster with colored polygons

Chaitanya kumar CH chaitanya.ch at gmail.com
Tue May 24 20:35:20 EDT 2011


1. First, make sure the attribute value field contains a single value for
each color, starting from one.

2. Run gdal_rasterize after adding the option '-init 0' to your original
options. This option makes sure that the background pixels are initialized
to zero. You will get a greyscale image.

3. The best way to color this image using gdal utilities is by using a .vrt
file. You may also be able to do this using an image editor like GIMP. Run
gdalbuildvrt with the option '-srcnodata 0' to create a VRT named, say,

4a. Refer to the GDAL VRT tutorial [1] and add ColorTable element inside the
VRTRasterband element. It should be at the same level as the NoDataValue
element (Which should contain the value 0 here).

4b. For the first Entry, which interprets the value 0, set the alpha value
(c4) to 0 to make it transparent. i.e. <Entry c1="0" c2="0" c3="0" c4="0"/>

4c. For the subsequent values set the alpha value to 255 and the RGB values
to show whatever color you need.

5. Run gdal_translate on output.vrt with the option "-expand rgba". The
output raster will contain the colors you specified in the ColorTable with
the background transparent.

[1]: http://www.gdal.org/gdal_vrttut.html

On Wed, May 25, 2011 at 2:58 AM, spiderplant0 <spiderplant0 at gmail.com>wrote:

> Hi, Thanks for the reply.
> When I tried this first, the image was displaying just black and white. But
> after some experimentation I discovered that '-ot Byte' with gdal_rasterize
> gave polygons colored various shades of grey - though not related to my
> attribute - seemed to be random.
> I dont understand what you mean by 'If the RGB values are in separate
> attributes'. I have one attribute and I want the polygon to be colored
> depending on the value of the attribute for this polygon. Eg
> if attribute is between 100 and 199 color polygon green
> if the attribute is between 200 and 299 color polygon red
> etc
> (I can change the attribute to discrete integer levels if ranges aren't
> allowed).
> I type this:
> gdal_rasterize -ts 500 900 -ot Byte -a attributeName -l layerName
> vectorFilename.shp rasterFilename.tiff
> Though I'm not sure if this is correct. I guess this is a greyscale image
> and not full color. Can't figure out how to get full color.
> Do you know how to add a color table? Is there a gdal utility for this?
> I couldn't find much on the web to explain how to add a color table other
> than a suggestion for converting to a .vrt and manually editing the file. I
> guess I want a palette raster? But I couldnt figure out the format of the
> color table. Or how to specify that the background should be transparent.
> Any clues you can give me would be appreciated.
> --
> View this message in context:
> http://osgeo-org.1803224.n2.nabble.com/Convert-vector-to-raster-with-colored-polygons-tp6396185p6400545.html
> Sent from the GDAL - Dev mailing list archive at Nabble.com.
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev

Best regards,
Chaitanya kumar CH.
/tʃaɪθənjə/ /kʊmɑr/
17.2416N 80.1426E
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20110525/34fcc77e/attachment.html

More information about the gdal-dev mailing list