RENDERING A OSM FILE USING MAPNIK

You have mapnik installed and a running python installation on your computer. The next step is to gather the data from OSM.

You can export data directly from osm.org manually:

  1.  Go to osm.org
  2.  Search your location or the area you wish to export.
  3.  Click on export on top left corner of your screen.
  4.  Select area manually.
  5.  Click on export.

NOTE: while exporting data manually you have to change longitudes and latitudes in render.py file you will create later.

If you want to download large data easily then, go to downloads.bbbkie.org or https://mapzen.com/data/metro-extracts/.

I have Downloaded Ludhiana map manually from osm.org with ( min longitude, min latitude, max longitude, min latitude) value : (75.0833,30.4866,76.4182,31.3935)

Create the main xml file which contains instructions for mapnik about the contents of the map and how to render them. Create the file mapnik_style.xml with the following content:

<?xml version="1.0" encoding="utf-8"?>
<Map background-color="#f2efe9" srs="+proj=latlong +datum=WGS84">
 <FontSet name="book-fonts">
 <Font face-name="DejaVu Sans Book" />
 </FontSet>
 <Style name="highways">
 <Rule>
 <Filter>[highway] &lt;&gt;''</Filter>
 <LineSymbolizer stroke="#808080" stroke-width="2" stroke-linejoin="round"
 stroke-linecap="round" />

</Rule>
 <Rule>
 <Filter>[highway] &lt;&gt;''</Filter>
 <TextSymbolizer name="[name]" fontset-name="book-fonts" size="9" fill="#0000" halo-radius="1" placement="line" />
 </Rule> 
 </Style>
 <Layer name="highways" status="on" srs="+proj=latlong +datum=WGS84">
 <StyleName>highways</StyleName>
 <Datasource>
 <Parameter name="type">osm</Parameter>
 <Parameter name="file">ldh.osm</Parameter>
 </Datasource>
 </Layer>
</Map>

Finally, we have to tell mapnik to render the map using the style file. Create a python script render.py with following content:

#!/usr/bin/env python2

from mapnik import *

mapfile = 'mapnik_style.xml'
map_output = 'mymap.png'

m = Map(4*1024,4*1024)
load_map(m, mapfile)
bbox=(Envelope( 75.0833,30.4866,76.4182,31.3935 ))

m.zoom_to_box(bbox)
print "Scale = " , m.scale()
render_to_file(m, map_output)

NOTE: while exporting data manually you have to change longitudes and latitudes in render.py file.

Now run command: python render.py

Problems faced:

The rendered image turned out to be blank.

Solution:

Latitudes and longitudes has to be changed when you change datasource. In bbox=(Envelope( 75.0833,30.4866,76.4182,31.3935 )) the format (min long, min lat, max long, max lat) should be followed.

Output:

 

Data exported from osm.org manually. Map area is ludhiana.

mymap2.png

To add details to image edit xml file. Replace LineSymbolizer with following:

<TextSymbolizer face-name="DejaVu Sans Book" size="16" placement="point" dy="8" fill="blue" placement-type="list">[name]
    <Placement size="10" dy="-8" fill="red"/><!-- Reduces text size and changes offset -->
    <Placement fill="green">[abbreviated_name]</Placement> <!-- size="10", dy="-8", fill="green", shorter text -->
    <Placement fill="orange" dy="8">[nr]</Placement> <!-- size="10", dy="8", fill="orange", shortest text -->
</TextSymbolizer>

This change will give Output:

mymap4

Zooming the above image we can see names of different locations.

new.png

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s