This document explain who to run a local server to give, much faster, travel times, in custom types of vehicles, on a custom region.
The alternative is to use e.g. Mapbox API or OSRM API.
For this tutorial, we focus on a region (Asturias), and a mode of transportation (a car).
- Install software
- Download [or update] OSM data
- Download and run routing engine OSRM.
- Sample routing called
You'll need:
- osmconvert:
wget -O - http://m.m.i24.cc/osmconvert.c | cc -x c - -lz -O3 -o osmconvert
- osmupdate
wget -O - http://m.m.i24.cc/osmupdate.c | cc -x c - -o osmupdate
- Gdal
- docker
Establish a Bounding box around Asturias. Add a generous buffer around it, since sometimes to go to you destination you might take a better road around the region.
wget https://gist.githubusercontent.com/brunosan/d6be05f3a58136fe7c0e816c684235f0/raw/03386f6f8734320f2bca8d46a4386109afcd0488/map.geojson
ogrinfo map.geojson map | grep Extent
BBOX="-7.822265625000001,42.69858589169842,-3.8671874999999996,43.6599240747891"
#Download the latest OSM data:
DATE=`date '+%Y-%m-%d'`
mkdir data/
cd data/
wget -P data/ -O asturias_$DATE.osm "http://overpass.osm.rambler.ru/cgi/xapi_meta?*[bbox=$BBOX]"
#]*
- OPTIONAL, Convert to 05m file format (MUCH smaller)
../osmconvert -v asturias_$DATE.osm -o=asturias_$DATE.o5m
- OPTIONAL, If you want to merge updates from OSM
../osmupdate -v gambia_$DATE.o5m gambia_$DATE_tmp.o5m -b=$BBOX
First, if you don't want to use default driving properties (e.g. a car), you should also define the ones of the vehicle you will be using for the travel times (top speed, types of roads it can travel, ...). I created a quick truck.lua
based on the standard car.lua
and lowering the top speed.
Follow instructions on OSRM, these are basically 3 lines of code:
docker run -t -v $(pwd):/data osrm/osrm-backend osrm-extract -p /opt/car.lua /data/asturias_2018-03-30.o5m
docker run -t -v $(pwd):/data osrm/osrm-backend osrm-partition /data/asturias_2018-03-30.o5m.osrm
docker run -t -v $(pwd):/data osrm/osrm-backend osrm-customize /data/asturias_2018-03-30.o5m.osrm
#run the server locally
docker run -t -i -p 5000:5000 -v $(pwd):/data osrm/osrm-backend osrm-routed --algorithm mld /data/asturias_2018-03-30.o5m.osrm
OPTIONAL, run a front-end to check the results, on http://127.0.0.1:9966
docker run -p 9966:9966 osrm/osrm-frontend
To get the time and distance from a origin-destination point:
For example, the travel time, and distance from origin [14.721761,-17.418823]
to the destination [13.480448,-13.958130]
:
import requests #http framework to make Mapbox API requests for routes
import json # handle response as json
import datetime # save timestamp
url="http://localhost:5000/route/v1/driving/"
comma="%2C"
sep="%3B"
origin=[14.721761,-17.418823]
destination=[13.480448,-13.958130]
fullurl=url+str(origin[1])+','+str(origin[0])+";"+str(destination[1])+','+str(destination[0])
response = requests.get(fullurl) #do the request
response.raise_for_status() # ensure we notice bad responses
print, fullurl
# http://localhost:5000/route/v1/driving/-17.418823,14.721761;-13.95813,13.480448'
print, json.loads(response.text)['routes'][0]['distance']," meters"
# 510247, ' meters'
print, json.loads(response.text)['routes'][0]['duration']," seconds"
# 0281.4, ' seconds'
Most likely you will have a set of origins and a set of destinations, and you want to calculate the travel times from each origin to each destination. OSRM offers a matrix
function for this case.
For this example we will use OSM Taginfo to find a set of origins and a set of destinations, and then download them from Overpass turbo.
Concretely we will use all Gambian villages as sources, and all Gambian health facilities as destinations.
See [Matrix ETA.ipynb](./Matrix ETA.ipynb).