10 Shortest Path Analysis: QGIS methods
Calculating the shortest distance between two points is a very common spatial problem that appears in our daily life quite often. In this hands-on exercise, you will learn how to use QGIS tool as a solution for finding the shortest path distance or time by calculating cumulative cost between two points in a network.
By the end of this hands-on exercise, you will be able to:
- extract, clean and process network data from roads layer of OSM data, and
- performing shortest path analysis using QGIS build in network analysis tools
10.1 Setting the scene
10.1.1 The Task
In this hands-on exercise, you are tasked to derive an accessibility to eldercare centre map of Singapore.
10.1.2 The Data
Three major data sets will be used in this hands-on exercise. They are:
- Master Plan 2019 Subzone Boundary from URA. This data can be downloaded from data.gov.sg.
- Eldercare services from Ministry of Social and Family Development. This data can be downloaded from data.gov.sg.
- Roads data from OpenStreetMap (OSM) data sets. OSM data sets can be downloaded from Geofabrik’s free download server.
Note: I recommend this download site instead of bbbike because I found out that the quality of the data provided by Geofabrik is better than bbbike.
10.2 Data Preparation
10.2.1 Start a new QGIS Project
DIY: Using the steps you had learned in previous hands-on exercise, start a new QGIS project. Save the project and give it a name (i.e. Hands-on_Ex09).
Reminder: Ensure that svy21 projection system is used.
10.2.2 Preparing base layer for the study area
DIY: Download Master Plan 2019 Subzone Boundary from data.gov.sg and import it into QGIS. Then, save the GIS layer into GeoPackage format. Name the output layer as
mpsz2019
.
10.2.3 Preparing eldercare layer
DIY: Download eldercare services data from data.gov.sg and import it into QGIS (Ideally, the shapefile version should be used). Then, save the GIS layer into GeoPackage format. Name the output layer as
eldercare
.
10.2.4 Extracting and preparing road network layer
10.2.4.1 Selecting all road network with the study area
DIY: Using appropriate QGIS function(s) you had learned from previous hands-on exercise, clip the road network with Singapore mainland (excluding outer insland, Pulau Tekong and Pulau Ubi). Save the GIS layer into GeoPackage format. Call the layer
all_roads
.
10.2.5 Extracting motor vehicle road network
DIY: Using appropriate QGIS function(s) you had learned from previous hands-on Exercise, extract motor vechicle road network (i.e. motorway, motorway_link, primary, primary_link, secondary, secondary_link, tertiary, tertiary_link, residential, trunk and trunk_link) from the all_roads layer. Save the GIS layer into GeoPackage format. Name the layer
roads
.
Be warn: The original data is in wgs84. For the purpose of network analysis, all network should be in projected coordinates system.
Reminder: Remove all the source data from QGIS project before continue to the next section.
10.3 Shortest Path Analysis
In this section, you will perform the following tasks:
- Identifying useful attribute(s) from the
roads
layer by using QGIS Identify Features tool, - Visualising topological properties of
roads
layer by using styling methods of QGIS, and - Determining shortest path from a demand point (ie. HDB block) to a supply point (i.e. eldercare centre).
10.3.1 Working with Identify Features tool
In this section, you will explore the attribute information of road
layer by using QGIS.
DIY: Zoom to Punggol area as shown in the screenshot below.
Switch off all other layers except
roads
andeldercare
.At Layers panel, click on
roads
layer to make it active.From the menu bar, click on Identify Features icon.
Hover the mouse over a road segment and click on it.
The Identify Results dialog window appears.
Your screen should look similar to the screenshot below.
There are many information can be derived from the dialog window. For the time being, let us focus on oneway field. It specifies whether the road segment is two-way or one-way. If it is one-way, the flow direction will be given.
There are three different values. B for two-way. F means that only driving in direction of the line string is allowed. T means that only the opposite direction is allowed.
- Close the Identify Results dialog window.
10.3.2 Working with QGIS’s Styling methods
Now, we will use the information in oneway field to display an arrow on one-way streets
- From the Layers panel, click on Open the Layer Styling Panel button.
The Layer Styling panel appears on the right as shown below.
- Select Rule-based from the drop-down menu.
We will create a new style with a filter for only the one-way roads.
- Click on the Add rule + button.
- From the Edit rule dialog, click on Expression button.
The Expression String Builder dialog window appears.
We are going to build an expression that select all one-way streets.
- Click on the black triangle in front of Fields and Values to expand the section.
Double-click on
oneway
field.Click on = button.
At the Expression pane, complete the expression as shown below.
- Click on Ok button.
Notice that Filter option has been updated.
Next, we are going to change the Symbol layer type to Market line.
Notice that the road lines are marked with red markers now.
- At the Marker placement pane, click on the radio button in front of
on center point
.
Notice that the markers are placed at the centre of each line segment.
- At the Symbol pane, click on
Simple marker
.
- Scroll down and select filled_arrowhead marker.
Notice that arrow-like symbol now appears on the one-way streets as shown below.
You can close Layer Styling dialog window now.
- At the Layer Styling panel, click on the cross located at the upper right of the panel.
10.3.3 Working with build in shortest path tool
Now, we are going to determine the shortest path from Block 619B to eldercare centre located at Block 211B (lower left corner of the view window)
From the menu bar, click on Processing -> Toolbar.
At the Toolbox search pane, type Shortest path.
Your screen should look similar to the screenshot below.
- Double-click on Shortest path (point to point) of Network analysis.
Shortest Path (Point to Point) dialog window appears.
For Vector layer representing network, select
roads
from the drop-down list.For Path type to calculate, select Shortest from the drop-down list.
For Start point, manually click on the Block 619B.
For End point, manually click on Block 211B.
For Direction field [optional], select
oneway
from the drop-down list.For Value for forward direction [optional], type F.
For Value for backward direction [optional], type T.
The Shortest Path (Point to Point) dialog window looks similar to the screenshot below.
We will keep the rest of the selection as default for the time being.
When you are ready,
- click on Run button.
Reminder: Read the log before closing the dialog window.
- Click on the Close button to close the dialog window.
Notice that a temporary layer called Shortest Path
is added on Layers panel and is display on the map view.
The default line thickness of Shortest Path
is too thin to view.
DIY: Using the steps you had learned in previous hands-on exercise, increase the thickness of the line symbol.
You screen should look similar to the screenshot below.
DIY: Using the steps you had learned from previous hands-on exercise, save the temporary Shortest Path layer into GeoPackage format. Name the layer
Shortest_path
and add it onto the map view.
DIY: Remove the temparory layer from the Layers panel.