fixup styles and data step

springmeyer 2012-01-09 09:48:35 -08:00
parent b52a2a8dfc
commit 0614951efb

@ -1,25 +1,24 @@
# Tutorial 2 -- 'Hello,world!' using an XML stylesheet
# Tutorial 2 -- 'Hello world' using an XML stylesheet
## Overview
Make sure you have mapnik installed and you've successfully run through [Getting Started Python Tutorial](GettingStartedInPython).
Make sure you have mapnik (and the python bindings) installed and you've successfully run through [Getting Started Python Tutorial](GettingStartedInPython).
* This page will guide you through using the Mapnik python bindings along with a separate XML file for your map styles.
* This is a useful approach to manage your map styles/rules separately from your python code, and can be advantageous for very complex formatting.
* This tutorial expects that you are running Mapnik 2.x or greater. The command `mapnik-config -v` will show you which version you are running.
Two examples will be shown:
Two examples are covered in this tutorial:
1) An XML stylesheet that exactly matches the map output from the pure python example in [Getting Started Python Tutorial](GettingStartedInPython).
1) An XML stylesheet is rendering that exactly matches the map output from the pure python example in [Getting Started Python Tutorial](GettingStartedInPython).
2) An XML stylesheet that uses a world borders dataset with population attributes to create a chloropleth map (aka thematic) by population size.
2) An XML stylesheet is showcased that uses a world borders dataset with population attributes to create a chloropleth map (aka thematic) by population size.
## Step 1
### Hello World XML
First you will need a python script that sets the basic map parameters and points to the XML stylesheet
First you will need a python script that sets the basic map parameters and points to the XML stylesheet. Copy the code below and save to a file called `world_map.py`.
```python
#!/usr/bin/env python
@ -30,24 +29,25 @@ m = mapnik.Map(600, 300)
mapnik.load_map(m, stylesheet)
m.zoom_all()
mapnik.render_to_file(m, image)
print "rendered image to '%s'" % image
```
* Copy the above code and save to a file called `world_map.py`
Now, we need some data to render, let's use a shapefile of world border polygons from http://www.naturalearthdata.com. You can download the data from this wiki's local copy [here](data/) or directly from the [Natural Earth Data site](http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/110m-admin-0-countries.zip). Unzip the archive and it should produce four files like `ne_110m_admin_0_countries.shp, ne_110m_admin_0_countries.shx, ne_110m_admin_0_countries.dbf, and ne_110m_admin_0_countries.prj`
Next you will need to create the `world_style.xml` file referenced in the `world_map.py` script.
To download and unzip on the command line with the do:
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
Next, create the `world_style.xml` file referenced in the `world_map.py` script. Copy this XML and save to a file called `world_style.xml` in the same directory as `world_map.py` script.
```xml
<Map bgcolor="steelblue" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
<Map background-color="steelblue" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
<Style name="My Style">
<Rule>
<PolygonSymbolizer>
<CssParameter name="fill">#f2eff9</CssParameter>
</PolygonSymbolizer>
<LineSymbolizer>
<CssParameter name="stroke">rgb(50%,50%,50%)</CssParameter>
<CssParameter name="stroke-width">0.1</CssParameter>
</LineSymbolizer>
<PolygonSymbolizer fill="#f2eff9" />
<LineSymbolizer stroke="rgb(50%,50%,50%)" stroke-width="0.1" />
</Rule>
</Style>
@ -55,16 +55,14 @@ Next you will need to create the `world_style.xml` file referenced in the `world
<StyleName>My Style</StyleName>
<Datasource>
<Parameter name="type">shape</Parameter>
<Parameter name="file">world_borders.shp</Parameter>
<Parameter name="file">ne_110m_admin_0_countries.shp</Parameter>
</Datasource>
</Layer>
</Map>
```
* Copy this XML and save to a file called `world_style.xml` beside the `world_map.py` script.
Now run that script:
Now run the python script:
python world_map.py