Page cover

Getting started with OS MasterMap Highways and pgRouting

OS MasterMap Highways network is a detailed and accurate navigable road network data set for Great Britian. It can be used in a variety of routing applications and these notes help get started with the data in the open source application pgRouting.

PgRouting extends the spatial capabilities of the PostGIS extension to the PostgreSQL database to provide routing functions. These notes are based on the work of Tim Manners and Ross MacDonald which has been shared on GitHub and provide an overview of how to create a network data set for use with pgRouting.

Software Requirements

PostgreSQL with the PostGIS and pgRouting extensions (pgRouting is packaged with PostGIS from version 2.1)

Data Requirements

OS MasterMap Highways Road and Asset Management Information (RAMI) – this gives the additional features required for routing although it is possible to build a basic network without these

Tables required:

Base data

  • RoadLink – network line geometry and attribution describing the link.

Routing information used to define where you can and cannot travel

  • Accessrestriction

  • Restrictionforvehicles

  • Turnrestriction

Depending on how the data has been translated from the GML this may be one table for each of these or separate tables for the different elements of each. The instructions work best if you have separated the network references into separate tables.

Before beginning you should also consider how much data you need, the bigger the area then the longer it will take to build and perform analysis.

Building the network

These notes are for guidance, table and field names should be checked to ensure they match.

The first statement generates a node lookup using the startNode and endNode references.

Where appropriate, it introduces additional nodes using the grade separation to ensure the real-world physical separations between RoadLinks are handled accordingly.

The second statement creates the directed graph which can be used for routing.

It utilises the node lookup to generate the required ‘source’ and ‘target’ integer fields; along with adding 'cost' and 'reverse_cost' fields for one ways (the wrong way has a very high cost).

It also uses the localid field in the data to create an id for each link, if you data does not contain this you can add it as serial.

Adding restrictions

The previous steps create a network which can be used for routing, however this does not take into account more complex turn features. The next step is to add these to your network.

Turn restrictions

No Turns

The Highways dataset provides tables of turn restrictions with a reference to the road links, the type of restriction and a turn sequence number. The turn restriction table is joined to the road link table and is used to generate the turn restrictions in pgRouting format.

First, we create a view of the links involved in the turn restriction. We select the FROM link with a sequence of 0 and the To link with a sequence number of 1 where the restriction type is NO TURN. The restrictions are represented in the GML as ordered pairs.

N.B. When translated a sequence attribute may be added with values of 0 and 1 to represent the order of the links in the restriction. However you may have translated your link references into an array containing a pair of values, the first should be accessed as 0 and the second 1.

Add the restriction type to the network reference table (this step is required if the network references are stored in a separate table to the turn restriction).

Mandatory turns

Mandatory turns are modelled in the dataset as turns which are allowed, giving an entry link and an exit link which, together with the associated node that make up the mandatory turn. To create a restriction to enforce the mandatory turn you need to select all the other links in the turn and restrict access to them.

No Entry

No entries represent links where access is forbidden and are held in the Access Restrictions table.

Build the restrictions table

This step takes the view previous created to build one table of all restrictions.

Example usage of pgRouting

A Driving Distance query finds everywhere along the network that can be reached within a fixed distance, in this example 1.5km, from a chosen starting point. The query uses the cost and reverse cost attribute to manage which networks elements are included in the query. In this example we have used the distance or time cost which were specified during edge table set up. The source node id is the point on the network from which the driving distance is calculated and can either be identified by querying the data in a GIS or in the database.

Create a Polygon Isochrone for your results

The results of this query are presented as a list of roads that are reachable in the time or distance specified. However you may want to display isochrones for your output and for use in service area analysis. This query takes the output and creates a polygon that represents its boundaries using a concave hull.

The results can then be displayed graphically in a GIS.

isochrone map created from pgRouting

Further information

https://github.com/mixedbredie/highways-for-pgrouting

https://github.com/tmnnrs/highways-network-pgrouting

PostGreSQL - https://www.postgresql.org/

PostGIS - http://www.postgis.net/

pgRouting - https://pgrouting.org/

http://planet.qgis.org/planet/tag/pgrouting/

Last updated

Was this helpful?