Introduction

OSRM – Open Street Routing Machine is a open source C++ project and Nominatim is a tool for synthesising address from OSM – Open Source Map data. Have a look at both of them here. These applications are used widely by many organisations to supplement their Map requirements. I am assuming the readers are familiar with the Google Maps API, you can have a look at their analogous functions with OSRM.

Google Maps – Directions API – OSRM-Backend route

Google Maps – Distance Matrix – OSRM-Backend table ( contains only duration )

Google Maps – Geo-coding API – Nominatim

Google Maps – Road API – OSRM-Backend match

In case you have the requirement for the above APIs or any here , go ahead to install OSRM and Nominatim.

Requirements

Have a look at the size of the osm pbf you would want to use at geofabrik . File size ranges from 100 MB – 33 GB. Your RAM requirements would range from 512 MB – 8 GB and your Swap requirements would range from 1 GB – 100 GB. So, be generous while allocating swap to avoid multiple trials installing. Tutorial to create swap .

Installation

To keep the OS updated.

sudo apt-get update

sudo apt-get upgrade

sudo apt-get install -y unattended-upgrades osmosis

Go to directory where you would like to have osrm

mkdir osrm

cd osrm

mkdir data

cd data

wget -O map.osm.pbf url_of_the_pbf

The url_of_the_pbf file you got from geofabrik.

Although the above command is used for people who want to download a country/continent, but we have the option of creating merge pbf files with multiple countrie(s)/continent(s), or you can skip to Install the dependencies.

Creating a merged PBF file for multiple Country(s)/Continent(s)

Download the pbf file using the command below.

wget -O <country/continent_1>.pbf pbf_url_path_of_country/continent

Extract the data from the pbf file.

osmosis --read-pbf file="country/continent" --write-xml file="country/continent.osm"

Sort the data in the extracted file.

osmosis --read-xml file="country/continent.osm" --sort type="TypeThenId" --write-xml file="country/continent-sorted.osm"

Likewise you can create a sorted pbf files for each country/continent you’re interested in. Use the command below merge the sorted list of country(s)/continent(s).

osmosis --rx country/continent-sorted.osm --rx country/continent_1-sorted.osm --merge --wx merged-sorted-list.osm

Now, create the merge pbf file from the merge sorted file.

osmosis --read-xml file="merged-sorted.osm" --write-pbf file="map.osm.pbf"

By now we have created our merged map.osm.pbf file.

Install the dependencies

sudo apt install build-essential git cmake pkg-config \

libbz2-dev libstxxl-dev libstxxl1v5 libxml2-dev \

libzip-dev libboost-all-dev lua5.2 liblua5.2-dev libluabind-dev libtbb-dev

Installing OSRM

cd ..

git clone https://github.com/Project-OSRM/osrm-backend.git

cd osrm-backend

mkdir build

cd build

cmake ..

sudo make install

cd ..

cd ..

osrm-extract data/map.osm.pbf -p osrm-backend/profiles/car.lua

osrm-extract command may not execute successfully. It depends on the map.osm.pbf file. If it does well and good else look for a older versions of file. Also it could be memory issue try using a bigger RAM and swap.

osrm-contract data/map.osrm

osrm-contract command takes long time to execute. It might fail due to small swap. Again I would give the same advice, try increasing your swap and RAM.

osrm-routed data/map.osrm

Voilaa.. you have it running on your http://localhost:5000.

Try the following command to

curl "http://127.0.0.1:5000/route/v1/driving/source_longitude,source_latitude;destination_longitude,destination_latitude?steps=true&alternatives=true&geometries=geojson"

source_longitude, source_latitude, destination_longitude and destination_latitude should be from the country/region you have downloaded.

Installing Nominatim – Dependencies

sudo apt-get update -qq

sudo apt-get upgrade -y

sudo apt-get install -y build-essential cmake g++ libboost-dev libboost-system-dev \

libboost-filesystem-dev libexpat1-dev zlib1g-dev libxml2-dev \

libbz2-dev libpq-dev libgeos-dev libgeos++-dev libproj-dev \

postgresql-server-dev-9.5 postgresql-9.5-postgis-2.2 postgresql-contrib-9.5 \

apache2 php php-pgsql libapache2-mod-php php-pear php-db \

git python3-pip

sudo pip3 install psycopg2

chmod a+x .

Installing Nominatim

git clone --recursive git://github.com/twain47/Nominatim.git

cd Nominatim

mkdir build

cd build

cmake ..

make

wget -O /PATH/TO/ROOT/osrm/Nominatim/data/country_osm_grid.sql.gz http://www.nominatim.org/data/country_grid.sql.gz

sudo -u postgres createuser www-data

sudo -u postgres psql nominatim

GRANT usage ON SCHEMA public TO "www-data";

GRANT SELECT ON ALL TABLES IN SCHEMA public TO "www-data";

sudo -su postgres./utils/setup.php --osm-file ../../data/map.osm.pbf --all 2>&1 | tee setup.log

Point your Server to Nominatim/build/website/ by setting DocumentRoot in /etc/apache2/sites-enabled/000-default.conf .

Also grant permissions to the osrm Directory in /etc/apache2/apache2.conf

Restart your apache server

sudo service apache restart

You should get your server running on your http://localhost/ . By default its port 80. Try the following command to verify.

curl "http://127.0.0.1/reverse.php?format=json&lat=<latitude>&lon=<longitude>&zoom=18

<latitude>, <longitude> should be from the country/region you have downloaded.

So guys, its just getting started with free usage for Maps. Theres a long road ahead, keep it going.

To setup your own tile server and provide free maps for your web applications you can read the following blog.