I’ve grown quite fond of the OpenStreetMap project, especially the self-hosting solution that comes with it. Of course, I would need an entire server farm to come even close the what Google Maps is capable of, but with the use of the imposm3 tool, well, one can get very interesting results!

I will be using imposm3 for the imports and updates, mapnik for the rendering engine and mod_tile to generate and serve files over the almighty intrawebz.

Why not osm2pgsql ?

If you’ve ever tried importing an entire country with osm2pgsql, you’ll know how much of a pain that is. It took me about 18 hours to import France, and don’t even get me started on diff imports or later country imports.

If I want to have a somewhat sexy self-hosted mapping solution, at least let me have daily diffs and decent performance from the ground up!

That’s where imposm rocked my world! It reduced importing time to a mere 2.5 hours, and I found the tables to be most convenient: some tables are “generalized” to account for various zoom levels, meaning it won’t have to process as much data when only low detail is required. Also, it’s way easier to figure out where all the objects are: roads/paths/tracks/highways are in osm_roads, buildings are in osm_buildings, areas are in osm_areas, etc.

Why can’t I just use that with mapnik then ?

Well, you can, but the generated osm.xml file only works with osm2pgsql imports. That’s when I figured out I needed to understand how this file works, and build my own for imposm3.

Installink mapnik (with libxml2)

apt install make cmake g++ libboost-dev libboost-system-dev   libboost-filesystem-dev libexpat1-dev zlib1g-dev   libbz2-dev libpq-dev libproj-dev lua5.2 liblua5.2-dev
apt install clang libxml2 libxml2-dev libxml2-utils liblwgeom-dev libpq-dev libproj-dev zlib1g-dev libharfbuzz-dev libfreetype6-dev libboost-all-dev python-setuptools unifont postgis

git clone https://github.com/mapnik/mapnik
cd mapnik
git submodule update --init
python scons/scons.py install XMLPARSER=libxml2

Installing mod_tile

apt install libagg-dev

git clone https://github.com/openstreetmap/mod\_tile.git
cd mod\_tile
./autogen.sh
./configure
make -j2
make install
make install-mod\_tile

Next, you’ll need to create /etc/renderd.conf:

[renderd]
;socketname=/var/run/renderd/renderd.sock
num\_threads=4
tile\_dir=/var/lib/mod\_tile
stats\_file=/var/run/renderd/renderd.stats

[mapnik]
plugins\_dir=/usr/local/lib/mapnik/input
font\_dir=/usr/local/lib/mapnik/fonts
font\_dir\_recurse=1

[default]
URI=/tiles/
TILEDIR=/var/lib/mod\_tile
XML=/home/osm/imposm.xml
HOST=localhost
TILESIZE=256

Then, put this in your Apache vhost:

	ServerAdmin webmaster@localhost
	ServerName map-tiles.gradew.net
	DocumentRoot /var/www/tiles

	<IfModule mod_tile.c>
		LoadTileConfigFile /etc/renderd.conf
		#ModTileRenderdSocketName /tmp/osm-renderd
		ModTileRenderdSocketName /var/run/renderd/renderd.sock
		ModTileRequestTimeout 3
		ModTileMissingRequestTimeout 30
		ModTileMaxLoadOld 2

		# In bulk mode, mod_tile does not request any dirty tiles to be rerendered.
		ModTileBulkMode On

		# That's a week
		# ModTileCacheDurationMax 604800
		# That's a year
		ModTileCacheDurationMax 31536000
	</IfModule>