Mis à jour le 07/05/2011: utilisation de Debian Squeeze (ie. beaucoup moins de composants à compiler)
aptitude -R install subversion bzip2 unzip python build-essential aptitude -R install apache2 apache2-mpm-prefork aptitude -R install postgresql-8.4 aptitude -R install openjdk-6-jdk
#> aptitude -R install postgresql-8.4-postgis
shared_buffers = 128MB checkpoint_segments = 10 maintenance_work_mem = 256MB
#> /etc/init.d/postgresql restart
Après ces changement de paramètres de PostgreSQL, vous risquez d'avoir avez un message d'erreur signalant qu'il n'est pas possible d'allouer autant de mémoire partagé sur votre système. Vous pouvez corriger cela avec la commande suivante (valable jusqu'au prochain redémarrage de la machine)
#> sysctl -w kernel.shmmax=268435456
Si vous souhaitez que le réglage soit persistant, il faut l'ajouter dans le fichier /etc/sysctl.conf
kernel.shmmax=268435456
Nous allons créer une base de données de type PostGIS :
#> su - postgres $> createuser -P tilegen $> createdb -E UTF8 -O tilegen gis $> createlang plpgsql gis $> psql -d gis -f /usr/share/postgresql/8.4/contrib/postgis-1.5/postgis.sql $> psql -d gis -f /usr/share/postgresql/8.4/contrib/postgis-1.5/spatial_ref_sys.sql $> echo "ALTER TABLE geometry_columns OWNER TO tilegen; ALTER TABLE spatial_ref_sys OWNER TO tilegen;" | psql -d gis
#> aptitude -R install osm2pgsql
Osmosis est un outil en ligne de commande qui permet de traiter les données d'OpenStreetMap :
Pour installer la dernière version :
#> aptitude -R install osmosis $> osmosis -v 6 mars 2009 23:07:39 com.bretth.osmosis.core.Osmosis run INFO: Osmosis Version 0.34 [...]
Récupérer un fichier depuis http://download.geofabrik.de/osm/
$> cd ~/osm/ $> mkdir data-import $> cd data-import $> wget http://download.geofabrik.de/osm/europe/france/pays-de-la-loire.osm.bz2
Création (filtrage ?) d'un fichier .osm pour la zone qui nous intéresse (ici, Nantes)
$> osmosis --read-xml pays-de-la-loire.osm.bz2 --bounding-box left=-2.30 top=46.90 right=-0.90 top=47.70 --write-xml nantes.osm
Import de la structure et des données initiales :
$> osm2pgsql -m -d gis -H localhost -U tilegen -W nantes.osm
Créer une configuration initiale pour la mise à jour via les “diff” par Osmosis : (le paramètre initialDate dépend évidemment de leur de votre dump initial - heure fournie par Geofabrik dans notre cas)
$> cd ~/osm/ $> cd data-import $> osmosis --read-change-interval-init initialDate=2011-05-06_07:00:00
Nous allons créer un script replicate_osm_file.sh. Il sera déclenché toutes les heures par crontab. Son rôles : déclencher Osmosis afin appliquer les “diff” au fichier .osm
#!/bin/sh # This script automates the replication of changes into an offline osm file for a specific area of interest. # This allows an up-to-date local snapshot of an area to be maintained. OSM_FILE=nantes.osm TEMP_OSM_FILE=tmp.osm WORKING_DIRECTORY=./ # The bounding box to maintain. LEFT=-2.30 BOTTOM=46.90 RIGHT=-0.90 TOP=47.70 osmosis --read-change-interval $WORKING_DIRECTORY --read-xml $OSM_FILE --apply-change --bounding-box left=$LEFT bottom=$BOTTOM right=$RIGHT top=$TOP --write-xml $TEMP_OSM_FILE if [ "$?" -ne "0" ]; then echo "Osmosis failed, aborting." exit -1 fi mv $TEMP_OSM_FILE $OSM_FILE
Il suffit ensuite de relancer un import de la base avec ce fichier mis à jour :
$> cd ~/osm/ $> osm2pgsql -m -d gis -H localhost -U tilegen -W nantes.osm
$> cd ~/osm/ $> wget http://tile.openstreetmap.org/world_boundaries-spherical.tgz # (50M) $> wget http://tile.openstreetmap.org/processed_p.tar.bz2 # (358M) $> wget http://tile.openstreetmap.org/shoreline_300.tar.bz2 # (46M) $> wget http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/10m-populated-places.zip # (1.5 MB) $> wget http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/110m-admin-0-boundary-lines.zip # (38 KB) $> tar xzf world_boundaries-spherical.tgz $> tar xjf processed_p.tar.bz2 -C world_boundaries $> tar xjf shoreline_300.tar.bz2 -C world_boundaries $> unzip -q 10m-populated-places.zip -d world_boundaries $> unzip -q 110m-admin-0-boundary-lines.zip -d world_boundaries
#> aptitude -R install mapnik-utils python-mapnik
Récupération des outils et fichiers de configuration Mapnik d'OpenStreetMap
$> cd ~/osm/ $> svn co http://svn.openstreetmap.org/applications/rendering/mapnik osm-mapnik $> cd osm-mapnik
On définit nos options de configurations et on génère notre fichier osm.xml :
$> ./generate_xml.py osm.xml my_osm.xml --host localhost --user 'tilegen' --dbname gis --symbols ./symbols/ --world_boundaries ~/osm/world_boundaries --port 5432 --password 'tilegen'
Génération d'un fichier “image.png” d'exemple (centrée sur UK par défaut) :
$> MAPNIK_MAP_FILE=my_osm.xml ./generate_image.py
Installation des headers pour la compilation du module Apache
#> aptitude -R install apache2-prefork-dev libmapnik-dev
Récupération de mod_tile depuis le SVN
$> cd ~/osm/ $> svn co http://svn.openstreetmap.org/applications/utils/mod_tile $> cd mod_tile
Edition de la configuration :
[renderd] num_threads=4 tile_dir=/home/drazzib/osm/www/tiles/ stats_file=/tmp/renderd.stats [mapnik] plugins_dir=/usr/lib/mapnik/0.7/input font_dir=/usr/share/fonts/truetype/ttf-dejavu/ font_dir_recurse=1 [default] URI=/tiles/ XML=/home/drazzib/osm/osm-mapnik/my_osm.xml
Voir la configuration détaillée de renderd
Compilation et installation
$> make #> make install
Le module mod_tile est copié dans le répertoire des modules d'Apache.
Il faut ensuite lancer le daemon qui sera en charge de recevoir les requetes de ce module. C'est ce daemon qui va dialoguer avec le système de rendu Mapnik. Pour le lancer :
$> cd ~/osm/mod_tile $> ./renderd -f
LoadModule tile_module /usr/lib/apache2/modules/mod_tile.so <VirtualHost *:80> ServerName localhost DocumentRoot "/home/drazzib/osm/www/tiles" LoadTileConfigFile /etc/renderd.conf <Directory "/home/drazzib/osm/www/tiles"> AllowOverride All Allow from All </Directory> </VirtualHost>
Voir la configuration détaillée de mod_tile
Création des répertoires :
$> mkdir -p ~/osm/www/tiles $> chmod -R 777 ~/osm/www
Activation du site :
#> a2ensite osm-tiles #> /etc/init.d/apache2 reload
Création d'une page HTML utilisant les tuiles générées :
<html> <head> <title>OpenStreetMap</title> <script src="http://www.openlayers.org/api/OpenLayers.js"></script> <script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js"></script> <script type="text/javascript"> var epsg4326 = new OpenLayers.Projection("EPSG:4326"); var epgs900913 = new OpenLayers.Projection("EPSG:900913"); var lat = 47.2169; var lon = -1.5495; var zoom = 11; var map; function init() { map = new OpenLayers.Map ("map", { controls:[ new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.Attribution()], maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), maxResolution: 156543.0399, numZoomLevels: 19, units: 'm', projection: epgs900913, displayProjection: epsg4326 } ); var newLayer = new OpenLayers.Layer.OSM("local", "/tiles/${z}/${x}/${y}.png", {numZoomLevels: 19}); map.addLayer(newLayer); var lonLat = new OpenLayers.LonLat(lon, lat).transform(epsg4326, map.getProjectionObject()); map.setCenter(lonLat, zoom); } </script> </head> <body onload="init();"> <div style="width:100%; height:100%" id="map"></div> </body> </html>