OSRM? Let's run it on Docker!
I was introduced to certain aspects of GIS by one of my mentors. OSRM was one part of which was discussed. I was made aware by my mentor about the usefulness of OSRM. This made me try and implement it. This was one of the problems I encountered while trying to build an OSRM image.
Why is it better to use OSRM over google map API?
The cost is the main factor that will be considered for such a comparison. OSRM is a powerful open-source routing engine to solve the shortest paths in road networks. It's quite fast too!
Many APIs are available in OSRM. I will not be talking about OSRM APIs here. You may check the below link to see what OSRm has to offer!
http://project-osrm.org/docs/v5.22.0/api/#general-options
This article is about an issue I encountered while running OSRM on Docker. After following many medium articles on OSRM and docker, I could not get the image running.
I will be explaining how I got it to run by using the OSRM route API.
Step 1: Download OSM.PBF file for your required region.
First, you need to obtain extracts for the territory you need. I considered Sri Lanka.
wget https://download.geofabrik.de/asia/sri-lanka-latest.osm.pbf
Step 2: Pre-process the extract with the car profile.
docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-extract -p /opt/car.lua /data/sri-lanka-latest.osm.pbf
I would like to explain what this does. ${PWD}:/data
creates a directory /data
and makes it the current working directory ${PWD}.
The file /data/sri-lanka-latest.osm.pbf inside the container refers to ${PWD}/data/
sri-lanka-latest.osm.pbf.
We use osrm-extract to a graph out of the OpenStreetMap base map.
Step 3: Partition and customize.
docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-partition /data/sri-lanka-latest.osrm
docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-customize /data/sri-lanka-latest.osrm
We use osrm-partition to partition the graph recursively into cells and osrm-customize customizes these cells by calculating routing weights for all cells.
Step 4: Run the image.
docker run -t -i -p 5000:5000 -v "${PWD}:/data" osrm/osrm-backend osrm-routed --algorithm mld /data/sri-lanka-latest.osrm
Using osrm-routed we are spawning the http server. Algorithm mld or known as Multi-Level Dijkstra is used to run to specify the algorithm.
The docker container should be up and running. You can confirm by using the URL.
http://127.0.0.1:5000/route/v1/driving/13.388860,52.517037;13.385983,52.496891?steps=true
or use a curl request.
curl "http://127.0.0.1:5000/route/v1/driving/13.388860,52.517037;13.385983,52.496891?steps=true"
The response should be as following.
{“code”:”Ok”,”routes”:[{“geometry”:”qugy@{`deN??”,”legs”:[{“steps”:[{“intersections”:[{“out”:0,”entry”:[true],”bearings”:[285],”location”:[79.65214,9.547294]}],”driving_side”:”right”,”geometry”:”qugy@{`deN??”,”mode”:”driving”,”duration”:0,”maneuver”:{“bearing_after”:285,”location”:[79.65214,9.547294],”bearing_before”:0,”type”:”depart”},”weight”:0,”distance”:0,”name”:””},{“intersections”:[{“in”:0,”entry”:[true],”bearings”:[105],”location”:[79.65214,9.547294]}],”driving_side”:”right”,”geometry”:”qugy@{`deN”,”mode”:”driving”,”duration”:0,”maneuver”:{“bearing_after”:0,”location”:[79.65214,9.547294],”bearing_before”:285,”type”:”arrive”},”weight”:0,”distance”:0,”name”:””}],”distance”:0,”duration”:0,”summary”:””,”weight”:0}],”distance”:0,”duration”:0,”weight_name”:”routability”,”weight”:0}],”waypoints”:[{“hint”:”oK4JgKKuCYBuAAAAAAAAACoBAAAAAAAAm62SQgAAAADbR0ZDAAAAAG4AAAAAAAAAKgEAAAAAAAAxAAAALGW_BB6ukQA8TMwArVghAwUA3xHVjwwm”,”distance”:7919871.540611,”name”:””,”location”:[79.65214,9.547294]},{“hint”:”oK4JgKKuCYBuAAAAAAAAACoBAAAAAAAAm62SQgAAAADbR0ZDAAAAAG4AAAAAAAAAKgEAAAAAAAAxAAAALGW_BB6ukQD_QMwA-wkhAwUA3xHVjwwm”,”distance”:7918747.74897,”name”:””,”location”:[79.65214,9.547294]}]}
I hope this article make your day. Thanks and have a nice day!
Github : https://github.com/anjulapaulus