Kick Start — Open Source Routing Machine Set-Up
cheerio google map, greeting open-source map
So this blog will be about Setting up OSRM (Open Source Routing Machine) with demo video and explaining HTTP requests
What is OSRM?
OSRM in Open Source Routing Machine. In simple words, you can create your map application like google map using OSRM (FOR FREE!!)
Why OSRM?
- Powerful and efficient engine routing machine for solving shortest road tracks.
- It’s fully open-source and you can host it on your computer without paying for a license
So say bye to google map and create your own map company by using OSRM
How to use it?
I tried OSRM set-up without docker and end up with frustration
Step 0: Docker set-up (If not before)
Ubuntu users just go with sudo apt-get docker
. Yes, your docker is ready.
Windows users have fun here and Mac user have fun here
Step 1: Getting your territory OSM.PBF
file
- Just get from here
Or
http://download.geofabrik.de/your_needed_territory-latest.osm.pbf--------------------------------------------------------------------http://download.geofabrik.de/europe/germany/berlin-latest.osm.pbf
Step 2: Pre-processing
This part is extracting your downloaded osm.pbf
. This will help to start a routing engine HTTP server on port 5000 (For sample)
(Ubuntu-users just add sudo
before docker command)
docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-extract -p /opt/car.lua /data/berlin-latest.osm.pbf
My file name was berlin so I went with berlin (you needed to go with your map or osm.pbf file name)
The flag -v "${PWD}:/data"
creates the directory /data
inside the docker container and makes available the current "{$PWD}”
working directory there. Inside the container, the file refers to on the host "${PWD}/berlin-latest.osm.pbf"
docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-partition /data/berlin-latest.osrmdocker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-customize /data/berlin-latest.osrm
And Now
docker run -t -i -p 5000:5000 -v "${PWD}:/data" osrm/osrm-backend osrm-routed --algorithm mld /data/berlin-latest.osrm
This is how your files will be looking after extracting osm.pbf
(Note: If got any error like files are missing, please repeat from step2 again without deleting any files )
Step 3: Getting Response
And now in your cmd
GET/{service}/{version}/{profile}/{coordinates}[.{format}]?option=value&option=value (Will explain you dont worry)orcurl "http://127.0.0.1:5000/route/v1/driving/13.388860,52.517037;13.385983,52.496891?steps=true"
Or in browser
http://127.0.0.1:5000/route/v1/driving/13.388860,52.517037;13.385983,52.496891?steps=true
And 13.388860,52.517037;13.385983,52.496891
are latitude and longitude for two places (which you need to find the distance between them)
This is HTTP requests to find a distance between the two-place using latitude and longitude
Let’s see different HTTP requests at below
And yes you will get an answer through a JSON like ( Note: If any error you can also call me at 3:00 AM)
{"code":"Ok","routes":[{"geometry":"kruh@q`mfNxBu`@nzEkcCfWmoBgs@}cBpoB}jJtkAoDtzCo`BnlKu{@pbKl^|eLmqFn`E}j@~qL_bGsU{vA_k@mDmkBomCtaA{uC`cBwrBsh@w}@~N}XxvBye@viAewBjwFmvApOxd@`fEs@jr@xo@","legs":[{"steps":[],"distance":87717.5,"duration":5342,"summary":"","weight":5342}],"distance":87717.5,"duration":5342,"weight_name":"routability","weight":5342}],"waypoints":[{"hint":"...... }]}
I will be explaining more about this JSON in my next blog soon
Here I have attached some demo video on how to host on localPort after extracting osm.pbf
Now Local port is ready to get requests
Results in JSON (Happy ending)
Requests
Okay let me explain it HTTP requests
GET /{service}/{version}/{profile}/{coordinates}[.{format}]?option=value&option=value
Version
The version of the protocol implemented by the service
Profile
Typically it is car
, bike
or foot
Coordinates
Just longitude and latitude in-service format which you needed
Service
Service has one of the following values: route
, nearest
, table
, match
, trip
, tile
Nearest service
Snaps a street network coordinate, and returns the nearest n matches.
Where coordinates only support a single entry in {longitude}, {latitude}.
Example
http://router.project-osrm.org/nearest/v1/driving/13.388860,52.517037?number=3&bearings=0,20
Example Response
{
"waypoints" : [
{
"hint" : "KSoKADRYroqUBAEAEAAAABkAAAAGAAAAAAAAABhnCQCLtwAA_0vMAKlYIQM8TMwArVghAwEAAQH1a66g",
"distance" : 4.152629,
"name" : "Friedrichstraße",
"location" : [
13.388799,
52.517033
]
},
{
"hint" : "KSoKADRYroqUBAEABgAAAAAAAAAAAAAAKQAAABhnCQCLtwAA7kvMAAxZIQM8TMwArVghAwAAAQH1a66g",
"distance" : 11.811961,
"name" : "Friedrichstraße",
"location" : [
13.388782,
52.517132
]
},
{
"hint" : "KioKgDbbDgCUBAEAAAAAABoAAAAAAAAAPAAAABlnCQCLtwAA50vMADJZIQM8TMwArVghAwAAAQH1a66g",
"distance" : 15.872438,
"name" : "Friedrichstraße",
"location" : [
13.388775,
52.51717
],
}
],
"code" : "Ok"
}
Route service
Finds the fastest route between coordinates in the supplied order
Example
http://router.project-osrm.org/route/v1/driving/13.388860,52.517037;13.397634,52.529407;13.428555,52.523219?overview=false
Table service
Computes the duration of the fastest route between all pairs of supplied coordinates
Example
http://router.project-osrm.org/table/v1/driving/13.388860,52.517037;13.397634,52.529407;13.428555,52.523219
And you can also select sources and destinations by just adding it index
Unlike other array encoded options, the length of sources
and destinations
can be smaller or equal to the number of input locations
sources=0;3&destinations=5;
Example
http://router.project-osrm.org/table/v1/driving/13.388860,52.517037;13.397634,52.529407;13.428555,52.523219?sources=0;3&destinations=2;4
Trip service
The trip plugin solves the Traveling Salesman Problem using a greedy heuristic (farthest-insertion algorithm)
So if I confused you or frustrated you or any wrong in my blog, yell me at comment box
Yes you can also yell at my twitter and if any error you can also call me at 3:00 AM