czwartek, 30 maja 2013

OpenStreetMap tile server on Debian

There are many reasons why one might want to run own OpenStreetMap tile server. It is somehow explained here, here and here. but, in my opinion, not thoroughly enough.

I'll explain in detail how I did it on a Debian Jessie server. Note that in this method tiles are generated manually. To generate tiles automatically a solution like apache mod_tile must be used.
  1. Download OpenStreetMap data.
    This file contains a map itself but in an abstract format. It's best to start with a small dataset (available at download.geofabrik.de), the whole planet is 28GB.
  2. Install and configure PostgreSQL database (description at debian wiki). That's where OpenStreetMap data will be kept for easy access.
    $ sudo aptitude install postgresql postgresql-client
    Add a user (preferably the name of user that will be accessing the database, say bob):
    $ sudo -u postgres createuser bob
    Add a database named gis owned by bob:
    $ sudo -u postgres createdb --encoding=UTF8 --owner=bob gis
  3. Install postGIS. This is a PostgreSQL extension for keeping there geographic data.
    $ sudo aptitude install postgis
    Configure created database for postgis:
    $ sudo -u postgres psql -d gis -f /usr/share/postgresql/8.4/contrib/postgis-1.5/postgis.sql
    $ sudo -u postgres psql -d gis -f /usr/share/postgresql/8.4/contrib/postgis-1.5/spatial_ref_sys.sql
    sudo -u postgres psql -d gis -c "ALTER TABLE geometry_columns OWNER TO bob"
    $ sudo -u postgres psql -d gis -c "ALTER TABLE spatial_ref_sys OWNER TO bob"
  4. Install osm2pgsql - a script to load data from .osm file into PostgreSQL database.
    $ sudo aptitude install osm2pgsql
    Perform import:
    $ osm2pgsql --slim -C 1500 poland-latest.osm.pbf
  5. Install Mapnik - library to generate tiles (images) based on data in PostgreSQL database.
    $ sudo aptitude install python-mapnik2 mapnik-utils
  6. Download and extract Mapnik stylesheet fileshttps://github.com/openstreetmap/mapnik-stylesheets/archive/master.zip (it has a nice README file).
  7. Download coastline shapes with script included in archive above:
    $ ./get-coastlines.sh
  8. Modify xml files with script provided:
    $ python generate_xml.py --dbname gis --user bob --accept-none
  9. I had problems with fonts (they are mentioned here), but I had to comment out lines in inc/fontset-settings.xml.inc that contained "unifont Medium".
  10. To test if everything works change bounds in generate_image.py to whichever fit your needs (they should be covered by OSM data you downloaded though) and launch the script:
    $ python generate_image.py
    If everything went fine there will be an output:
    output image to image.png!
    And of course an image will be created.
  11. To generate all tiles modify file to choose what tiles you want to be created (at the bottom of the file). There are some examples provided. To create tiles for Poland for zooms ranging from 1 to 12 I used:
    bbox = (14.28,48.92, 24.21,54.95)
    render_tiles(bbox, mapfile, tile_dir, 1, 12 , "Polska")
    To than generate tiles create a directory, say tiles and do (in bash):
    $ MAPNIK_MAP_FILE="osm.xml" MAPNIK_TILE_DIR="tiles/" ./generate_tiles_multiprocess.py
    (it's a trick with setting environment variables for command execution).
To display your tiles using OpenLayers (as described here):
  1. Make your tiles available via web server.
  2. Install packaged version of openlayers:
    $ sudo aptitude install libjs-openlayers
  3. Create an index.html file and paste the following:

1 komentarz:

  1. Ma problem przy 3 punkcie, zainstalowałem postgis'a, ale przy próbie konfiguracji zostaje zwrócony błąd: "BŁĄD: bieżąca transakcja została przerwana, polecenia ignorowane do końca bloku transakcji"
    Google niestety nie był dla mnie pomocny, za pomoc z góry dzięki.

    OdpowiedzUsuń