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:
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 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;
CREATE EXTENSION postgis;
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
Note, this is orig_lon, orig_lat; dest_lon, dest_lat (origin then destination).
Compare it with Google.
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.
./access_query_osrm/configcreate a new
.yamlfile by following the format of
template.yaml. This will ensure the correct OSM network data is downloaded, the OSRM server is initiated and queried.
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
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.