Finding a Way for Routing

I was working on a small hack involving paths on a OpenStreetMap map, and I found the public, free service provided by Project OSRM to calculate car routes beetwen two points.
But I've been disappointed by the fact the public server was often unavailable due the high rate of incoming requests, so I tried to host a private instance of OSRM on my own VPS.
I've been even more disappointed by the high resources required to run an OSRM instance, even on a small area: on my VPS, sporting 2GB of RAM, I've been unable to even pre-process (with the osrm-prepare utility) a small OSM file fetched from OpenStreetMap database.

In the end I found Routino, a naive but quite functional alternative to OSRM. It is already packaged in Debian Wheezy (on the contrary of OSRM), it is fast enough in execution (probably not such as OSRM, but good for me), and - most important due my constraints - the pre-process of a small OSM file occupies just a few megabytes of RAM and ends within one minute.

I'm hosting a public instance, limited for now to the area of Turin, Italy (the one which interests to me), which can be freely queried with this example code:

var url = 'http://route.madbob.org/router.cgi?transport=motorcar;lat1=' + start['lat] + ';lon1=' + start['lon'] + ';lat2=' + end['lat'] + ';lon2=' + end['lon'] + ';type=shortest';

$.get(url, function(generated) {
    var uuid = generated.substr(0, generated.indexOf("\n"));
    $.get('http://route.madbob.org/results.cgi?uuid=' + uuid + ';type=shortest;format=gpx-track', function(xml) {
        // DO THINGS
    });
});

To host your own instance, just apt-get install routino routino-www. The Debian package provides most of the setup, as usual you will find the configuration in /etc/apache2/conf.d/routino.conf.

To run on top of nginx, you have also to apt-get install fcgiwrap and setup your virtual host as:

server {  
    listen   80;

    server_name  route.madbob.org;
    root         /usr/share/routino/www;

    location ~* \.cgi$ {
        gzip           off;
        root           /usr/share/routino/www/;
        fastcgi_pass   unix:/var/run/fcgiwrap.socket;

        fastcgi_param  QUERY_STRING       $query_string;
        fastcgi_param  REQUEST_METHOD     $request_method;
        fastcgi_param  CONTENT_TYPE       $content_type;
        fastcgi_param  CONTENT_LENGTH     $content_length;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
        fastcgi_param  REQUEST_URI        $request_uri;
        fastcgi_param  DOCUMENT_URI       $document_uri;
        fastcgi_param  DOCUMENT_ROOT      $document_root;
        fastcgi_param  SERVER_PROTOCOL    $server_protocol;
        fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
        fastcgi_param  REMOTE_ADDR        $remote_addr;
        fastcgi_param  REMOTE_PORT        $remote_port;
        fastcgi_param  SERVER_ADDR        $server_addr;
        fastcgi_param  SERVER_PORT        $server_port;
        fastcgi_param  SERVER_NAME        $host;

        # This is to permit the whole web to query your service, remove if you
        # don't want that
        add_header     Access-Control-Allow-Origin *;
    }
}

Script to periodically update the data:

#!/bin/sh

# This is the default folder for Routino data, can be configured in
# /usr/share/routino/www/paths.pl
mv /var/lib/routino

# Those are my coordinates, change it accordly your needs
wget "http://overpass-api.de/api/map?bbox=7.4765,44.9553,8.0156,45.1699" -O file.osm

# For some reason, those two tags - appearing of top of XML exported by
# OverPassAPI - are not liked by planetsplitter. Can be remove with no concerns
sed "s/^<meta .*$//g" -i file.osm  
sed "s/^<note>.*$//g" -i file.osm

mkdir temp  
planetsplitter --dir=temp/ file.osm  
rm data/*  
mv temp/* data/  
rm -rf temp

rm file.osm  

Enjoy free maps!