Building a Local OSRM Instance
The Open Source Routing Machine (OSRM) is a library for calculating routes, distances and travel times between spatial locations. It can be accessed via either an HTTP or C++ API. Since it’s open source you can also install locally, download appropriate map data and start making efficient travel calculations.
These are the instructions for getting OSRM installed on a Ubuntu machine and hooking up the
osrm R package.
Building OSRM is memory intensive, so unless you are installing on a machine with a good chunk of RAM, you’ll want to ensure that there’s around 4 Gb of swap space available.
First make sure that you have the necessary infrastructure and libraries required to build OSRM.
Now grab the source directly from the repository on GitHub.
Move into the source folder, create a
build folder and then run
cmake to generate Makefiles.
Next initiate the build.
Time to kick back and wait: this will take some time!
When the build completes, make the
Go to the export page on OpenStreetMap. Zoom in on you area of interest and then press the Export button. The area I was working with was too large to download directly from OpenStreetMap, so I followed the link to the Overpass API, which worked flawlessly.
I’m installing on a remote instance, so I used
wget to do the download.
The resulting download will be a (possibly rather large) XML file. Move it to the
osrm-backend folder created above.
Extracting the Map
profiles folder you’ll find three files (
foot.lua) which provide speed profiles for various means of transportation. You can create a custom profile if necessary, but the ones provided will suffice for most situations. We’ll go with the car profile.
The next step is to extract the routing data. This can be very memory intensive, so make sure that you have sufficient swap space or that you’re using something like STXXL.
Creating a Hierarchy
Now to create data structures that facilitate finding the shortest route between two points.
Launching the Service
We can launch a HTTP server which exposes the OSRM API as follows:
Let’s try a few test queries. First we’ll find the nearest road to a location specified by a longitude/latitude pair.
Next the distance and time between two locations.
duration values are in seconds and the
distance is in metres. Looks pretty legit!
More information on the API can be found here.
If you were wanting to expose this service to the outside world then you’d need to integrate it with your web server and maybe set up something to restart the service if the machine reboots. I’m only planning on using OSRM locally, so these are not issues for me.
osrm R Package
My primary motivation for setting up OSRM is so that I can use it from within R.
First install a couple of packages.
Now install the
Load the package and point it at the local OSRM service.
Now create a couple of locations.
Generate a table of travel times between those locations.
Calculate the optimal route between two locations.
The units are now minutes for
duration and kilometres for
I’ve been using the
gmapsdistance package until now. It has worked brilliantly but I’ve had to manage it carefully to avoid overstepping API limits. With a local OSRM I’ll be making the calculations unconstrained!