Difference between revisions of "Network Analysis: Finding shortest path in QGIS"

From CUOSGwiki
Jump to navigationJump to search
(→‎Batch Clip tool: Updated to modernize(different in 3.20 than 2.18))
Line 64: Line 64:
 
Now we will clip all of our layers to match the extent of the AOI. Navigate to the Vector menu at the top of the QGIS user interface, put the cursor on Geoprocessing tools and select Clip. You select "Run as a Batch Process". This allows us to clip all of our desired layers at once using one tool (Please note that the Batch Processing Tool requires at least 3 input layers to function).
 
Now we will clip all of our layers to match the extent of the AOI. Navigate to the Vector menu at the top of the QGIS user interface, put the cursor on Geoprocessing tools and select Clip. You select "Run as a Batch Process". This allows us to clip all of our desired layers at once using one tool (Please note that the Batch Processing Tool requires at least 3 input layers to function).
   
[[File:BatchClip.jpg|200px|thumb|left]]
+
[[File:Clip.png|200px|thumb|left]]
   
   

Revision as of 20:36, 29 September 2021

Purpose

This project aims to introduce the various capabilities of open source softwares and their ability to solve spatial problems. The idea of open software was introduced during the GEOM 4008 course at Carleton University, and the finalized work of this course is this tutorial which was originally published in 2014, edited in 2017 and 2021. It will attempt to explain a helpful tool in Quantum GIS (QGIS), an open source software, that can identify the shortest path between two points in a network based on either distance or time. We have to use the Network Analysis tool in the processing toolbar for this tutorial and I will go step-by-step on how we can do that. QGIS is widely used in previous tutorials of the GEOM 4008 course.

Introduction

Calculating the shortest distance between two points is a very common spatial problem that appears in our daily life quite often. In ArcGIS software (not open source), this problem can be solved using the Network Analysis tool. We use an identically named tool on QGIS and it is an ideal open source alternative. This tutorial will explore that QGIS tool as a solution for finding the shortest path distance or time by calculating cumulative cost between two points in a network. Installing, activating and configuring the tool in the latest version of QGIS 2.18.13 are the most important steps to start this project. The use of the tool is to measure a cumulative cost based on length between two nodes of a network. Measurements take in consideration either the first case, when the speed limit is the same for all the roads (edges of our network) or the second case, when the speed limit differs for some selected roads.

Data

The data are provided by City of Ottawa Open Data Catalog. In order to create the network, a few files are required. Download the "Roads Centrelines" shapefile, the "Outdoor Rinks" shapefile, the "Water" shapefile and lastly the "Wards 2010" shapefile, in addition of to the "Basketball courts" , "Tennis courts", "Outdoor Wading Pools", "Cycling Network", "O-Train" "O-Train Stage 1" and their corresponding station shapefiles.

The shapefiles should be located in the same folder, within which we are going to store our work. Create a folder in your desired directory called "NetworkAnalysis" or something similar in order to store the downloaded shapefiles as well as any outputs that are generated.

Acquiring QGIS (version 3.20.3)

  • You need to install the newest version of Quantum GIS (version 3.20.3 at the time of editing this tutorial) in order to activate the road graph plugin.QGIS2017.jpg
  • You may go to this link : QGIS Download in order to download the QGIS version that is suitable to the operating system you are working on.
  • After the software is downloaded and ready to execute, double click the QGIS shortcut icon on your Desktop or search for it in the Start Menu. The QGIS window opens, and we are ready to begin.

Set up the Environment

The type of the data that we are going to work are vector data. The first step is importing the data into QGIS.

Add vector data

In Layer menu, select Add layer and choose Add Vector Layer. Another option is click the Add Vector Layer button of the toolbar.Vector.PNG.

Add each of the layers(in .shp format) individually.

Projection

All the layers are to be projected under EPSG:2951 - NAD83(CSRS) / MTM zone 9 projection. Right click on the layer, select Properties, you then go into Source, search and select the appropriate projection. Click apply to see your changes and repeat with your 4 layers. This process can done for all the layers that we are working with, however they should all have the same CRS automatically.

Source2.JPG

.











Area of Interest (AOI)

The project is focused on determining the shortest path between the outdoor rink at Brewer Park and one at Jack Purcell Park in downtown Ottawa. By creating an area of interest, or AOI, we can effectively reduce the amount of useless data in the output. In larger data sets, creating an AOI may also reduce processing times.

  • In Layer menu, choose Create Layer, then New Shapefile layer and the New Vector Layer window will show up. The AOI should be a polygon.
  • Click the AOI layer.
  • Click the Toggle on button to start editing the layer. Alternatively, right click on your new layer in the Layers Panel on the left hand side and toggle editing "On" Toggle1.PNG
  • Zoom to the scale that meets your needs by holding the CTRL button and using the scroll wheel on your mouse. To create the polygon, find the polygon create tool begin by clicking in each of the four corners of your current zoom extent. This ensures that your AOI has 90 degree angles and crisp lines. On the last point of the polygon, right click to finish creating it.
AOI2.PNG

Batch Clip tool

Now we will clip all of our layers to match the extent of the AOI. Navigate to the Vector menu at the top of the QGIS user interface, put the cursor on Geoprocessing tools and select Clip. You select "Run as a Batch Process". This allows us to clip all of our desired layers at once using one tool (Please note that the Batch Processing Tool requires at least 3 input layers to function).

Clip.png








  • Input vector is the layer we want to clip: Roads
  • Clip layer is the AOI that we just created
  • Output shapefile is specified by us. Refer to the file folder you created to hold all shapefiles and outputs earlier in the tutorial.

Symbology

To improve the visual appeal and usability of the map that we create, we should symbolize the different layers to meet our needs instead of staying with the defaults. Firstly, we will symbolize roads based on their type (Street, Boulevard, Parkway, Highway etc.)

  • Right click on the clipped roads layer
  • Select Properties
  • Choose Style and at the top of the Styles window where it currently says Single Line, select Categorized and Column: ROAD_TYPE
  • Double click on the symbols of each roads type to edit them further if you would like. In the example screenshot below, I increased the thickness of the Highways and changed to color to a deep red to make them, stand out from other roads.


We can also label the ClippedRinks file in order to see the names of the rinks.

  • Right click on the clipped rinks layer
  • Select Properties
  • Choose "Labels" and at the top of the Labels window, select "Show labels for this layer", using "PARKNAME" as the label field.

The results should look similar to the screenshot below.

SymbolizedClipped.jpg

Road Graph Plugin

The Road Graph Plugin is a C++ plugin for QGIS that calculates the shortest path between two points on any polyline layer and plots this path over the road network.

Main features:

   - Calculates path, as well as length and travel time.
   - Optimizes by length or by travel time.
   - Exports path to a vector layer.
   - Highlights roads directions (not used in this tutorial).


Activate the Tool

Plugin.PNG















To activate the Road Graph plugin:


  • Start the Plugin Manager by clicking on the QGIS main window’s menu item PluginsManage and Install Plugins.
  • A dialog window appears. Write road on the search tab
  • It will automatically appear the road graph plugin and check it on. (this plugin is already installed in the repository, when we installed the software in our computers )
  • Go back to your work window
  • This panel will appear in your interface
Panel.PNG












  • If the plugin doesn't show automatically in your interface, go to View ‣ Panels and ensure that Shortest path has a check mark next to it.


Configure the Tool

  • Go to Vector menu
  • Select Road graph
  • Select Settings
Config.PNG













Time Unit and Distance Unit we leave as default in hour and kilometer. Topology tolerance: In the case of a gap between two lines that should be treated as continuous, a topology tolerance may be applied, for example 10m. In our case we left as default 0 because it can be assumed that a municipal dataset should be almost entirely topology error-free.

For the Transportation Layer, we choose the ClippedRoads layer created earlier.

For the direction field, are able to use the FLOW field in the attribute table. For Forward direction, specify "FT", and use "TF" for reverse direction. These abbreviations mean "From - To" and "To - From" respectively, meaning From a specified point to an edge (in this case, a road polyline) and vice-versa for TF. Leave the value for two-way direction blank. This allows the tool to factor one-way roads into its calculations, instead of sending you on an impossible route through the city.

Choose the speed limit value under the Default Settings tab. For this example the speed was chosen 60 km/h as shown in Figure below.

FTTF.jpg
















Use the Tool

Suppose you are playing hockey at Brewer Park after class, but your friend wants to meet at Jack Purcell rink downtown. We need to calculate the shortest possible route in length from Brewer Park to Jack Purcell Park . In the plugin panel, click on the Capture Point button next to the Start field:

St.PNG






  • Click point that represents Brewer Park as the start point.
  • Use the Capture Point button next to the Stop field and capture the point representing Jack Purcell Park.




  • Click on the Calculate button to see the solution:
Inputs.jpg













The result for the shortest path is shown below. This is a simple case, where the speed mode is the same for all the streets, 60 km/h.

ShortestRoute1.jpg












Using Criteria

What happens when different streets in the network road have different speed limits?

The idea is to set different speed limits for some of the selected streets by us. In this case we had to add a new field in the roads layer and call it Speed.

Create New Field

  • Right click on the roads layer.
  • Open attribute table and select the Toggler button to set on the edit mode . Toggle1.PNG
  • After that click the New Column icon.NewC.PNG
  • Call this new column SPEED, and give it the type Whole number (integer) with a width of 3.

Queries

Now we run a Field Calculator expression within the road layer that will specify the speed limit 60 km/h for the Streets, 50 for Avenues, 80 for Roads and 40 km/h for the rest of the road types.

Open up the attribute table for the RoadsClipped file and toggle on "Editing" in the top left corner. Then, open up the Field Calculator.

FieldCalculatorButton.jpg








From here, we can write a simple expression that will populate the SPEED field created earlier with our desired values. Enter the expression into the Field Calculator as is shown below, or specify your own values for different road types. The speeds defined here are totally arbitrary, and are simply meant to show the functionality of the field calculator to remove the need for individually defining each street's speed limit.


FieldCalc.jpg











After assigning the speed limits for each selection we save the edits by clicking the save button and then click the toggle button again to turn off the edit mode.Save.PNG

Use the tool after criteria is done

  • Check the Vector ‣ Road graph ‣ Road graph settings to ensure that it’s set up as explained previously in this tutorial, but with the Speed value set to the SPEED field you just created.
  • In the Shortest path panel, click the Start point button.
  • Set the starting point to where the point of Museum of Nature is located.
  • Set the end point to the Museum of War location.


  • In the Criterion drop-down list in the Shortest path panel, select Length.
  • Click Calculate. The route will be calculated for the shortest distance:
SpeedL.PNG











Notice the values of Length and Time in the Shortest Path panel.



And this is the result graphically of the shortest path in distance

LENGTH.jpg













  • Set the Criterion to Time.
  • Click Calculate again. The route will be calculated for the shortest time:


Time.PNG












  • You can switch back and forth between these criteria, recalculating each time, and note the changes in the Length and Time taken. In this particular example, the only difference between length and time is a single right turn.


The Shortest Time path result as shown below

TIME.jpg













  • The last operation is to export the output as a shapefile, by clicking the Export button on the Shortest path panel.

Conclusion

The Road Graph plugin for QGIS is a useful utility for solving any cost-distance query that one might have. The tool itself is very user-friendly and intuitive, but does require some tweaking of settings and the generation of a new field in some cases. One could verify that they set up the tool properly by checking if the outputs for the Length and Time are the same, indicating that the SPEED field wasn't created properly. This tool is slightly limited in the sense that if we would like to calculate the distance between two points in miles or the speed field is in miles/hour, the tool will not give us solution, because by default it supports only kilometers and meters. One way to solve this would be to convert miles to meters or km through another field calculator expression.

Future work

Future work might be creating Drive Time isochrones in QGIS. Site analyses can benefit greatly from using “drive-time” isochrones to define the study area. In ArcGIS this is called Service Area. It might be helpful to check pgRouting open source software to create the nodes network, where each of them would define a start and an end point for different segments that create the network. Check in for more details: Drving time isochrones.

References