Monday, 22 October 2012

GDAL Rasterize

I wanted to convert a vector outline of the GB coastline into a raster in order to use it as a background layer in MapGuide  Maestro. I would usually do such a process in ArcMap, but I am trying to learn open source alternatives to these types of functions. As a result I have been playing around with GDAL and OGR for a week or so now, and have been very impressed with their power and versatility, I only wish I could run them at a UNIX shell at work instead of at the windows command line. With these two packages, their python bindings, numpy, pyshp and matplotlib I think I could begin to move away from ArcGIS without too much pain.

Version 1.8 and up of GDAL support the creation of output raster files when running the GDAL_RASTERIZE command, which makes this operation a single line process, there are a number of switches and options which can be used but here I will only go through the switches used for this process. The command I used to convert the vector outline to a raster file was:


The individual switches operate as follows:
  • -a ID Indicates the attribute to be used as the key when converting the vector file. This can be extended using an SQL statement and the -SQL switch to allow for selections of only parts of a file to be converted.
  • -l GB_Fix Is the name of the layer to be converted
  • -a_nodata -9999  Assigns a raster nodata value of -9999
  • -a_srs EPSG:3857 Sets the coordinate system using an EPSG code
  • -tr 1000 1000 Sets the raster cell size, the smaller these values are the bigger the output file will be. I initially ran the command with 10 10 and got a file of over 15Gb
  • -ot Float32 The datatype of the output file, either Byte, Float32 or Float64 depending on the file format
  • GB_Fix.shp dest.tif The input and output filenames, including extensions

This is just scraping the surface of this one command but serves as a starting point for any future conversions I have to perform using GDAL.

No comments:

Post a Comment