Styling a River Network with Expressions (QGIS3)

Contributed by: Steven Kim

In the previous tutorial Creating a Block World Map (QGIS3) we used expressions for scaling values and apply a color ramp. We build on those concepts and learn how to use expressions to visualize rivers in a popular style.

Nota

This tutorial focuses on the use of expressions for styling. You can check out our another tutorial Creating a Colorized River Basin Map (QGIS3) that creates a different version of the map shown here using a tools-based workflow.

Overview of the task

We will use expressions to filter and style South American rivers - with line widths representing upland area and colors representing basin id from HydroRIVERS.

../../_images/output2.png

Get the data

The HydroRIVERS dataset provides seamless global coverage of consistently sized river reaches, supported by geometric information that allows for basic analysis of river network topology such as stream connectivity and distances. The data is a subset of the comprehensive HydroATLAS package from WWF. Let’s download the shapefile of rivers for South America.

  1. Visit the HydroRIVERS homepage.

../../_images/data118.png
  1. The page has links for global and regional extracts of HydroRIVERS in shapefile and geodatabase formats. Scroll down and click on the download link for the South America shapefile. You will see the HydroRIVERS_v10_sa_shp.zip file downloaded which is used for this tutorial.

../../_images/data25.png

For convenience, you may directly download a copy of the above layer from below: HydroRIVERS_v10_sa_shp.zip

Data Source: [HYDROSHEDS]

Procedure

  1. Locate the HydroRIVERS_v10_sa_shp.zip file in the QGIS Browser and expand it. Select the HydroRIVERS_v10_sa_shp/HydroRIVERS_v10_sa.shp file and drag it to the canvas.

../../_images/1141.png
  1. A new layer HydroRIVERS_v10_sa will now be loaded in QGIS and you should see a map of rivers in South America. Click on Open Layer Styling Panel.

../../_images/2109.png
  1. Switch from Single Symbol to Rule-based.

../../_images/360.png
  1. Double-click on the default rule to enter the Edit Rule dialog.

../../_images/430.png
  1. To filter using an expression, click the Filter ε. This will open the Expression Builder.

../../_images/528.png
  1. Select the Expression box on the left to input the expression as follows and click OK. This expression selects river features with an upland area equal to or greater than 100 square km.

"UPLAND_SKM" >= 100
../../_images/627.png
  1. The map now shows the filtered rivers with large upland areas. You can adjust this threshold based on the scale of your region.

../../_images/727.png
  1. Now that we have used an expression to filter features, let us use an expression to color each river segment based on the field MAIN_RIV. Scroll-down to the Symbol options in the styling panel and select Simple Line to bring up styling options such as color, stroke width and more. Click on Data define override button for the Color and select Edit… on the menu.

../../_images/826.png
  1. This brings up the Expression Builder dialog for color. Enter the expression below. This expression combines the functions ramp_color() and scale_linear() to select colors from the spectral color ramp using the range of MAIN_RIV id values. Click OK to exit the dialog.

ramp_color('Spectral',scale_linear("MAIN_RIV",60000001,61620963,0,1))
../../_images/927.png
  1. At the Edit Rule dialog, click on Data define override button for Stroke width and select Edit… on the menu. Enter the following expression. The expression scales line width based on the upland area of each river segment, with a minimum of 0.01 mm to a maximum of 0.1 mm for rivers with an upland area equal to or larger than 10,000 sqkm.

scale_linear("UPLAND_SKM",100,10000,0.01,0.1)
../../_images/1033.png
  1. Click OK to exit the dialog. The river lines will now be rendered in different widths based on their upland area. The expression needs to be evaluated for each feature and thus the rendering may take some time.

../../_images/1142.png
  1. Let’s change the background color to black to contrast with our color choices. Navigate to Project ‣ Properties ‣ General. Click on the down arrow and select black. Click OK.

../../_images/1236.png
  1. You should now see the completed river network of South America.

../../_images/1334.png

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