DIY: Routing engine powered by Open Data

How to route your ‘Bossche Bol’ to Maastricht with a Docker container

Have you ever run over the maximum number of queries in your Google Maps API key?
Do you want to get accurate directions for other vehicles than just your regular bikes or cars?
Do you work in logistics and want to make sure your products are distributed efficiently?
Are you curious about how to use Docker containers to extract the most value from your (open) data?
If you’ve answered yes to one of these questions, then this blog post is for you!

Open data is an excellent resource for tackling many problems. It comes in many different flavors, including what’s known as crowdsourced data. This type of data is commonly used in mapping solutions, including Open Street Maps (OSM).

One of the main challenges when using open data is extracting its value and making it available for the end user. Many open source tools facilitate sharing and using data effectively. You can use these tools to seize the power of the crowdsourced data and enrich your projects.

In this blog post, we will create a Docker container capable of generating routes for different types of vehicles. In this case, we will make use of OSM PBF files, commonly used in tools such as Open Source Routing Machine (OSRM) and Open Route Service (ORS).

ORS is an alternative to OSRM, that can use the same data files (PBF). One of the main advantages of this service is that it can efficiently generate distance and travel time matrices. On top of this, you can create isochrones and get step-by-step directions from point A to point B.

The choice for ‘Bossche Bollen’ was an easy one. At our Rubix office, pre-covid of course, colleagues would often bring in these delicious treats on their birthday. For this blog, we’ll just assume that it’s someones birthday and this someone wants to relocate these treats because of very good reasons.

Without further ado, let’s get started

For those with a direct action addiction we have prepared a TL;DR. It will bring your Bossche Bollen home, in case you already have a PBF file that suits your interests.

Cloning our repository

The first step is to clone the repository of this project. To do so, use ‘git clone’ in a terminal as follows:

$ git clone https://github.com/rcaroprese/docker-blog.git

This will create a folder named ‘docker-blog’. Inside it, you will find two directories: ‘openrouteservice’ and ‘viz.’. We will use the first one to build the routing service, whereas the second directory contains a visualizer that can print routes.

The first step in working with data is getting it. As mentioned above, ORS works with OSM PBF files. One of the most useful websites regarding this type of information is Geofabrik. They store in their servers data extracts to make routes for the entire planet.

First of all, go into the following directory:

$ cd docker-blog/openrouteservice/docker/data/pbf/

Since we want to do a quick example with our major assignment to bring “Bossche Bollen” to Maastricht, we need to download files covering two Dutch provinces: Noord-Brabant and Limburg. In order to do it, we can use ‘wget’:

$ wget https://download.geofabrik.de/europe/netherlands/noord-brabant-latest.osm.pbf
$ wget https://download.geofabrik.de/europe/netherlands/limburg-latest.osm.pbf

These files will not only allow us to know which route to take from Den Bosch to Maastricht, but it will let us try one amazing Docker container and keeps things lightweight.

Modifying the data

To create an OSM PBF file which contains all the relevant roads for our test, we can use the docker container of osmium-tool to merge the files:

First, we need to download the image:

$ docker pull stefda/osmium-tool

Then, we can use osmium to merge the files of the two provinces we downloaded:

$ docker run -it -w /data -v $(pwd):/data stefda/osmium-tool osmium merge /data/noord-brabant-latest.osm.pbf /data/limburg-latest.osm.pbf -o /data/nb_li.osm.pbf

In summary, what this command does is mount the current directory $(pwd) to the /data directory in the container. Furthermore, it runs the merge command of the osmium tool. Finally, it generates a merged file in the current directory with the name nb_li.osm.pbf.

This is very useful; you can merge any regions of interest and make specific, lightweight local routing services.

Finally, delete the original files.

$ rm noord-brabant-latest.osm.pbf limburg-latest.osm.pbf
Running a custom Docker container

Once we have created our OSM PBF file, we can use a customized Docker image to generate new routes in these provinces. The repository you have cloned already includes a customized version of the ‘openrouteservice’ repository. If you are interested in knowing more about that project, you can go to

https://openrouteservice.org/

or have a look at their repository: https://github.com/GIScience/openrouteservice

In the meantime, you only need to go to the openrouteservice/docker directory. If you’ve followed the steps so far,

$ cd ../..

Should do the trick.

There, run:

$ docker-compose up

Leave this terminal running. This command will trigger the creation of several files based on the OSM PBF file we created. This will enable you to get directions between places in the region we downloaded. The process should last a couple of minutes.

In the end, you should be able to go to localhost:8180/ors/health and get ready as the status.

Visualizing routes

Open a new terminal and go to the viz directory located at the root of the repository you cloned:

$ cd docker-blog/viz

should do the trick.

Once again, the only thing you need to do here is run:

$ docker-compose up

Once again, keep the terminal running. In this case, the command will create a docker container that includes a Jupyter notebook you can use to query and see routes. You can access the notebook by going to localhost:8888 and pasting the token for your session. The name of the notebook is Visualizer.ipynb.

If everything went well, you should be able to see the following map in the Visualizer notebook:

Now you will be able to get some of the famous ‘Bossche Bollen’ in Den Bosch and bring them to Maastricht!

Enjoy 😀

TL;DR 

Do you just want direct action?! If you already have an osm.pbf file follow the next instructions to build your route engine. Otherwise grab one that interests you from https://www.geofabrik.de/ and buckle up!

First, clone the repository:

$ git clone https://github.com/rcaroprese/docker-blog.git

Copy your osm data file to docker-blog/openrouteservice/docker/data/pbf/ with the name nb_li.osm.pbf

Routing engine

Once you have the data in the correct directory we can build the routing engine container. To do so, go to the following folder:

$ cd docker-blog/openrouteservice/docker/

And run:

$ docker-compose up

You will need to wait around 5 to 10 minutes before the process is complete. You can check the progress status by going to http://localhost:8180/ors/health. Once it says ready, you will have a routing engine running on your computer in the port 8180. Keep this terminal running.

Visualizer

For the visualizer, in a different terminal, go to:

$ cd docker-blog/viz

Once again, run:

$ docker-compose up

You will be able to access a Jupyter notebook in port 8888.