Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Last active January 2, 2016 21:29
Show Gist options
  • Save springmeyer/8364280 to your computer and use it in GitHub Desktop.
Save springmeyer/8364280 to your computer and use it in GitHub Desktop.
Mapnik demo of loading a single map and rendering each layer inside it. Originally forked example from https://gist.github.com/springmeyer/7459452

Mapnik example

This shows how to programmatically turn on and off layers on a Mapnik map from python.

Then the map is rendered twice to show how only active layers are displayed.

Depends

Mapnik >= 2.2 and the python bindings

Usage

git clone https://gist.github.com/8364280.git mapnik-layer-example
cd mapnik-layer-example
python render.py
# then look at the images: you should see how one shows points and one shows lines
<Map
background-color="#eee"
srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
<Style name="point">
<Rule>
<MarkersSymbolizer />
</Rule>
</Style>
<Layer name="point" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
<StyleName>point</StyleName>
<Datasource>
<Parameter name="type">csv</Parameter>
<Parameter name="inline">
id|name|wkt
1|null island|Point(0 0)
</Parameter>
</Datasource>
</Layer>
<Style name="line">
<Rule>
<LineSymbolizer />
</Rule>
</Style>
<Layer name="line" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
<StyleName>line</StyleName>
<Datasource>
<Parameter name="type">csv</Parameter>
<Parameter name="inline">
wkt|name
LineString(-180.0 -85.0, 0 0, 180.0 0))|line
</Parameter>
</Datasource>
</Layer>
</Map>
import mapnik
m = mapnik.Map(256,256)
mapnik.load_map(m,'map.xml')
m.zoom_all()
# turn off second layer
# and render just first
m.layers[1].status = False
im = mapnik.Image(m.width,m.height)
mapnik.render(m,im)
im.save('point.png')
# then flip active layers
m.layers[0].status = False
m.layers[1].status = True
im = mapnik.Image(m.width,m.height)
mapnik.render(m,im)
im.save('line.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment