Basic Network Visualization and Routing (QGIS3)

Creating, visualizing, and managing networks is an important part of GIS. Many types of physical infrastructure such as roads, railways, utilities can be modeled as networks with lines and nodes - with properties attached to them. In this tutorial, we will learn how road networks are commonly modeled and apply some styling techniques to visualize the routing properties. We will also use QGIS3’s built-in tools for network analysis that to find shortest path between 2 points along the network.

Privire de ansamblu asupra activității

We will take a layer of road centerlines for Washington DC, visualize the connectivity and build a network to find shortest path between any 2 points in the city.

Other skills you will learn

  • How to use data defined overrides to align an arrow symbol based on line direction.

Obținerea datelor

District of Columbia government freely shares hundreds of datasets on the Open Data Catalog.

Download the Street Centerlines shared by DCGISopendata data as a shapefile.

../../_images/data1.png

For convenience, you may directly download a copy of the datasets from the links below:

Street_Centerlines.zip

Data Source: [DCOPENDATA]

Procedura

  1. Locate the downloaded Street_Centerlines.zip file in the Browser panel. Expand it and drag the Street_Centerlines.shp file to the canvas.
../../_images/110.png
  1. You will see a new line layer called Street_Centerlines added to the Layers panel. This layer represents each road in Washington DC. Select the Identify tool in the Attributes Toolbar. Click on any road segment to see what attributes are attached to it. There are standard attributes like road name, type etc. there is an attribute called DIRECTIONA. This is an import attribute for routing as it specifies whether the segment is two-way or one-way. It contains 4 different values. Two Way for two-way streets. One Way (Digitizing direction) for one-way streets where the traffic is allowed in the direction of the line (start-point to end-point) and One way (Against digitizing direction) for one-way streets where the traffic flows in the opposite direction of the line. There is also Unknown value where we will assume two-way traffic. We will now use the information in that attribute to display an arrow on streets that are one-way.
../../_images/210.png
  1. Click the Open the layer Styling Panel button in the Layers panel. Select the Rule-based renderer from the drop-down menu.
../../_images/310.png
  1. We will create a new style with a filter for only the one-way roads. Click the Add rule + button.
../../_images/42.png
  1. In the Edit rule dialog, click the Expression button.
../../_images/52.png
  1. In the Expression string builder dialog, expand the Fields and Values section in the middle-panel. Select DIRECTIONA attribute and click All Unique in the right-hand panel. The 4 values that we discussed earlier will appear. Having these values here as a reference helps when building the expression. Also, you can double-click on any value to add them to the expression.
../../_images/62.png
  1. The goal is to create an expression that selects all one-way streets. Enter the following expression and click OK.
"DIRECTIONA" = 'One way (Against digitizing direction)' OR  "DIRECTIONA" ='One Way (Digitizing direction)'
../../_images/72.png
  1. Next, change the Symbol layer type to Marker line.
../../_images/82.png
  1. Select on center point under Marker placement.
../../_images/92.png
  1. Click on the Simple marker symbol. Scroll down and pick the filled_arrowhead marker. You will see that the arrow-like symbol now appears on the one-way streets. But all of them are pointing in a single direction, whereas we know that our filter contains roads in multiple directions. We can further refine the symbols with a data-defined override for the Rotation value.
../../_images/102.png
  1. Click the Data defined override button next to Rotation.
../../_images/112.png
  1. We can put a conditional expression that returns different rotation value depending on the one-way direction. A simple 0 or 180 degree rotation works to account for the direction, but it works only for horizontal lines. To align the arrow-head perpendicular to all the lines - we need to account for the angle of the line in the expression as well. The angle_at_vertex function helps us find the angle and use it in the expression. Enter the following expression and click OK.
CASE
  WHEN "DIRECTIONA" =  'One Way (Digitizing direction)'
    THEN angle_at_vertex($geometry, 1) - 90
  WHEN "DIRECTIONA" =  'One way (Against digitizing direction)'
    THEN angle_at_vertex($geometry, 1) - 90 + 180
END
../../_images/122.png
  1. Now you will see the arrow-heads aligned to the correct road direction and angle. To keep the style uncluttered, we are choosing to display arrows only on one-way streets. Unlabeled streets are assumed to be two-way. Now that we have the network styled correctly, we can do some analysis. Go to Processing ‣ Toolbox.
../../_images/132.png
  1. Search for and locate the Network analysis ‣ Shortest path (point to point) algorithm. Double-click to launch it.
../../_images/142.png
  1. In the Shortest Path (Point to Point) dialog, select Street_Centerlines as the Vector layer representing network. Keep the Path type to calculate as Shortest. Next we need to pick a start and end point. You can click the ... button and click on any point on the network in the canvas. If you want to replicate the results in this tutorial, you can enter -76.99730092166396,38.887624846748984 as the Start point and -76.99154831062152,38.89151000569929 as the End point. Expand the Advanced parameter section. Choose DIRECTIONA as the Direction field. You must be familiar with the one-way direction values for forward and backward traffic flow. Enter One Way (Digitizing direction) as the Value for forward direction and One way (Against digitizing direction) as the Value for backward direction. Keep other options to their default values and click Run.
../../_images/152.png
  1. The algorithm will use the geometry of the layer and provided parameters to build a network graph. This graph is then used to find the shortest path between the start and end points. Once the algorithm finishes, you will see a new layer Shortest path added to the Layers panel that shows the shortest path between start and end points.
../../_images/162.png
  1. You will see that there are many possible paths between start and end points. But given the constraints of the network - such as one-ways, the result is the shortest possible path. It is always a good idea to validate your analysis and assumptions. One easy way to validate it is to use a third-party mapping service to see if their results match with the ones we derived. Here is the shortest path suggested by Google Maps between the same start and end points. As you can see the recommended shortest route matches exactly with our results - validating our analysis.
../../_images/172.png
comments powered by Disqus

This work is licensed under a Creative Commons Attribution 4.0 International License