Note that there are some explanatory texts on larger screens.

plurals
  1. PORasterizing a GDAL layer
    primarykey
    data
    text
    <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="noreferrer">documentation</a>:</p> <pre><code>import random from osgeo import gdal, ogr RASTERIZE_COLOR_FIELD = "__color__" def rasterize(pixel_size=25) # Open the data source orig_data_source = ogr.Open("test.shp") # Make a copy of the layer's data source because we'll need to # modify its attributes table source_ds = ogr.GetDriverByName("Memory").CopyDataSource( orig_data_source, "") 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["arbitrary"]') # Rasterize err = gdal.RasterizeLayer(target_ds, (3, 2, 1), source_layer, burn_values=(0, 0, 0), options=["ATTRIBUTE=%s" % RASTERIZE_COLOR_FIELD]) if err != 0: raise Exception("error rasterizing layer: %s" % 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="noreferrer">java bindings</a>).</p> <p>I adapted a <a href="http://svn.osgeo.org/gdal/trunk/autotest/alg/rasterize.py" rel="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("data.shp") # Run the algorithm. err = gdal.RasterizeLayer(target_ds, [3,2,1], cutline_ds.GetLayer(0), burn_values=[200,220,240]) if err != 0: print("error:", 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="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 "dirty data" (degenerate polygons, etc...), and sometimes specified in large coordinates) ?</p> <p>For example I want to go from this:<br> <a href="http://i.imagehost.org/0232/from.png" rel="noreferrer">http://i.imagehost.org/0232/from.png http://i.imagehost.org/0232/from.png</a></p> <p>To this:<br> <a href="http://f.imagehost.org/0012/to_4.png" rel="noreferrer">http://f.imagehost.org/0012/to_4.png http://f.imagehost.org/0012/to_4.png</a></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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload