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.


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.


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.

  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 file downloaded which is used for this tutorial.


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

Data Source: [HYDROSHEDS]


  1. Locate the 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.

  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.

  1. Switch from Single Symbol to Rule-based.

  1. Double-click on the default rule to enter the Edit Rule dialog.

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

  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
  1. The map now shows the filtered rivers with large upland areas. You can adjust this threshold based on the scale of your region.

  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.

  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.

  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.

  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.

  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.

  1. You should now see the completed river network of South America.


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