🆕pgRouting
Instructions to get you started with using OS MRN in pgRouting.
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide routing and other network analysis functionality.
What you need
QGIS provides a few useful options for pgRouting and the handling of OS MRN data:
Part of the QGIS install gives access to a command prompt that comes with some spatial functionality for data management without needing much setup (this will be covered in more detail below).
It can be linked in with the PostgreSQL database to allow for visualising existing tables and pgRouting results.
It gives access to pgRouting in plugin form, allowing routes to be calculated within QGIS using those database tables.
Introduction to graph data and routing
A graph data structure is made up of nodes and edges. With OS MRN, these are represented by the mrn_ntwk_transportnode
and mrn_ntwk_transportlink
layers.
Nodes are points that represent junctions connecting one or more paths on a network. This is where a decision is made on where to turn next (or stop if you are at the end of the route). All nodes must connect to an edge.
Edges connect two or more nodes, and represent the distance between them. They can be directed (for example, one-way restrictions) or undirected (any direction allowed). Their default direction will be in the direction the edge was digitised, from startpoint to endpoint.
The distance is a reflection of the cost to travel along that edge. At the base level, this would be the length of the edge (in metres), but can be extended with maths to incorporate things like the speed of travel along the edge.
Using other data you may have, cost can be extended even further, for example, to take into account factors that might make a traveller use a particular route more (such as scenery, traffic, points of interest and gradient).
With pgRouting, these costs can be applied to each edge in a particular direction, that is, in direction (the direction of digitising) and in reverse direction. This enables the user to have more control on what the routing algorithms look for.
Routing algorithms
Routing algorithms are a set of rules used to determine the best path between two or more points. Some of the common ones used are:
pgr_dijkstra
– The classic shortest path algorithm, from one node to another, using a directed or non-directed graph.pgr_drivingdistance
– Using Dijkstra, finds all nodes that are reachable within a set distance using a directed or non-directed graph. Produces results that can then be visualised as an isochrone polygon.pgr_trsp
(turn restriction shortest path) – Runs similar to Dijkstra, with the addition of using restriction information. With OS MRN, this restriction data is available in themrn_ntwk_turnrestriction
layer. These turn restrictions reference the edges from the network layer and are used as a lookup as the algorithm works.
Checking that PostGIS and pgRouting are enabled
In more recent versions of PostgreSQL, both PostGIS and pgRouting are enabled as standard, ready to be used.
To check this:
Load pgAdmin4 (or your chosen database explorer).
Highlight your database.
Open a SQL window and run:
In the example above, PostGIS was already enabled. However, in the example below, pgRouting wasn't enabled, but is now ready to be used within the selected database:
Loading the GeoPackage
The easiest way to load the GeoPackage (GPKG) into PostgreSQL via ogr2ogr is to use the OSGeo4W Shell that is provided with the QGIS install package:
Before loading the GPKG, check that environment variables have been set up so that PostgreSQL will be recognised by the command prompt. Admin permissions are required to edit these variables.
Add the folder location of your PostgreSQL install to the
User->Path
andSystem->Path
variables.Once you have done this, open the OSGeo4W Shell (as shown above), and type in
ogr2ogr
. If everything is setup correctly, you should see:
To then load the OS MRN GPKG into your PostgreSQL database, navigate to the location of the file, and type the following:
You could alternatively use the following command (where /vsizip/
is a file handler that allows reading ZIP archives on-the-fly without decompressing them beforehand):
This will load the three layers of the OS MRN GPKG into the defined database schema, and create the relevant spatial indexes.
You can now add some further attribute indexes for the unique identifiers:
With all the indexes added, you can create a basic edge table on which to start routing:
Loading the Protocolbuffer Binary Format
osm2po is a cross platform tool written in Java that can be used to parse OS MRN PBF data and make it routable in pgRouting.
To find out if you have Java installed, open PowerShell / Command Prompt (Windows) or Shell (Linux) and type:
Press Enter. If Java is installed, you should see a few lines of text detailing which version you have installed. If you get something along the lines of
"java is not recognized ..."
, please download JVM [or OpenJDK] from https://adoptium.net/en-GB/.Once osm2po is downloaded and extracted, open and edit the config file to satisfy your usage requirements (for example, change the
wtr.finalMask = car
line (use Ctrl+F to search the file) tocar,foot,bike
travel types).Uncomment the following line (this helps with getting the topology into a format pgRouting likes):
When this is done, and assuming Java is installed and accessible, open a command prompt and navigate to the location of the .pbf file, and then run the following:
This will create the .sql file version of the .pbf file.
Import the .sql file version of the .pbf file to the PostgreSQL database using the following:
In this newly created table, each record should have a source and target node, and an integer classification. Cost and reverse cost columns are also applied. Cost in this case is worked out using distance (in km) and speed (kmh) defined in the config file.
Running a test route with pgRouting (Dijkstra)
The mandatory things pgRouting needs to be able to route with are a routing table, start node and end node. Once the routing table is created, pgRouting can be run straight away.
For the following example, OS MRN sample data has been used, with the GPKG data being loaded into PostgreSQL via ogr2ogr (with the SQL run to create basic edge table). A couple of waypoints have been selected in Exeter, and a sample Dijkstra route query can look like the one below:
This route query is made up of three parts:
First, the 'start' section finds the start node to use from the network that is closest to the point given. This could be fed in to the query in multiple ways, such as clicking on a web map, or using a plugin.
Second, the 'destination' section finds the start node closest to the destination point given.
The final part includes the
pgr_dijkstra
function, reading in the start and destination ids, and returns a merged geometry of links that form the route.
When visualised in QGIS, the result should look similar to this:
It should be noted that the above route query does not take any of the additional OS MRN attribution into account. This means that every edge will be considered when calculating the shortest path. We can therefore apply some additional logic in the SELECT
statement to facilitate either vehicular or pedestrian routing:
Vehicular routing – Determines the best vehicular route where the edges are deemed to be roads.
Pedestrian routing – Determines where the edges can be traversed by foot and whether there is designated infrastructure for walking.
Last updated