Styling Contours in 3D (QGIS3)

Contributed by: Steven Kim

In the previous tutorial, Styling a River Network with Expressions (QGIS3) we used expressions to filter and style a river network. In this tutorial, we will work with a contour dataset and style it using expressions to give it a 3D effect.

Overview of the task

We will use a geometry generator symbol layer to dynamically apply an offset to the contours and then apply a color ramp to create an appearance of a 3d map.

../../_images/output1.png

Get the data

The National Map is a collaborative effort among the U.S. Geological Survey (USGS) and other partners to improve and deliver topographic information for the Nation. We will download a contour dataset from The National Map downloader.

  1. Visit the TNM Download v2. Browse to your region of interest. For this tutorial, we will be downloading the contours for a region around Austin, Texas.

../../_images/data19.png
  1. On the downloader, look for the Elevation Products(3DEP) dataset and check the box to see all subcategories. Select only the Contours (1:24,000-scale) category. Under File Formats, select GeoPackage.

../../_images/data23.png
  1. Click the blue button Search Products to see the list of matching files.

../../_images/data33.png
  1. In the results, you will find the USGS NED 1/3 arc-second Contours for Austin E, Texas file. Click on Download Link(ZIP) to download the zipped GeoPackage file ELEV_Austin_E_TX_1X1_GPKG.zip.

../../_images/data43.png

You can directly download the zip file at ELEV_Austin_E_TX_1X1_GPKG.zip

Data Source [USGS]

Nota

You can also generate contours from any DEM layer and use it instead of the contours used here. Refer to our tutorial Working with Terrain Data (QGIS3) for step-by-step instructions.

Procedure

  1. Unzip the ELEV_Austin_E_TX_1X1_GPKG.zip file on your computer. Locate the resulting ELEV_Austin_E_TX_1X1_GPKG.gpkg file in the QGIS Browser and expand it. Select the Elev_Contour layer and drag it to the canvas.

../../_images/150.png
  1. A new layer Elev_Contour will now be loaded and you should see the contour lines.

../../_images/229.png
  1. Right-click the Elev_Contour layer and choose Open Attribute Table. You will notice that the field contourelevation contains the elevation value for each contour line. This field name is important and is used in the expressions in subsequent steps.

../../_images/320.png
  1. We are now ready to style the contours. Before continuing, we must first add a color ramp suitable for coloring elevation. If you already have added the wiki-1.02 color ramp for our previous tutorial Creating a Block World Map (QGIS3), you can skip to Step 9. Click on Settings in the menubar and select Style Manager.

../../_images/49.png
  1. Click on the plus sign Add item at the bottom and select Catalog: cpt-city….

../../_images/59.png
  1. Scroll down the color ramps until you find wiki-1.02. There may be several with the same name, just click on one and click OK.

../../_images/69.png
  1. At the Save New Color Ramp window, enter the value wiki-1.02 as the Name.

../../_images/79.png
  1. Click Save to exit the window, then click Close to exit Style Manager. Now the wiki-1.02 color ramp is available to use in your projects going forward.

../../_images/89.png
  1. Now we can style the contours. Select the Elev_Contour layer and click on the button Open Layer Styling Panel. Select Simple Line to access its properties.

../../_images/99.png
  1. Switch Symbol layer type from Simple Line to Geometry Generator. This symbol layer allows us to dynamically change the geometry of features in the layer using an expression.

../../_images/1013.png
  1. In the input box, enter this expression to translate each contour line in the y-axis by its elevation value. We use the minimum() and maximum() functions to find the range of values in the contourelevation column and the scale_linear() function to scale the elevation linearly from the minimum to the maximum to the desired y-offset range between 0 to 0.2 degrees. If your contour layer is in a CRS that has units other than degrees, make sure to adjust these values appropriately. Enter the expression and click OK.

translate($geometry,0,scale_linear("contourelevation",
minimum("contourelevation"),maximum("contourelevation"),0,0.2))
../../_images/1116.png
  1. Below Geometry Generator in the symbol window, select the Simple Line layer to access it’s properties. Click on the Data define override button for Color and select the Edit menu.

../../_images/1214.png
  1. This brings up the Expression Builder dialog for the color of the line. Enter this expression to color contour lines by contourelevation value from the wiki-1.02 color ramp. We need to map the elevation values to the range expected by the color ramp (0 to 1). Instead of scale_linear function used earlier, we use the scale_polynomial function to scale the values in a non-linear way. Once the expression is entered, click OK.

ramp_color('wiki-1.02',scale_polynomial("contourelevation",
minimum("contourelevation"),maximum("contourelevation"),
0,1,0.5))
../../_images/1312.png
  1. You should see a 3D representation of the contours colored by elevation. Experiment with the coloring and scaling expressions to reveal different artistic representations of the landscape.

../../_images/1411.png

If you want to give feedback or share your experience with this tutorial, please comment below. (requires GitHub account)