Rasterizing a GDAL layer

EDIT: I guess I’d use qGIS python bindings: http://www.qgis.org/wiki/Python_Bindings

That’s the easiest way I can think of. I remember hand rolling something before, but it’s ugly. qGIS would be easier, even if you had to make a separate Windows installation (to get python to work with it) then set up an XML-RPC server to run it in a separate python process.

I you can get GDAL to rasterize properly that’s great too.

I haven’t used gdal for a while, but here’s my guess:

burn_values is for false color if you don’t use Z-values. Everything inside your polygon is [255,0,0] (red) if you use burn=[1,2,3],burn_values=[255,0,0]. I’m not sure what happens to points – they might not plot.

Use gdal.RasterizeLayer(ds,bands,layer,burn_values, options = ["BURN_VALUE_FROM=Z"]) if you want to use the Z values.

I’m just pulling this from the tests you were looking at: http://svn.osgeo.org/gdal/trunk/autotest/alg/rasterize.py

Another approach – pull the polygon objects out, and draw them using shapely, which may not be attractive. Or look into geodjango (I think it uses openlayers to plot into browsers using JavaScript).

Also, do you need to rasterize? A pdf export might be better, if you really want precision.

Actually, I think I found using Matplotlib (after extracting and projecting the features) was easier than rasterization, and I could get a lot more control.

EDIT:

A lower level approach is here:

http://svn.osgeo.org/gdal/trunk/gdal/swig/python/samples/gdal2grd.py\

Finally, you can iterate over the polygons (after transforming them into a local projection), and plot them directly. But you better not have complex polygons, or you will have a bit of grief. If you have complex polygons … you are probably best off using shapely and r-tree from http://trac.gispython.org/lab if you want to roll your own plotter.

Geodjango might be a good place to ask .. they will know a lot more than me. Do they have a mailing list? There’s also lots of python mapping experts around, but none of them seem to worry about this. I guess they just plot it in qGIS or GRASS or something.

Seriously, I hope that somebody who knows what they are doing can reply.

Leave a Comment

tech