Mapnik is an open source, high quality map rendering engine. From the front page of their site:
"Mapnik is a Free Toolkit for developing mapping applications. Above all Mapnik is about making beautiful maps. It is easily extensible and suitable for both desktop and web development."
For some time I have been planning to take a closer look at it but I’ve been to busy to sit down and pick my way through. Over the last two evenings I finally made a go of it and built it on my system and took it for a test drive. First step to installing (on Ubuntu Jaunty 64 bit) was to get the build dependencies from apt.
Mapnik Library Setup
sudo apt-get install g++ cpp libboost-dev libxml2 libxml2-dev \
libfreetype6 libfreetype6-dev libjpeg62 libjpeg62-dev libltdl7 \
libltdl7-dev libpng12-0 libpng12-dev libgeotiff-dev libtiff4 libtiff4-dev \
libcairo2 libcairo2-dev python-cairo python-cairo-dev libcairomm-1.0-1 \
libcairomm-1.0-dev ttf-dejavu ttf-dejavu-core ttf-dejavu-extra libgdal1-dev \
python-gdal postgresql-8.3-postgis postgresql-8.3 postgresql-server-dev-8.3 \
postgresql-contrib-8.3 libsqlite3-dev subversion build-essential
Note:Above updated for Karmic, 29 Nov 2009
I am installing the most current state of the software from the mapnik subversion trunk. To use mapnik with the Quantumnik QGIS plugin (which we cover further down), you need at least version 0.6.1 of Mapnik. Using the trunk covers that base, but you could also install from the official release tarballs. The mapnik in apt is however too old so we need to build from source.
Next I went to my development dir:
cd ~/dev/cpp
And then checked out the mapnik sources:
svn co http://svn.mapnik.org/trunk
Then build mapnik:
cd mapnik
python scons/scons.py configure INPUT_PLUGINS=all \
OPTIMIZATION=3 SYSTEM_FONTS=/usr/share/fonts/truetype/ttf-dejavu/
python scons/scons.py
sudo python scons/scons.py install
It didnt take a long time to build and install. Since I am running on the 64bit version of Jaunty, I had to make a small tweak to my system library search path:
sudo vim /etc/ld.so.conf
To which I added the following line:
/usr/local/lib64
And then I updated the library search path:
sudo ldconfig
Next I did the obligatory ‘hello world’ test:
python
import mapnik
Installing the Quantumnik plugin
So the mapnik library is a high quality map rendering engine. To define the maps, you need to create xml definition files (mapfiles). The Quantumnik plugin for QGIS will take an existing QGIS project and generate a mapnik mapfile for it – with some limitations. The limitations stem from limitations in the QGIS symbology infrastructure which does not allow for named styles, multiple symbols per feature and scale based symbol switching in a single layer. Some of these issues should be resolved in QGIS 1.3 with the introduction of some symbology work that Martin Dobias is busy with. In the mean time you can still create a fairly pleasing mapfile using QGIS.
To start I added the mapnik repository to QGIS using the python plugin manager. The repo url is:
http://qgis.dbsgeo.com/
After adding the repo I went ahead and installed the Quantumnik plugin, enabled it in the Plugin Manager in QGIS and then restarted QGIS.
Next I compiled a simple project in QGIS using vector layers stored in a PostGIS database.
Note that the layers need to be listed in the geometry_columns table in PostGIS in order for mapnik to recognise them.
Once my project was compiled, I could use the Plugins -> Quantumnik -> Quantumnik menu to switch the QGIS map canvas to instead use Mapnik to render the view. Once you are happy with the product, use the Plugins -> Quantumnik -> Export XML menu to write my mapnik xml mapfile out to disk.
Here is what my project looked like using the standard QGIS render engine.
And here is what the mapnik renderer looks like:
Testing the tilelite standalone server
You can test your mapnik mapfile out using a lightweight web serverakin to the webserver used for testing django projects. First we need to install the mercurial code revision system:
sudo apt-get install mercurial
Then install the tilelite server:
cd ~/dev/python
hg clone http://bitbucket.org/springmeyer/tilelite/
cd tilelite
sudo python setup.py install
Now go to where you exported the mapnik mapfile using Quantumnik – I put mine into /tmp/ while testing. Then run the tilelite ‘liteserv’ server:
liteserv.py mapnik.xml
Finally point your web browser at http://localhost:8000 and you should see your map.
Publishing your map
From here you have basically three options for publishing your maps to the greater world:
- use the apache mod_tile module to serve the tiles in google mercator
- use tilecache (see my previous blog post on setting up tilecache). Tilecache has special support to caching mapnik data. You just need to remember to specify the projection = <proj4literal> and extension=png256 options. The latter will reduce the size of the rendered tiles.
- use OGCServer (also a mapnik project) to publish the map as wms. Publishing the map as WMS means you should be able to use it from a WMS client like QGIS since it does not render to tile boundaries.
You can get an idea of how simple it is to overlay your mapnik data onto google maps from this code example: http://bitbucket.org/springmeyer/tilelite/src/tip/utils/gmap.html#cl-28
In a follow up post I plan to walk through the configuration I do to publish a pleasing looking map via mapnik and TileCache.
Thanks
Many thanks to Dane Springmeyer (mapnik developer) who walked me through the whole process outlined above – much appreciated!