STEP 1

Download required datasource file, It can be a osm file or a shapefile.

STEP 2

Create a xml file if you are using shapefile with following code:

<?xml version="1.0" encoding="UTF-8"?>
<Map background-color="rgba(255,255,255,1)">
 <Style name="NoOffset">
 <Rule>
 <LineSymbolizer stroke="rgb(170,255,0)" stroke-width="1" offset="0" smooth="1">
 </LineSymbolizer>
 </Rule>
 <Rule>
 <TextSymbolizer face-name="DejaVu Sans Book" fill="#008000" halo-radius="10" halo-fill="rgba(255, 255, 255, 0.6)" placement="line" clip="true" vertical-alignment="middle" dy="-10"><![CDATA[[name]]]>
 </TextSymbolizer>
 </Rule>
 </Style>
 <Layer name="NoOffset">
 <StyleName>NoOffset</StyleName>
 <Datasource>
 <Parameter name="type">shape</Parameter>
 <Parameter name="file">roads.shp</Parameter>
 </Datasource>
 </Layer>
 <Style name="Offset">
 <Rule>
 <LineSymbolizer stroke="rgb(255,0,0)" stroke-width="6" stroke-dasharray ='5' stroke-dashoffset ='5' offset="1" >
 </LineSymbolizer>
 </Rule>
 <Rule>
 <TextSymbolizer face-name="DejaVu Sans Book" size="10" placement="point" dy="8" fill="black" placement-type="list">[name] 
 <Placement size="50" dy="-8" fill="blue"/>
 </TextSymbolizer>
 </Rule>
 </Style>
 <Layer name="Offset">
 <StyleName>Offset</StyleName>
 <Datasource>
 <Parameter name="type">shape</Parameter>
 <Parameter name="file">railways.shp</Parameter>
 </Datasource>
 </Layer>
</Map>

Also, create a python file with following code:

from mapnik import *

mapfile = 'new.xml'
map_output = '44.png'

m = Map(4*600,4*600)
load_map(m, mapfile)
bbox=(Envelope( 75.7823000,30.8456000,75.9241000,30.9438000))

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

NOTE: in bbox=(Envelop (….)) add your files (minimum longitude, minimum latitude, maximum longitude, maximum latitude).

If you are using a osm file just create same xml file as above and replace shape with osm and shapefile with your osm file, Datasource.

STEP 3

Now run command: python render.py

OUTPUT

44.png

Zooming the above image we can railway tracks with red color and roads with green color.

Screenshot from 2017-06-26 11:15:35.png

Share: