Creating Contours from DEM using QGIS

I often get asked to create contours from DEM for engineers and architects for site planning purposes. I primarily use ArcGIS Pro at work to generate contours with ESRI provided terrain data or Lidar derived DEM. But for this tutorial I will show you how to generate contours from a publicly available DEM using QGIS.

For this tutorial, I am using a 30-meter (1-arc-second) SRTM DEM. There are various ways you can download this data. Below are some options with some tips on downloading and using the SRTM data. This tutorial provides detailed instructions for option 4 which is using the SRTM downloader plugin directly in QGIS (version 3.22).

Notes on SRTM data

If you are not familiar with SRTM data, here are some quick notes on to keep in mind.

  • There are 3 versions of SRTM data. Version 1 is the semi raw data (which you do not want), version 2.1 is non-void filled, and version 3 (SRTM Plus) is void-filled. Version 3 is the version used here

  • SRTM comes in either 1-arc second (30 meters) or 3-arc seconds (90 meters) and are divided into 1°×1° data tiles.

  • Data downloads are distributed as a zipped file in *.hgt format. The filename of the *.hgt file is labeled with the coordinate of the southwest (bottom left corner) cell. For example, the file N45W122.hgt contains data from 45°N to 46°N and from 122°E to 123°E inclusive.

  • *IMPORTANT: the name of the *.hgt file actually contains the source of the file’s georeferencing or coordinate system – DO NOT rename the file, otherwise it will not display properly

  • QGIS with GDAL support can natively read the hgt files (as long as the file hasn’t been renamed)

For more information on SRTM , check out the resources linked below:

SRTM Download Options

NOTES: Some sources require an account to download data. These accounts are free but you need to register first before you can download data.

Option 1: SRTM Tile Downloader

This is a nice and simple web app that allows you to quickly download SRTM tiles (1°×1° tiles). I wished it had an option to change basemap for viewing purposes.

SRTM Tile Downloader

To download the data tile, click the Download button and login with your Earth Data account

Tip on extracting the zipped file: When extracting the downloaded file (e.g. N45W122.SRTMGL1.hgt.zip) make sure the extracted folder does not contain the .hgt extension, otherwise QGIS will see the folder as N45W122.SRTMGL1.hgt and think it is a data format (which will not display) instead of N45W122.SRTMGL1.

Extracting *.hgt file

When extracting, make sure to remove the *.hgt for the folder name

Option 2: OpenTopography Download Site

The OpenTopography site does not require an account to download but it does require an email address when submitting download request, so you can describe and keep track of your data download. You can specify a custom AOI extent instead of downloading by tile. This site also has the option to download data as a Geotif, Arc ASCI Grid, or IMG file. However, the data is provided as a tar.gz file, which you will have to unzip first - see the tip below.

OpenTopography Site

OpenTopography site for downloading SRTM data. Instructions for download on the site are self explanatory.

Tip on extracting TAR.GZ on Windows 10 OS:

  • Run Command Prompt as an administrator

  • cd into directory containing the zipped file (eg. cd C:\temp)

  • use the tar -xvzf command on the zipped file (e.g. type in tar -xvzf rasters_SRTMGL1.tar.gz)

Extracting tar.gz windows 10 OS

Example of how to extract TAR.GZ file on Windows 10 OS

Option 3: USGS Earth Explorer

This option requires you to have an Earth Explorer account to download data. It’s not quite as easy to use as some of the other options, but it allows you to download other types of datasets as well as being able to upload a shapefile or KML for your AOI. It also offers download options as Geotif, BIL, DTED formats.

USGS Earth Explorer SRTM download

SRTM data download options in USGS Earth Explorer. Note you will have to need to login with an Earth Explorer account first to download data, otherwise download option is grayed out.

Option 4: SRTM Downloader QGIS Plugin

With this option, you can download SRTM data directly in QGIS. You will need to install the SRTM downloader plugin and also have an Earth Data account. This is the option I will show you here.

Install SRTM-Downloader Plugin

To install the SRTM downloader plugin:

  • In QGIS (I am using version 3.22), go to Plugins menu >> Manage and Install Plugins

  • In the Plugins window:

    • select All >> search for SRTM-Downloader and select it >> Click Install Plugin.

    • you should get a “plugin installed successfully” message. Close the window

SRTM Downloader plugin QGIS

Download SRTM Tile in QGIS

After installing the SRTM-Downloader plugin, you’ll want to add in a basemap and zoom in to your area of interest (AOI). Here, I am using an OpenTopoMap as the basemap. My AOI is Mount Hood, OR.

QGIS SRTM tile

My AOI is Mount Hood, OR.

Open the SRTM downloader:

  • Go to Plugins menu >> SRTM-Downloader >> SRTM downloader

  • In the SRTM downloader window: enter your parameters for your AOI and select a folder to download the tile to

  • After you click the Download button, another window should pop up asking you for your Earth Data account login

SRTM Downloader Plugin QGIS

Clicking the Download button will bring up a pop up window asking for your Earth Data account login (see below)

Enter your Earth Data login info. Check the box to save credentials in you want. Click OK and it should download the data.

When it’s done downloading, the DEM should be loaded in QGIS (unless you uncheck that option in the download window). Here is my DEM tile for Mount Hood area.

SRTM DEM Mount Hood OR

Create Contours from DEM

Now that you have a DEM, you can create contours from it.

  • Go to Raster menu >> Extraction >> Contours

  • In the Contour window: enter your parameters: eg. 50 meter interval, save to file as a shapefile, geopackage, dxf, etc.

Raster Create Contour

Enter your parameters (e.g. 50 meter interval, saved as shapefile), click Run then close the window when done

Here is my resulting contours at 50 meter interval.

Mount Hood Contours

50 meter contours (magenta lines) shown on top of OpenTopoMap

Here’s a close up view showing the contours (magenta lines) on top of OpenTopoMap - the contours generated from the SRTM DEM at 50 meters interval matches up well with the topo lines shown on the OpenTopoMap (topo lines are also derived from SRTM data).

Generated contours (magenta lines) matches the topo lines on the OpenTopoMap pretty well. Although, if you look inspect closer, the generated contours aren’t as smoothed in some areas as the OpenTopoMap lines.

That’s it for this tutorial. Thanks for reading. Until next time…

Bonus Follow Up - Tips on DEM and Contour Visualization

3D DEM Visualization in QGIS 3.0

The other day I just happened to be looking through ESRI’s ArcUser magazine (Winter 2018) while waiting for a process to finish on my computer, and came across an article on visualizing DEM using multidirectional hillshade -  Create Amazing Hillshade Effects Quickly and Easily in ArcGIS Pro.  Inspired by this article, I thought I would try do the same thing in QGIS 3.0. Below is a short tutorial on how to visualize a DEM in 3D with QGIS 3.0. I'm thinking that the more I use QGIS (especially the newest update) the more I like it.

A quick note on traditional vs multidirectional hillshade:

Traditional Hillshade: hillshading illuminated from 315 deg azimuth - so only single source of light

Multidirectional Hillshade: a combination of hillshading illuminated from 225, 270, 315, and 360 deg azimuth - 4 different sources of light

1. Download DEM

First, you'll need a DEM for your area of interest. For this tutorial, I downloaded a 1 meter DEM for Diamond Head Crater from NOAA Data Access Viewer. Below is the specs I used for my download.

DEMSpec.jpg

After you download your DEM, unzipped it. You're going to then use QGIS 3.0 to view it

2. Create Multidirectional hillshade

I am using QGIS 3.0 and I assume you have it installed already. So here's what you're going to do. First, add the DEM to the QGIS, then make a copy of it. You'll end up having 2 DEM layers for viewing purposes: 1) colored DEM used for drapping over 2) the hillshade. You are not actually creating a new layer or doing anything to the actual DEM.

  • In QGIS open add your DEM to the Layers Panel

  • Make a copy of the DEM. Right Click on your DEM >> Duplicate

  • Optional - In your Layers Panel, rename your second copy of the DEM to indicate that this layer will have the Hillshade effect applied to it - like the screenshot below:

Fig0001A.png
  • Turn off the top layer because you're going to create the hillshade effect on the second layer first.

  • Right click on Hillshade_oahu_dem_2013 >> Properties >> Style

    • Render type: Hillshade

    • Multidirectional: check the box

    • Resampling: Use either Bilinear or Cubic and Average to get a smoother looking hillshade. The default Natural Neighbor produced weird cross hatching effect.

Fig0004.png

Try to see what happens when you checked (multidirectional hillshade) and unchecking (traditional hillshade) the Multidirectional hillshade box. Here's are two views of a traditional hillshade and a multidirectional one. It looks to me like there is less shadowing  in the multidirectional hillshade (which would make sense if illumination is coming from multiple directions) then the traditional one - this though I think makes it have less contrast (?) so things don't seem to pop out as much??

Fig0025.png
Fig0026.png

3. Drape Colored DEM Over Hillshade

After you create the multidirectional hillshade, you're going to just change the color of the top DEM layer and "draped" it on top.

  • Right click on the top DEM (e.g. oahu_dem_2013) >> Properties >> Style

    • Rendered type: single pseudocolor

    • Make sure min/max values are correct for your DEM

    • Color ramp: Use drop down menu >> Select all Color Ramps >> BrBG (this is the one I'm using). To invert color ramp, right click inside the color box >> Invert Color

    • Blending mode: Multiple (this produces better effects than Normal blending with transparency)

    • Resampling: again use Bilinear or Cubic to get smoother effect

    • Optional Transparency: change transparent if you want. I have it at 50%.

Fig0018.png

Here is a view with the colored DEM draped over the multidirectional hillshade. Play around with the setting options to get your desired effect.

Fig0020.png

For comparison, here's the same view I did in ArcGIS Pro. Both results look really good - although I think ArcGIS Pro's multidirectional effect (using default settings) has a slight edge over the QGIS one (also using default settings).

Fig0019.png

4. Create 3D Map View

You can now view/simulate 3D landscape (DEM) in QGIS 3.0 natively without a plugin.

  • In QGIS, go to View menu >> New 3D Map View

  • In the 3D Map 1 window: click the Configure button

    • Elevation: select your DEM or the hillshade

    • Make any changes to the other settings if you want

  • In the 3D Map 1 window: hold down the shift key and the left mouse button to zoom in/out and rotate

Fig0028.png
Fig0029.png

Did you also know that you can customize the QGIS user interface by stacking and moving panels and map views around - try dragging and docking the panels in your user interface. The image below shows my user interface setup with the main map view or canvas on the left and the 3D Map view on the right.

Fig0022.png

Here is one of my favorite 3D visualization where I burned (Blending mode = burn) an aerial (ESRI Aerial base map) into the multidirectional hillshade in QGIS.

Fig0023Burn.png

QGIS and ArcGIS Pro DEM Comparisons

My conclusion having used the same method for 3D DEM visualization in both QGIS 3.0 and ArcGIS Pro is that they're both very good and comparable. Of course you can do some comparisons and decide for yourself, but I think QGIS does a great job considering it is free. Anyway, below are some of my snapshots for comparison.

Fig0012.png
Fig0016.png

That's it for this post. I hope you enjoyed reading it. Feel free to drop me a note in the comments section or via email anytime.