title: Rasterizing a GDAL layer
body: <p><strong>Edit</strong></p>
  <p>Here is the proper way to do it, and the <a href="http://gdal.org/gdal__alg_8h.html#dfe5e5d287d6c184aab03acbfa567cb1" rel="nofollow noreferrer">documentation</a>:</p>
  <pre><code>import random
  from osgeo import gdal, ogr    
  
  RASTERIZE_COLOR_FIELD = &quot;__color__&quot;
  
  def rasterize(pixel_size=25):
      # Open the data source
      orig_data_source = ogr.Open(&quot;test.shp&quot;)
      # Make a copy of the layer's data source because we'll need to 
      # modify its attributes table
      source_ds = ogr.GetDriverByName(&quot;Memory&quot;).CopyDataSource(
              orig_data_source, &quot;&quot;)
      source_layer = source_ds.GetLayer(0)
      source_srs = source_layer.GetSpatialRef()
      x_min, x_max, y_min, y_max = source_layer.GetExtent()
      # Create a field in the source layer to hold the features colors
      field_def = ogr.FieldDefn(RASTERIZE_COLOR_FIELD, ogr.OFTReal)
      source_layer.CreateField(field_def)
      source_layer_def = source_layer.GetLayerDefn()
      field_index = source_layer_def.GetFieldIndex(RASTERIZE_COLOR_FIELD)
      # Generate random values for the color field (it's here that the value
      # of the attribute should be used, but you get the idea)
      for feature in source_layer:
          feature.SetField(field_index, random.randint(0, 255))
          source_layer.SetFeature(feature)
      # Create the destination data source
      x_res = int((x_max - x_min) / pixel_size)
      y_res = int((y_max - y_min) / pixel_size)
      target_ds = gdal.GetDriverByName('GTiff').Create('test.tif', x_res,
              y_res, 3, gdal.GDT_Byte)
      target_ds.SetGeoTransform((
              x_min, pixel_size, 0,
              y_max, 0, -pixel_size,
          ))
      if source_srs:
          # Make the target raster have the same projection as the source
          target_ds.SetProjection(source_srs.ExportToWkt())
      else:
          # Source has no projection (needs GDAL &gt;= 1.7.0 to work)
          target_ds.SetProjection('LOCAL_CS[&quot;arbitrary&quot;]')
      # Rasterize
      err = gdal.RasterizeLayer(target_ds, (3, 2, 1), source_layer,
              burn_values=(0, 0, 0),
              options=[&quot;ATTRIBUTE=%s&quot; % RASTERIZE_COLOR_FIELD])
      if err != 0:
          raise Exception(&quot;error rasterizing layer: %s&quot; % err)
  </code></pre>
  <p><strong>Original question</strong></p>
  <p>I'm looking for information on how to use <code>osgeo.gdal.RasterizeLayer()</code> (the docstring is very succinct, and I can't find it in the C or C++ API docs. I only found a doc for the <a href="http://map.hut.fi/doc/Geo-GDAL/html/class_geo_1_1_g_d_a_l.html#be1fb3f92ce85b06950935d6ccd83984" rel="nofollow noreferrer">java bindings</a>).</p>
  <p>I adapted a <a href="http://svn.osgeo.org/gdal/trunk/autotest/alg/rasterize.py" rel="nofollow noreferrer">unit test</a> and tried it on a .shp made of polygons:</p>
  <pre><code>import os
  import sys
  from osgeo import gdal, gdalconst, ogr, osr
      
  def rasterize():
      # Create a raster to rasterize into.
      target_ds = gdal.GetDriverByName('GTiff').Create('test.tif', 1280, 1024, 3,
              gdal.GDT_Byte)
      # Create a layer to rasterize from.
      cutline_ds = ogr.Open(&quot;data.shp&quot;)
      # Run the algorithm.
      err = gdal.RasterizeLayer(target_ds, [3,2,1], cutline_ds.GetLayer(0),
              burn_values=[200,220,240])
      if err != 0:
          print(&quot;error:&quot;, err)
  
  if __name__ == '__main__':
      rasterize()
  </code></pre>
  <p>It runs fine, but all I obtain is a black .tif.</p>
  <p>What's the <code>burn_values</code> parameter for ? Can <code>RasterizeLayer()</code> be used to rasterize a layer with features colored differently based on the value of an attribute ?</p>
  <p>If it can't, what should I use ? Is <a href="http://www.antigrain.com/" rel="nofollow noreferrer">AGG</a> suitable for rendering geographic data (I want <strong>no</strong> antialiasing and a very robust renderer, able to draw very large and very small features correctly, possibly from &quot;dirty data&quot; (degenerate polygons, etc...), and sometimes specified in large coordinates) ?</p>
  <p>Here, the polygons are differentiated by the value of an attribute (the colors don't matter, I just want to have a different one for each value of the attribute).</p>