Converting Vertical Units in a Digital Elevation Model


Sometimes you need to convert the vertical units in a digital elevation model (DEM). And sometimes you have to do it for a whole bunch of DEMs. Not everyone has a high-end GIS package with good batch processing capability, so what can you do? Luckily there is some very good free and open source software that can help you do the job. Today we’re going to look at converting a DEM between feet and meters using GDAL.

The specific program we’re going to use is GDAL_translate. When I first read the docs for it, I didn’t think it could do what I needed. It wasn’t until I got into the code to add what I wanted that I realized it was already there. I’m writing this under the assumption that I’m not alone in missing the obvious.

What I’d missed was how to use the -scale argument. Based on the name, it certainly looks like the right thing. However, it’s written as if your goal is something like re-scaling a band to use the full scale (e.g. expand it to use 0 to 255). You have to provide the min and max for input and output. When you look in the code, you realize it is using that to compute a scale and offset, which is just what you’d need to convert. So, here’s an example of the arguments to convert from feet to meters:

gdal_translate -scale 0 1 0 0.3048 inputfeet.tif outputmeters.tif

That essentially says that 1 foot is 0.3048 meters with a zero intercept. To go from meters to feet, you just swap the scale arguments:

gdal_translate -scale 0 0.3048 0 1 inputmeters.tif outputfeet.tif

To do lots of them, you simply use you favorite shell, such as Bash, Powershell, or CMD, to loop over the files you need to do. Here’s a somewhat lazy Bash example to do all the files in a directory:

mkdir meters
for file in *.tif
do
  gdal_translate -scale 0 1 0 0.3048 $file meters/$file
done

One of the best things about GDAL and GDAL_translate is that they understand a lot of different file formats. I used a TIFF as my example, but many formats that hold floating point numbers can work too. Plus, the output format doesn’t need to be the same as the input format.

5 comments

  1. Kirk,

    This is exactly what I need, but I don’t know how to use GDAL Translate. Would you please add a little to your post showing how, or maybe point me to more info on how to use GDAL Translate?

    Dave Stoll

    Like

    • Dave,
      The best place to start for information on using GDAL and it’s utility programs is the http://www.gdal.org site. For gdal_translate in particular, you might want to look at http://www.gdal.org/gdal_translate.html. The main stumbling block I think many people have these days is that the utilities are command line. Once you figure out how to use command line programs, this should be pretty straightforward. There are also a number of packages (such as QGIS or OSGeo4W) that will also have the GDAL utilities and may provide a GUI interface. Without knowing where you got stuck, I don’t know where to help you. How far did you get in this list of steps?
      1) download and install GDAL
      2) Optionally add the GDAL binary location to your PATH environment variable.
      3) start a shell (cmd, power shell, bash, etc)
      4) navigate to where your data is unless you like to type full path names
      5) run gdal_translate as shown in the blog post or in the gdal examples. If you didn’t do step 2, you’ll have to give the full path to the gdal_translate binary.

      Like

      • Kirk,

        OK, I downloaded GDAL Translate. In the latest version of QGIS, GDAL is already in it, and I definitely prefer the GUI to command line. I know nothing about the command line, so you lost me at step 2.

        I thank you for the two links. I will investigate the GDAL Org site and do some reading.

        Thanks for the quick response.

        Dave

        Like

      • QGIS should have a way to provide that functionality, though I don’t know it off the top of my head. In my personal opinion, the command line is worth learning. It’s not particularly hard and can be more efficient and flexible. However, I realize there is only so much time in the day. A search for “windows cmd tutorial” brings up lots of options to learn more. I’m assuming windows because Linux users are very likely to use the command line already.

        Like

  2. Kirk,

    In QGIS I used Raster >> Conversion >>Translate to convert the DEM Feet to Meters. I Copy-Pasted in your “-scale 0 1 0 0.3048” code. It worked well, but I was surprised that the converted DEM file I exported was 2/3 the size of the original.

    Thank you for the Scale Code, without which I would not have been able to do this.

    Dave

    Like

Leave a Reply. Comments are moderated.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s