Open Street Maps

Much like Google Maps, Openstreetmaps is a map that allows you to navigate the world. The main difference being that OSM is free and can be self hosted. However there is not a plug-and-play solution that will host your own maps. It turns out that hosting your own maps, including geolocation and calculating distances is quite a big task, especially if you want to suppor the whole world.

OSRM

Open Source Routing Machine (OSRM) is a project that allows you to calculate distances from a OSM map. See more about the project here

Setup

Setting OSRM up is straightforward, but it requires you to the same docker container several times. In short the commands for the Netherlands are as follows:

#Download The Netherlands
mkdir -p data/
cd data/
wget https://download.geofabrik.de/europe/netherlands-latest.osm.pbf

cd ../
docker run --name osrm-backend -t -v "${PWD}/data:/data" osrm/osrm-backend osrm-extract -p /opt/car.lua /data/netherlands-latest.osm.pbf

# Process the data
docker run -a osrm-backend -t -v "${PWD}/data:/data" osrm/osrm-backend osrm-contract /data/netherlands-latest.osrm
docker run -t -v "${PWD}/data:/data" osrm/osrm-backend osrm-partition /data/netherlands-latest.osrm
docker run -t -v "${PWD}/data:/data" osrm/osrm-backend osrm-customize /data/netherlands-latest.osrm

# Finaly run the routing engine
docker run -t -i -p 5998:5000 -v "${PWD}/data:/data" osrm/osrm-backend osrm-routed --algorithm mld /data/netherlands-latest.osrm

Now you can use curl to do requests to port 5998.

Note

When using Google Maps coordinates to send requests to OSRM, make sure that the Longitude and Latitude coordinates are not swapped.

Nominatim

As it turns out, OSRM is a very good routing engine but it can’t do geolocation. To solve this Nominatim was used to resolve postal codes to coordinates.

$ cat run_nominatim.sh
#! /bin/bash

docker run -it \
-e PBF_PATH=/nominatim/data/netherlands-latest.osm.pbf \
-v "${PWD}/data:/nominatim/data" \
-e REPLICATION_URL=https://download.geofabrik.de/europe/netherlands-updates/ \
-p 5999:8080 \
--name nominatim \
mediagis/nominatim:4.2

This will result in a website that is accessible on port 5999 to which you can send queries.

IP Firewall

Procesing routing data is hard. In order to prevent bots from misusing these interfaces a firewall rule was added to only allow incomming traffic from specific ip addresses.

$ sudo ufw allow from <ip_addr> to any port 5998
$ sudo ufw allow from <ip_addr> to any port 5999

Tile Server

In order to also serve tiles and be fully independent from Google Maps a tileserver was also started. For this the previously donwloaded .pbf file needs to be imported and a postgresql database was used to hopefully speed up the performance.

First we need to import the data, then start a tileserver.

Import data
$ docker run -v "${PWD}/data/netherlands-latest.osm.pbf:/data.osm.pbf" -v openstreetmap-data:/var/lib/postgresql/12/main overv/openstreetmap-tile-server:1.3.10 import

Run tile server
$ docker run -p 5997:80 -v openstreetmap-data:/var/lib/postgresql/12/main -d overv/openstreetmap-tile-server:1.3.10 run

This will open a map server at port 5997. When navigating to http://www.herreweb.nl:5997/tile/0/0/0.png, a world map is shown.

Note

The import step took over 2 hours on a 6-core slow server(Intel Xeon E5-2620 v2). The map server is not fast either, but faster after loading all the map tiles.