Routing with OSM data

From Open source mapmaking technologies
Jump to: navigation, search
This page contains changes which are not marked for translation.

Other languages:
English • ‎português

Routing on top of OpenStreetMap

The are several solutions to calculate routes using OpenStreetMap data.

In this course, we will use Routino.

Lets install some dependecies.

sudo apt-get install flex libbz2-dev libjson-pp-perl

Download routino

mkdir public_html
cd public_html
wget http://www.routino.org/download/routino-2.7.3.tgz

Compiling routino

tar xvzf routino-2.7.3.tgz
cd routino-2.7.3
make

Download China's planet file

This planet file is prepared every day, so we will get updates until yesterday.

The download will take some time. After downloaded, the planet is used to construct a graph. Route calculation is performed on top of this graph. The graph generation is also take some minutes (12 min. on my laptop).

cd web/data
wget http://download.geofabrik.de/asia/china-latest.osm.bz2
../bin/planetsplitter china-latest.osm.bz2

Calculate a route in the terminal emulator

Use two points in Wuhan just to test if the routing is working on the command line, before configuring a web interface.

../bin/router --lon1=114.3225 --lat1=30.4594 --lon2=114.3704 --lat2=30.5760 --output-gpx-track --shortest

If it works, a new shortest-track.gpx is written with the result. You can open it in QGIS, for example, to make sure it has a route. Let's learn another way of adding data to QGIS: passing it as parameters when calling QGIS.

qgis shortest-track.gpx

Since the gpx format can support different layers simultaneously, when you add a gpx track, a dialog with open to choose the desired layer(s). Choose the track layer.

GPX dialog.png

A track is displayed, hopefully. If the track is shown, the routing software is working. If not, do not proceed until you solve the problem.

Now we can proceed to install a web interface on top of this routing software.

Apache configuration

Apache is a very well know web server. It is the most used web server.

Let's configure Apache 2.4.x to serve our web routing solution. To do so, we need to edit Apache configuration.

sudo medit /etc/apache2/sites-enabled/000-default.conf

Add the following configuration by the end of the file, but before </VirtualHost>.

        AddHandler cgi-script .cgi 
        Alias /routino/ "/home/user/public_html/routino-2.7.3/web/"
        <Directory "/home/user/public_html/routino-2.7.3/web/">
                Options +Indexes +FollowSymLinks +MultiViews +ExecCGI
                AllowOverride All
                Require all granted
        </Directory>

Save and close the editor, and restart the Apache server with:

sudo apache2ctl graceful

Don't worry, if it blames with:

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message

Open http://localhost/routino/www/routino/router.html with the browser. You should see something, but not yet the complete web interface.

Routino web configuration

Let's install some Javascript libraries. First we need to update the OpenLayers link.

cd ../www/openlayers/
medit install.sh

Replace the line:

wget http://openlayers.org/download/OpenLayers-$version.tar.gz

with:

wget http://github.com/openlayers/openlayers/releases/download/release-2.13.1/OpenLayers-$version.tar.gz

Save. Now we can proceed.

sh -x install.sh
cd ../leaflet
sh -x install.sh
cd ../..
chmod -R a+rw results

Check if you can open http://localhost/routino/www/routino/router.html.

OpenStreetMap opens centered in England. We need to change mapprops.js.

cd www/routino
medit mapprops.js

Replace the bounds with these values (copy and paste on top of the existing lines):

 westedge: 73.557701,          // Minimum longitude (degrees)
 eastedge:  134.773926,          // Maximum longitude (degrees)
 southedge: 15.780000,          // Minimum latitude (degrees)
 northedge: 53.560860,          // Maximum latitude (degrees)
 zoomout:    5,            // Minimum zoom
 zoomin:    18,            // Maximum zoom

You can get the bounding box, with

ogrinfo -so CHN_adm3.shp CHN_adm3
Extent: (73.557701, 15.780000) - (134.773926, 53.560860)

Open again http://localhost/routino/www/routino/router.html

You should see the map centered in China and now you can calculate routes.

If something is falling, use the browser firebug/development tools to identify the problem. The configuration is complete.

Routino up and running.png

Update OpenStreetMap data automatically

To keep the routing update as possible, we need to download the OSM periodically.

The following script getchina.osm.sh will download the planet file and reconstruct the routing graph.

cd ~/public_html/routino-2.7.3/web/data
medit getchina.osm.sh

Contents of getchina.osm.shː

#!/bin/bash
#
cd ~/public_html/routino-2.7.3/web/data
find . -type f -mtime +7 -name '*'.osm.bz2 -exec rm -f \{\} \;
data=`date +%Y%m%d%H%M`
planeta=china-latest.$data.osm.bz2
server="download.geofabrik.de"
wget -N http://$server/asia/china-latest.osm.bz2 -O $planeta
../bin/planetsplitter --errorlog $planeta

Before we can run the script, we have grant it the execute privilege, withː

chmod +x getchina.osm.sh

Now the script can be run at any time, just by doingː

./getchina.osm.sh

To keep the data updated and to minimize the server overload, a better option is to download it and recreate the graph in in off-peak hours. crontab is used to configure repeating tasks in Lunix.

crontab -e

(crontab will ask you which editor to use. Accept the default, and go to the end of the file.)

# m h  dom mon dow   command
45 5 * * * $HOME/public_html/routino-2.7.3/web/data/getchina.osm.sh