## Using NASA DEM data to create high resolution contour lines with QGIS

Recently, a few cycling buddies and I wanted to design our own cycling jerseys (a post about that whole process is in the making). Contours are a popular choice of theme for their simplicity and serve as an easy way of making the design unique and personal through choice of the geographical location.

# Using SwissTopo

The Federal Office of Topography Switzerland SwissTopo has a fantastic high resolution interactive map with lots of different datasets.

And even though it has different height models, there are no ready-made contour images. I’m sure one could download some of the datasets and work on that further.

The closest thing (artistically) I could find to contour lines was the Gravimetric Atlas

with a little imagemagick magic, I could transform this into a low quality version

Of course this has nothing to do with contours and it doesn’t allow for any customisation. Anyway, it is a cool resource that I wanted to share.

# Using QGIS

Instead of relying on pre-made contour images we can try to make our own; this has the advantage that we can configure parameters as such the height between contour lines, colours, line thickness.

QGIS is a program with which one can manipulate map data; but before we get onto using that, we need to source the Digital Elevation Model (DEM) data from which we can produce contour lines.

NASA offers many different elevation model datasets. One of the highest resolution datasets currently available is the NASA Shuttle Radar Topography Mission Global (SRTMGL1) dataset which is accurate up to 1-arc second (~30m). Another 1 arc-second resolution dataset is the Terra Advanced Spaceborne Thermal Emission and Reflection –> Riometer (ASTER) Global Digital Elevation Model (GDEM) version 3.

After creating a free account, this data can be downloaded by the public through the earthdata portal. If the link doesn’t work, search for “SRTMGL1” in the main search field of search.earthdata.nasa.gov. Under the “NASA Shuttle Radar Topography Mission Global 1 arc second V003” section, there are ~15,000 “granules” each with a title of the form

N<North Coordinate>E<East Coordinate>.SRTMGL1.hgt.zip
S<South Coordinate>E<East Coordinate>.SRTMGL1.hgt.zip
N<North Coordinate>W<West Coordinate>.SRTMGL1.hgt.zip
S<South Coordinate>W<West Coordinate>.SRTMGL1.hgt.zip

for example

N47E008.SRTMGL1.hgt.zip

for Zurich. Note the search function for Granule ID(s) searches for exact matches, so use the wildcard * character.

The downloaded granule will be in the form of a zip archive with a title like N47E008.SRTMGL1.hgt.zip. We don’t need to worry about unzipping this because QGIS will do this automatically for us.

EARTHDATA_PASS=<EARTHDATA_PASS>
LOCATION=N47E008
wget --user="$EARTHDATA_USERNAME" --password="$EARTHDATA_PASS" \
SMOOTHED=$LOCATION-s$SIGMA-r$KERNEL.sdat" saga_cmd grid_filter "Gaussian Filter" -INPUT "$ORIGINAL" -SIGMA "$SIGMA" -KERNEL_TYPE 0 -KERNEL_RADIUS "$KERNEL" -RESULT "$SMOOTHED" Note that smoothing can take a very long time (~1 hour). So I have provided a small test grid showing how different values of$SIGMA and $RADIUS will give us different types of smoothing However, it is likely, that some trial and error will be required. # Creating Contours (Part 2) I will continue using N47E008 smoothed with SIGMA=50 and RADIUS=100. We can create different types of graphics now By adding a thicker set of contours every 50m to the 10m contour lines, we get this kind of graphic It is possible to add labels and other features. This is described in great detail in this video by “Open Source Options”. Although this has limited application since we have smoothed the data, making it geographically incorrect; although, having elevation labels may fit the “contour line aesthetic”. Often however, drawing contour lines a fixed distance apart creates a combination of crowded pockets and sparse planes. We can fix this, by drawing contour lines at exponential height intervals. That is, for a given base$b$, draw contour lines at heights$b^1, b^2, b^3, \ldots$. I had to do some trial and error with different bases, and settled on 1.1. However, since Zurich as a whole is at around 400m above sea level, the differences between 400m and 500m aren’t that obvious: the difference in elevation between contour lines is increasing, meaning that there aren’t many contour lines between 400m and 500m. gdal_contour has an -OFFSET option which allows one to specify what we consider to zero (relative) elevation. Ultimately, this will cause contour lines to be drawn at$C + b^1, C + b^2, C + b^3, \ldots$where$C\$ is the offset constant.

With this option, smaller deviations in the flatter areas are still visible without crowding hilly areas many contour lines.

# Automating almost everything

We can automate the entire process quite easily with two scripts found on my GitHub account.

The bash script handles downloading the data from NASA, smoothing the data, and then producing the contour files.

The python script takes the contour files and actually draws them into a png image.