MAPNIK

Mapnik is used to rendering Maps and provides flexible styling options for designing different kinds of Maps. So, the main question is how to render a map with mapnik? For this first we need to install mapnik.

Step 1: check installation

Make sure you have mapnik installed. You should be able to open a terminal and type:

mapnik-config -v # should return a version number.

Next test the Python bindings:

python -c “import mapnik;print mapnik.__file__”

Step 2

Now, we need some data to render. Download the data from the Natural Earth Data Site. http://naturalearthdata.com

Once unzipped, you should see four files. To download and unzip files from command line type:

wget https://github.com/mapnik/mapnik/wiki/data/110m-admin-0-countries.zip
unzip 110m-admin-0-countries.zip # creates ne_110m_admin_0_countries.shp

Step 3

Open a Python interpreter simply by typing in your terminal and import mapnik python bindings: import mapnik

Create Map

Then create a map:

m = mapnik.Map(600,300)

m.background = mapnik.Color(‘steelblue’)

Create Style

Then Create the Styles which determines how the data is rendered:

s = mapnik.Style()
r = mapnik.Rule()
polygon_symbolizer = mapnik.PolygonSymbolizer()
polygon_symbolizer.fill = mapnik.Color(‘#f2eff9’)
r.symbols.append(polygon_symbolizer)

line_symbolizer = mapnik.LineSymbolizer()
line_symbolizer.stroke = mapnik.Color(‘rgb(50%,50%,50%)’)
line_symbolizer.stroke_width = 0.1

r.symbols.append(line_symbolizer)
s.rules.append(r)

And add the Style to the Map:

m.append_style(‘My Style’,s)

Create a Datasource

use an absolute path of the files we downloaded  and unzipped in step2

ds = mapnik.Shapefile(file=’/Users/abhinav/Downloads/ne_110m_admin_0_countries.shp’)

>>> ds.envelope()

Box2d(-180.0,-90.0,180.0,83.64513) will be shown after ds,envelope()

Here, Coordinates are between -180 and 180 for the x or longitude values and -90 and 90 for the y or latitude values.

Create a Layer

layer = mapnik.Layer(‘world’)

Now attach the datasource to the layer:

layer.datasource = ds

Apply style to map:

layer.styles.append(‘My Style’)

Render The Map

layer.styles.append(‘My Style’)
m.layers.append(layer)
m.zoom_all()
mapnik.render_to_file(m,’world.png’, ‘png’)
print “rendered image to ‘world.png'”

Exit the python interpreter.

Step 4

Create a blank text file called, world.py

Make it executable:

chmod +x world.py

Now append the entire text given below and save the file:

#!/usr/bin/env python
import mapnik
m = mapnik.Map(600,300)
m.background = mapnik.Color(‘steelblue’)
s = mapnik.Style()
r = mapnik.Rule()
polygon_symbolizer = mapnik.PolygonSymbolizer()
polygon_symbolizer.fill = mapnik.Color(‘#f2eff9’)
r.symbols.append(polygon_symbolizer)

line_symbolizer = mapnik.LineSymbolizer()
line_symbolizer.stroke_width = 0.1

r.symbols.append(line_symbolizer)
s.rules.append(r)
m.append_style(‘My Style’,s)
ds = mapnik.Shapefile(file=’/home/abhinav/ne_110m_admin_0_countries.shp’)
layer = mapnik.Layer(‘world’)
layer.datasource = ds
layer.styles.append(‘My Style’)
m.layers.append(layer)
m.zoom_all()
mapnik.render_to_file(m,’world.png’, ‘png’)
print “rendered image to ‘world.png'”

Problems Faced

While creating datasource errors occured:

RuntimeError: Shape Plugin: shapefile ‘/home/abhinav/Downloads/110m-admin-0-countrie.shx.shp’ does not exist

This Error occured because file 110m-admin-0-countrie.shx.shp did’nt had permissions due to which it was neither readable nor executable.

Solution

By using command chmod 777. This command makes the file readable, writable and executable by everyone.

 

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