The fast calculation of network distance can be highly useful. For example, the accessibility of community services is becoming a proxy for community resilience and has opened the door to a large range of future research opportunities in urban planning, disaster resilience, and climate mitigation.

This post will outline the steps required to set up your own local server to evaluate proximity.

To see where we have used this work in the past, check out the following links:

  1. Accessibility and community wellbeing
  2. X-Minute Cities: Infrastructure investment
  3. Identifying critical transport links and services
  4. Multi-criteria spatial optimisation: Maximising proximity and minimising hazard exposure

Initialising and querying locally from the OpenSourceRoutingMachine (OSRM)

OSRM is an open-source equivalent of Google maps and its distance API. Effectively, if you provide OSRM with a street network (usually from OpenStreetMap, OSM) and a destination and origin pair, it will determine the fastest route and report the distance and time taken. This can be done with either walking, biking, or driving, and modifying the assumptions for different transport profiles is possible. More information on OSRM can be found here.

Environment & Directory Structure

While we operate the OSRM within a remote Linux server, it can be hosted on local windows or mac environments too.

First, clone our GitHub code onto your computer. Then create a directory for the OSM data (./osm_dir); this could be within the cloned directory, but if you plan to use the OSM data for multiple projects, consider putting it somewhere common to them all.

Install Docker

Install Docker onto your system. Here are the instructions.

Then pull the DockerHub image of the OSRM-backend. Further details are here.

PostGreSQL and PostGIS

There are many options for dealing with the results of OSRM queries. The code we’ve provided uses PostGreSQL+PostGIS. To use this, you can pull the PostGIS image from the DockerHub:
docker run --name $db_name -p localport:5432 -v data:/var/lib/postgresql -e POSTGRES_PASSWORD='$yourpassword' -d postgis/postgis

Then you need to enable PostGIS:

postgres sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
CREATE DATABASE $db_name;
\c $db_name;
CREATE EXTENSION postgis;
\q

Our code

Our code has been written to automatically download the OSM data and initialise the OSRM server.

It is set up to query a large number of origin-destination pairs.

Test the OSRM server

If you have an OSRM server running, you can test it through a URL request (either in the browser or command line).
It should be of this form  http://localhost:5000/route/v1/walking/-75.556521,39.746364;-75.545551,39.747228?overview=false .
Note, this is orig_lon, orig_lat; dest_lon, dest_lat (origin then destination).

Compare it with Google.

Street network

As stated, the code automatically downloads the OSM network data. You will need to update the config.yaml file to tell the code what region you want to download. Check http://download.geofabrik.de/ for the right name formatting.

Additional notes

Configuration file
Within ./access_query_osrm/configcreate a new .yamlfile by following the format oftemplate.yaml. This will ensure the correct OSM network data is downloaded, the OSRM server is initiated and queried.

Transport Profiles
Profiles refer to the transport mode and inform OSRM the speed someone travels over surfaces etc. and penalties for intersections. Be sure that you are using the correct profile. We have set these in the config.yaml.

Origins & Destinations
New origins and destinations can be fed into the query process by adding the file path of the respective shapefiles within the config.yaml. Adding the file path will allow the main.py function to upload the files into PostgreSQL.