Setting up Mapnik for rendering tiles in your language.

Mapnik is the default renderer on the OpenStreetMap main site. We can setup our own OpenStreetMap server for rendering the tiles either online or offline. This note would help you to setup an offline one (online if you do on your server) with special eye on font setup for localized tiles.

To render the tiles using Mapnik, you need to create a PostgreSQL database out of the OSM data. Then install Mapnik depending on the Operating System you run. These primary steps are very well covered in the following pointers.

  1. Setting up PostgreSQL database out of the OSM data. Read more.
  2. Build your own OpenStreetMap server – Richard Weait. Read more.
    • For Ubuntu Lucid Lynx read here.
  3. Using Mapnik for rendering the tiles – Richard Weait. Read more.

Once you are done with the above steps, we might think of tweaking the Mapnik rules to render the tiles in a local language. For this all we need to do is point Mapnik to the right font. You can quickly follow the steps briefed by Richard, here.

You can run

python
>>> from mapnik import *
>>> for face in FontEngine.face_names(): print face
… [Enter]

DejaVu Sans Bold
DejaVu Sans Bold Oblique
DejaVu Sans Book
DejaVu Sans Condensed
DejaVu Sans Condensed Bold
DejaVu Sans Condensed Bold Oblique
DejaVu Sans Condensed Oblique
DejaVu Sans ExtraLight
DejaVu Sans Mono Bold
DejaVu Sans Mono Oblique

……………………………………..

>>> Ctrl-d

to see what fonts are currently being recognized by Mapnik. The second task is to install the local language unicode font to Mapnik’s default font directory. If you have already installed Mapnik, you can run strace -ff, and search for font to see which directory is used by Mapnik. In my case it was the default directory at /usr/share/fonts/

Next, you need to copy the required fonts to the above directory. You might run the python code snippet again to see whether Mapnik recognizes the fonts. For example, in my case, it looks like this.

DejaVu Sans Bold

DejaVu Sans Bold Oblique

DejaVu Sans Book

DejaVu Sans Condensed

DejaVu Sans Condensed Bold

DejaVu Sans Condensed Bold Oblique

DejaVu Sans Condensed Oblique

DejaVu Sans ExtraLight

Rachana Regular —> new one
Note the name of the font that appears in the above list. This name would be used in the Mapnik rule file.
Now, we’ll edit the Mapnik custom rule file, as described below.
You can find this in ~/mapnik/<template name>.xml. Open the file in an editor of your choice. The file begins with something like this.

<FontSet name=”bold-fonts”>
<Font face_name=”DejaVu Sans Bold”></Font>
</FontSet>
<FontSet name=”book-fonts”>
<Font face_name=”DejaVu Sans Book”></Font>
</FontSet>
<FontSet name=”oblique-fonts”>
<Font face_name=”DejaVu Sans Oblique”></Font>
</FontSet>

Change the face_name to the font name we just copied to the fonts directory.  In my case, it would look like this.
<FontSet name=”bold-fonts”>
<Font face_name=”Rachana Regular”></Font>
</FontSet>
<FontSet name=”book-fonts”>
<Font face_name=”Rachana Regular”></Font>
</FontSet>
<FontSet name=”oblique-fonts”>
<Font face_name=”Rachana Regular”></Font>
</FontSet>
Mapnik rule file defines fonts for each of the possible tags which appear in the map data. You need change that in order to apply the special fonts to specific tags.

<TextSymbolizer name=”name” fontset_name=”book-fonts” size=”11″ fill=”rgb(0,0,0)” halo_radius=”1″></TextSymbolizer>

You can edit the lines which are like as shown above. Change the fontset_name, to the one you want. And run the renderer! You are done!
I have two images to egg you on!
Before installing font After installing fonts
DejaVu Sans Bold
DejaVu Sans Bold Oblique
DejaVu Sans Book
DejaVu Sans Condensed
DejaVu Sans Condensed Bold
DejaVu Sans Condensed Bold Oblique
DejaVu Sans Condensed Oblique

DejaVu Sans ExtraLigh

Thanks to the #osm channel irc.oftc.net and Richard Weait for helping out to figure the changes.

4 thoughts on “Setting up Mapnik for rendering tiles in your language.”

Leave a Reply

Your email address will not be published. Required fields are marked *