Deploying your own Slippy Map Gentoo

From OpenStreetMap Wiki
Jump to: navigation, search

On gentoo things work a little different than on the other distro's. This page is dedicated to Deploying your own Slippy Map on Gentoo. It contains some dirty hacks and version specific information which may be outdated quickly. However that is the beauty of a wiki that YOU can correct my silly mistakes.

I will use Mapnik, Mod_tile, and Openlayers to create the slippy map.

This page is based on Deploying your own Slippy Map.

Overview

There are three main components we need to set up to deploy a slippy map. First we need to get the Openstreetmap data and imported it into a postgres database with postgis extension. Solutions that try to avoid this step are horrendously slow and only intended for very small areas. Osm2pgsql will be used to import the data. The second component is the rendering toolchain based on mapnik. Lastly we need to set up a stylesheet, basically a config file for the rendering toolchain. Importantly it contains the data sources, where the map data is found. In addition to the postgres database, most map styles will also reference static shape files. Which include information on high level features like coastlines.

Step one: Importing Openstreetmap data

Get the planet file

Get a planet file or get an extract (saves time to download). You can chose the protocol buffer (.osm.pbf) format. These are smaller to download and osm2pgsql can handle them.

Installing database packages

  • dev-db/postgresql (9.5.3, use at least 9.0)
  • dev-db/postgis (2.2.2)
  • sci-geosciences/osm2pgsql (0.90.0)

Set the pbf use flag for osm2pgsql so we can use the newer format

echo sci-geosciences/osm2pgsql pbf >> /etc/portage/package.use/slippymap

Install the database packages

emerge dev-db/postgres dev-db/postgis sci-geosciences/osm2pgsql

Creating database and user

As the database is pretty large you might not want to store it in the default location (own partition maybe).

First su into the postgres user:

su - postgres

Create the directory where you want the database and initialize it

mkdir /path/to/new/dir
initdb --pgdata /path/to/new/dir

Edit /etc/conf.d/postgresql-9.5 to make DATA_DIR point to the /path/to/new/dir and start the postgres server.

Create a new user and a new database:

createuser <username>
createdb -E UTF8 -T template0 -O <username> <db>

Install the postgis extension for the database

psql <db>
CREATE EXTENSION postgis;
ALTER TABLE geometry_columns OWNER TO <username>;
ALTER TABLE spatial_ref_sys OWNER TO <username>;
\q

Permissions

Make sure you add the users that are going to use the database (user running osm2pgsql, the webserver) to the postgres group

gpasswd -a <username> postgres

Importing using osm2pgsql

Now that the database is all set lets fill it

osm2pgsql -U <user> -m -d <db> <planet-file>

If you only want a section of the world do:

osm2pgsql -U <user> --bbox minlon,minlat,maxlon,maxlat -m -d <db> <planet-file>

Step two: Rendering toolchain

Use Flags

Set the following useflags:

  • geos
  • gdal
  • proj
  • postgres
  • python
  • icu (for boost, see Mapnik wiki)

Required packages

The following packages from portage will be used Version numbers are included to document a working combination. Newer should also work.

  • dev-vcs/git
  • sci-geosciences/mapnik (3.0.9)
  • www-servers/apache (2.4.23)

we will also need mod_tile which isn't currently in portage

  • www-apache/mod_tile (9999 live ebuild)

Geekasylum's overlay has a live ebuild. You can follow the instructions of adding his overlay. Or fetch it from here and add it to a local overlay.

emerge dev-vcs/git sci-geosciences/mapnik www-servers/apache www-apache/mod_tile

Mapnik python bindings

Starting with mapnik 3 python bindings are no longer included in the mapik library. We will install python-3.4 bindings.

Set up a virtual python environment

mkdir env
pyvenv env
source env/bin/activate

Your prompt should be prefixed with '(env)'.

clone and build python-mapnik

git clone https://github.com/mapnik/python-mapnik
BOOST_PYTHON_LIB=boost_python-3.4 ./setup.py develop build
BOOST_PYTHON_LIB=boost_python-3.4 ./setup.py develop install

If these commands throw an error involving LDFLAGS variable comment out the offending line in setup.py To test if the python bindings work

python
>>> import mapnik
>>> 

Should complete with no errors.

configure apache and mod_tile

mod_tile installed a sample html file into /var/www/osm move this directory to your apache document root. In /etc/conf.d/apache2 add -D MOD_TILE to the APACHE2_OPTS. example:

APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D SSL -D SSL_DEFAULT_VHOST -D LANGUAGE -D MOD_TILE"

Mod_tile consists of 2 components, the apache module, and a rendereing daemon renderd. edit /etc/renderd.conf in particular set

[mapnik]
font_dir=/usr/share/fonts/
[default]
URI=/osm/
XML=/path/to/mapnik-stylesheets/osm.xml

the XML file will be created in the next step.

Step Three: Map style

We will use the old openstreetmap-mapnik style in this article. For simplicity as it avoids the need for node.js. OSM-bright and openstreetmap-carto are some newer alternatives.

Get the style

git clone https://github.com/openstreetmap/mapnik-stylesheets

Get the world boundries

Besides the .osm.pbf stylesheets often also rely on shape files. Which contain information on coastlines, and other features. Mapnik-stylesheets contains a script get-coastlines.sh which will download the ones needed in this article. We will instead use the portage package sci-geosciences/mapnik-world-boundaries and manually download the 3 missing ones.

emerge sci-geosciences/mapnik-world-boundaries

Fetch and extract all the archives into /usr/share/mapnik/world_boundaries/. Different styles may use additional shape files(.shp). In case any of them come without an index(.index) file you can use the shapeindex tool (part of mapnik) to generate them.

Generate osm.xml

The stylesheet that mapnik uses osm.xml includes several files from the inc/ subdirectory. These include files need to be configured to know where to find the postgresql database and shape files.

Since this is the old style format it is not python3, and we'll use python2 to save the trouble of having to do it manually

python2 generate_xml.py --host localhost --password "" --port 5432 --user <username> --dbname <db> --world_boundaries=/usr/share/mapnik/world_boundaries/

Generating the first tile

Make sure postgres is still running. Since we installed python bindings for python3 we will make mapnik-stylesheets/livetile/render_single_tile.py python3 compatible by changing:

sys.stdout.write(im.tostring('png'));

to

open('firsttile.png', 'wb').write(im.tostring('png'))

you should now be able to render your first tile with the command:

python render_single_tile.py ../../mapnik-stylesheets/osm.xml 0 0 0

Slippy map

start apache2 and check that mod_tile is loaded by visiting http://localhost/mod_tile. you can start renderd via its init script. but to see what's going on we'll start it from shell with the -f flag

renderd -f

Pointing your browser to http://localhost/osm should show a slippy map.