Howto real time tiles rendering with mapnik and mod python

From OpenStreetMap Wiki
Jump to: navigation, search


In a project I was creating, based on my own rendering rules sets, I hit a problem that I solved by real-time rendering tiles. It is clearly not the fastest solution as mod_tile is better. But I didn't want to install mod_tile

Here is a live example of it : Yet another validation tool for osm data

What is this solution good for?

  • If you wish to save disk space
  • If you want to test or to have several different mapnik styles
  • If you want more flexibility on tile generation process with your own python code
  • If you want updates very often
  • if you can't or don't want to install mod_tile which is faster

What problems does that create?

  • If more than ~2 or 3 users access maps at the same time, it will put a lot of CPU load on the machine (for massive use, a cache mecanism is available, and you'll be able to serve more users )
  • Generation times are quite slow
  • On low zoom (3 to 10) tile generation time might be even worse (a cache is definitively useful for those zoom levels)

Hardware machine for my rendering

I'm using a fairly "standard" machine, and I expect things to be a lot lot better if you have a powerful server. Composed of :

  • Intel(R) Core(TM)2 Duo CPU E7200 @ 2.53GHz
  • 2 GO RAM
  • 512 Go disk space (I'm currently using 100 Mo of it for the permanent tiles cache)

software configuration

  • GNU/Linux Debian Lenny amd64
  • postgres & postgis
  • osm2pgsql

Starting up

I won't write everything again, so please have a look at the very good tutorials :

Then you have to try some generation with python of tiles to make sure every things work with mapnik ( is a good one to test )

How to set up the real time rendering

Prerequist and line of installation

This method needs mod_python included with apache in order to limit loading and unloading the python interpreter

The idea is that you will define a "virtual" tile directory that will in fact be empty where the python script that does all the work is.

Preparing the directory

Create a directory of your choice ("tiles" in my example) and write a .htaccess file with :

SetHandler python-program
PythonPath "['/home/sites/'] + sys.path"
PythonHandler renderer::handle
ExpiresActive on
ExpiresDefault "access plus 1 hours"
  • /home/sites/ is the path of you directory
  • ExpiresDefault "access plus 1 hours" will tell that tiles should be kept by the browser in cache for at least 1 hour
  • PythonHandler renderer::handle says that to do the tile rendering should be handled by a file called "" with a method "handle"

Put the python script in

In that directory put a file called that contains :

#!/usr/bin/env python
# Source is GPL, credit goes to Nicolas Pouillon
# comments goes to sylvain letuffe org (" " are replaced by @ and . )
# fichier execute par mod_python. handle() est le point d'entree.

import os, os.path
from gen_tile import MapMaker

def get_renderers(path, zmax = 20):
    r = {}
    for filename in os.listdir(path):
        if filename.startswith("."):
        if not filename.endswith(".xml"):
        rname = filename[:-4]
            r[rname] = MapMaker(os.path.join(path, filename), zmax)
    return r
renderers = get_renderers("/home/sites/", zmax)

def handle(req):
    from mod_python import apache, util
    path = os.path.basename(req.filename)+req.path_info

    # strip .png
    script, right = path.split(".", 1)
    new_path, ext = right.split(".", 1)
    rien, style, z, x, y = new_path.split('/', 4)

    if style in renderers:
        req.status = 200
        req.content_type = 'image/png'
        z = int(z)
        x = int(x)
        y = int(y)
	#req.content_type = 'text/plain'
	#return apache.OK
	if z<13:
        req.write(renderers[style].genTile(x, y, z, ext, cache))

        req.status = 404
        req.content_type = 'text/plain'
        req.write("No such style")
    return apache.OK

You have to change /home/sites/ to where you mapnik styles are

Note : If you are using an other webserver than Apache and you could not use the mod_python module, here is a script which works with lighttpd (fcgi and flup packages needed) :

#!/usr/bin/env python
# Source is GPL, credit goes to Nicolas Pouillon
# comments goes for this version to pierre_dot_mauduit _at_ gmail _dot_ com

import os, os.path
from gen_tile import MapMaker

def get_renderers(path, zmax = 20):
    r = {}
    for filename in os.listdir(path):
        if filename.startswith("."):
        if not filename.endswith(".xml"):
        rname = filename[:-4]
            r[rname] = MapMaker(os.path.join(path, filename), zmax)
    return r
renderers = get_renderers("/var/www/rtmapnik/", zmax)

def handle(environ, start_response):
    newpath, ext = os.environ['PATH_INFO'].split('.', 1)
    rien, style, z, x, y = newpath.split('/', 4)

    if style in renderers:
        status = 200
        content_type = 'image/png'
        z = int(z)
        x = int(x)
        y = int(y)
        if z<13:
        start_response('200 OK', [('Content-Type', content_type)])
        print renderers[style].genTile(x, y, z, ext, cache)
        return []
       return ["Error occured"]

if __name__ == '__main__':
    from flup.server.fcgi import WSGIServer

And another one, in the same directory with a name ""

#!/usr/bin/env python

import os
import mapnik
import math

def minmax (a,b,c):
    a = max(a,b)
    a = min(a,c)
    return a

class GoogleProjection:
    def __init__(self,levels=18):
        self.Bc = []
        self.Cc = []
        self.zc = []
        self.Ac = []
        c = 256
        for d in range(0,levels):
            e = c/2;
            self.Cc.append(c/(2 * math.pi))
            c *= 2
    def fromLLtoPixel(self,ll,zoom):
         d = self.zc[zoom]
         e = round(d[0] + ll[0] * self.Bc[zoom])
         f = minmax(math.sin(math.radians(ll[1])),-0.9999,0.9999)
         g = round(d[1] + 0.5*math.log((1+f)/(1-f))*-self.Cc[zoom])
         return (e,g)
    def fromPixelToLL(self,px,zoom):
         e = self.zc[zoom]
         f = (px[0] - e[0])/self.Bc[zoom]
         g = (px[1] - e[1])/-self.Cc[zoom]
         h = math.degrees( 2 * math.atan(math.exp(g)) - 0.5 * math.pi)
         return (f,h)

class MapMaker:
    sx = 256
    sy = 256
    prj = mapnik.Projection("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs +over")
    def __init__(self, mapfile, max_zoom):
        self.m = mapnik.Map(2*, 2*
        self.max_zoom = max_zoom
        self.gprj = GoogleProjection(max_zoom)
        except RuntimeError:
            raise ValueError("Bad file", mapfile) = hex(hash(mapfile))
    def tileno2bbox(self, x, y, z):
        p0 = self.gprj.fromPixelToLL((*x,*(y+1)), z)
        p1 = self.gprj.fromPixelToLL((*(x+1),*y), z)
        c0 = self.prj.forward(mapnik.Coord(p0[0],p0[1]))
        c1 = self.prj.forward(mapnik.Coord(p1[0],p1[1]))
        return mapnik.Envelope(c0.x,c0.y,c1.x,c1.y)
    def genTile(self, x, y, z, ext="png", cache=False):
        if cache:
            outname = '/home/sites/'%( z, x, y, ext)
            if os.path.exists(outname):
                fd = open(outname, 'r')
	  outname = os.tmpnam()

        bbox = self.tileno2bbox(x, y, z)
        bbox.width(bbox.width() * 2)
        bbox.height(bbox.height() * 2)

        im = mapnik.Image(*2,*2)
        mapnik.render(self.m, im)
        view = im.view(,,,
       , ext)
        fd = open(outname)
        out =

        if not cache:
        return out

# Fonction de test, qui n'est appelee que si on execute le fichier
# directement.

def test():
    renderer = MapMaker('/home/sites/', 18)
    for filename, x, y, z in (
        ('test.png', 2074, 1409, 12),
        fd = open(filename, 'w')
        fd.write(renderer.genTile(x, y, z))

if __name__ == '__main__':

Again replace /home/sites/ by your directory.

As you can see, if you read python fluently, here :

        cache = z<13
        req.write(renderers[style].genTile(x, y, z, ext, cache))

This is a piece of code that says to keep cached files for zoom <13 you can adapt if you wish to "<0" for no cache at all or "<20" for a cache of anything.

<13 looks a good compromise because, low zoom tiles take a long time to create, while keeping everything will need to flush at some times (based on tiles dates, osm2pgsql diff expired tiles list or at defined dates )

Open Layers

I started form there : OpenLayers Simple Example

And added the relevant parts in the index :

<script src="/libs/OpenLayers.js"></script>
<script src="/libs/styles.js"></script>


var layers = [];
  for ( var idx in all_available_styles ) {
      var name = all_available_styles[idx];
      var l = new OpenLayers.Layer.TMS( 
          getURL: get_osm_url, 
          displayOutsideMaxExtent: true }, {'buffer':1} );


My full index.html file for reference

With /libs/OpenLayers.js being a local copy.

And /libs/styles.js containing the different possible styles that you want to use.

var all_available_styles= new Array();
all_available_styles["noname"]=["No name"];
all_available_styles["nooneway"]=["No Oneway"];
all_available_styles["noref"]=["No Ref on way"];
all_available_styles["fixme"]=["Fixme or note Tags"];
all_available_styles["for_wikipedia"]=["For Wikipedia (without POI)"];
all_available_styles["mapnik_copy"]=["Mapnik SVN Copy (11/2008)"];
all_available_styles["test_zone"]=["test zone"];
all_available_styles["test_zone2"]=["test zone 2"];
  • the key is the name of your mapnik style file (without .xml )
  • The value is the name that is displayed on the layer switcher

Cron job

My simplified cron job, takes its data from and looks likes this : It must be launched by the postgres user or a user that has access to the DB :
cd /home/sites/

#File name

# URL where to find it

#remove old
rm $FILE

#get the new
wget $URL/$FILE

/usr/local/bin/osm2pgsql -S ./ -m -d gis $FILE 2>>log >>/dev/null
  • "-S ./" is optionnal but you can write your own if you wish

edit 2009-03-20 : This solution was the easy one but importing europe is long, very long, I now use diff imports as explained here Minutely_Mapnik

Tile URL acccess

In those examples, the URL for the tile access is constructed to be :

noname being the name of the style file you want to use for the rendering
12 being the zoom level
jpeg being the chosen extension, just change it to png and you will get png !

PS: you could remove the "" as it is useless and adapt the split code of the url, but it at least tells people that this uses python and not mod_tile

Thanks for reading. -- Sletuffe 15:08, 25 November 2008 (UTC)

Tweeks to my setup

Questions / remark

On the talk page, or by e-mail please