Basic GDAL commands for Cartographers

Over the past few years at Ordnance Survey I have done a considerable amount of work with our IS Development and Testing department. This has ranged from product development tasks to GIS support for web service creation. One thing that has repeatedly cropped up in that time is the need for reliable processing of map data outside of our regular cartographic software packages. This is where we have turned to GDAL.

GDAL stands for the Geospatial Data Abstraction Library, and is an open-source translator library for raster geospatial data formats. As it is purposely made for geographic data, this is by no means an exhaustive list of uses, but what follows is a short list of examples where GDAL can be really, really useful in the everyday work of a cartographer, especially if one’s work includes product development as mine does.

GDAL supports over 50 raster formats, and OGR over 20 vector formats. The official list of utility programs supported by GDAL is a good place to look after this for further information and uses.

 
Working with TIFFs

a)      Tagging a raster with file information

With GDAL installed, you can run the utilities as a command line. The utility program we use for TIFF tags is called gdal_translate. Say you have a TIFF file of a raster map that you wish to add an image description and copyright to the metadata. The command you need to run will look like this:

gdal_translate –mo “TIFFTAG_IMAGEDESCRIPTION=My wonderful map” –mo “TIFFTAG_COPYRIGHT=Copyright 2014 Christopher Wesson” c:\Data\input.tif c:\Data\output.tif

Substitute obviously your own tags and filepaths. The speech marks are not necessary but we use them to ensure the tags are contained and treated like text strings.

Next, we can make the command a bit longer and specify the output TIFF ‘save as’ options too if we wish. These use the –co library as follows:

gdal_translate –mo “TIFFTAG_IMAGEDESCRIPTION=My wonderful map” –mo “TIFFTAG_COPYRIGHT=Copyright 2014 Christopher Wesson” –co COMPRESS=LZW –co NBITS=8 c:\Data\input.tif c:\Data\output.tif

b) Converting a standard TIFF into a GeoTIFF

To do this we need to know the corner coordinates and projection of the original raster. This can be obtained, for example from a Tiff World file, but I won’t go into that here (see ESRI website or Wikipedia or email me if you’re stuck).

The command here is also using the gdal_translate utility and will look like this:

gdal_translate –a_srs EPSG:27700 –a_ullr minX maxY maxX minY –co PROFILE=GeoTIFF c:\Data\input.tif c:\Data\output.tif

Substitute the minimum and maximum values from your own data where I have written ‘minX’, etc; and substitute your own filepaths.

A full list of acceptable TIFF file information tags and file specification (save as) commands can be found on the GDAL website here.

And from the next full release (v2) of GDAL, we will be able to define colour profiles too.

 
Transforming data between projections

Reprojecting data is something that seems a little scary and can sometimes go wrong in a GIS package. However a free piece of software called FWTools can take all of the pain out it and provide a simple, reliable and unbelievably quick way of turning your geodata from one coordinate system to another.

FWTools is a bundle or kit that combines GDAL with other binary libraries and also offers a window to run the commands from. So you could, if you wished, also use it to run the gdal_translate commands from section 1, above.

Once installed, open the FWTools command window. It looks just like and MSDOS prompt. And depending on your location of installation and version installed it will open with some text similar to the following:

C:\FWTools2.4.7>

Reprojection makes use of the ogr2ogr program in GDAL. We can type our command immediately after this as follows:

C:\FWTools2.4.7>ogr2ogr –s_srs EPSG:27700 –t_srs EPSG:4258 c:\Data\reprojected.shp  c:\Data\original.shp

Substitute the EPSG values for your own coordinate systems. The first value is of the original data, the second is the system to transform the data into. Then substitute the filepaths: Quite why the new file comes before the original I have no idea, but it does!

To find the EPSG values of projections, the website spatialreference.org is very handy.

 

As well as being quick and reliable, GDAL is also well-liked by our IS department because these utility programs can be ran as commands without the need to open and close any desktop software.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: