finish edit pass
parent
18ce892100
commit
05e534ebb8
1 changed files with 107 additions and 135 deletions
|
@ -2,21 +2,27 @@
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
This tutorial is designed to introduce a few more advanced Mapnik styling features, mainly `Rules` and `Filters`.
|
This tutorial is designed to introduce a few more advanced Mapnik styling features, mainly `Rules` and `Filters`. This XML file is only compatible with Mapnik 2.x and above.
|
||||||
|
|
||||||
|
This script should result in a graphic similar to:
|
||||||
|
|
||||||
|
[[/images/world_population.png]]
|
||||||
|
|
||||||
|
* Note: text labeling positions may be different between Mapnik versions.
|
||||||
|
|
||||||
## Step 1: data
|
## Step 1: data
|
||||||
|
|
||||||
For this tutorial we'll use the same world borders shapefile from natural earth used for the first two tutorials. Refer to either [Tutorial 1](GettingStartedInPython) or [Tutorial 2](XMLGettingStarted) for details.
|
For this tutorial we'll use the same world borders shapefile from natural earth used for the first two tutorials. Refer to either [Tutorial 1](GettingStartedInPython) or [Tutorial 2](XMLGettingStarted) for details.
|
||||||
|
|
||||||
This script should result in a graphic like this:
|
## Step 2: Script
|
||||||
|
|
||||||
[[/images/world_population_minimized.png]]
|
Save the below code into a file called `world_population.py`:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import mapnik
|
import mapnik
|
||||||
mapfile = "population.xml"
|
mapfile = "world_population.xml"
|
||||||
m = mapnik.Map(1400, 600)
|
m = mapnik.Map(1400, 600)
|
||||||
mapnik.load_map(m, mapfile)
|
mapnik.load_map(m, mapfile)
|
||||||
bbox = mapnik.Envelope(mapnik.Coord(-180.0, -75.0), mapnik.Coord(180.0, 90.0))
|
bbox = mapnik.Envelope(mapnik.Coord(-180.0, -75.0), mapnik.Coord(180.0, 90.0))
|
||||||
|
@ -24,94 +30,62 @@ m.zoom_to_box(bbox)
|
||||||
mapnik.render_to_file(m, 'world_population.png', 'png')
|
mapnik.render_to_file(m, 'world_population.png', 'png')
|
||||||
```
|
```
|
||||||
|
|
||||||
And here is the xml file:
|
## Step 3: Stylesheet
|
||||||
|
|
||||||
|
Save the below XML into a file called `world_population.xml` in the same directory as the `world_population.py` python script:
|
||||||
|
|
||||||
```xml
|
```xml
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!DOCTYPE Map>
|
<!DOCTYPE Map>
|
||||||
<!-- Sample Mapnik XML template by Dane Springmeyer -->
|
<!-- Sample Mapnik XML template by Dane Springmeyer -->
|
||||||
<Map bgcolor="white" srs="+proj=latlong +datum=WGS84">
|
<Map srs="+proj=latlong +datum=WGS84" background-color="white" minimum-version="0.7.2">
|
||||||
|
|
||||||
<Style name="population">
|
<Style name="population">
|
||||||
|
|
||||||
<Rule>
|
|
||||||
<!-- Built from Seven Class sequential YIGnBu from www.colorbrewer.org -->
|
<!-- Built from Seven Class sequential YIGnBu from www.colorbrewer.org -->
|
||||||
<!-- Quantile breaks originally from QGIS layer classification -->
|
<!-- Quantile breaks originally from QGIS layer classification -->
|
||||||
<Filter>[POP2005] = 0 </Filter>
|
<Rule>
|
||||||
<PolygonSymbolizer>
|
<Filter>[POP_EST] > -1 and [POP_EST] < 15000</Filter>
|
||||||
<CssParameter name="fill">#ffffcc</CssParameter>
|
<PolygonSymbolizer fill="#c7e9b4"/>
|
||||||
</PolygonSymbolizer>
|
<LineSymbolizer stroke="black" stroke-width=".1"/>
|
||||||
<!-- Outlines for Antarctica look good -->
|
|
||||||
<LineSymbolizer>
|
|
||||||
<CssParameter name="stroke">black</CssParameter>
|
|
||||||
<CssParameter name="stroke-width">.1</CssParameter>
|
|
||||||
</LineSymbolizer>
|
|
||||||
</Rule>
|
</Rule>
|
||||||
|
|
||||||
<Rule>
|
<Rule>
|
||||||
<Filter>[POP2005] > 0 and [POP2005] < 15000</Filter>
|
<Filter>[POP_EST] >= 15000 and [POP_EST] < 255000</Filter>
|
||||||
<PolygonSymbolizer>
|
<PolygonSymbolizer fill="#7fcdbb"/>
|
||||||
<CssParameter name="fill">#c7e9b4</CssParameter>
|
<LineSymbolizer stroke="black" stroke-width=".1"/>
|
||||||
</PolygonSymbolizer>
|
|
||||||
<!-- Outlines for Antarctica look good -->
|
|
||||||
<LineSymbolizer>
|
|
||||||
<CssParameter name="stroke">black</CssParameter>
|
|
||||||
<CssParameter name="stroke-width">.1</CssParameter>
|
|
||||||
</LineSymbolizer>
|
|
||||||
</Rule>
|
</Rule>
|
||||||
|
|
||||||
<Rule>
|
<Rule>
|
||||||
<Filter>[POP2005] >= 15000 and [POP2005] < 255000</Filter>
|
<Filter>[POP_EST] >= 255000 and [POP_EST] < 1300000</Filter>
|
||||||
<PolygonSymbolizer>
|
<PolygonSymbolizer fill="#1d91c0"/>
|
||||||
<CssParameter name="fill">#7fcdbb</CssParameter>
|
|
||||||
</PolygonSymbolizer>
|
|
||||||
</Rule>
|
</Rule>
|
||||||
|
|
||||||
<Rule>
|
<Rule>
|
||||||
<Filter>[POP2005] >= 255000 and [POP2005] < 1300000</Filter>
|
<Filter>[POP_EST] >= 1300000 and [POP_EST] < 4320000</Filter>
|
||||||
<PolygonSymbolizer>
|
<PolygonSymbolizer fill="#41b6c3"/>
|
||||||
<CssParameter name="fill">#1d91c0</CssParameter>
|
|
||||||
</PolygonSymbolizer>
|
|
||||||
</Rule>
|
</Rule>
|
||||||
|
|
||||||
<Rule>
|
<Rule>
|
||||||
<Filter>[POP2005] >= 1300000 and [POP2005] < 4320000</Filter>
|
<Filter>[POP_EST] >= 4320000 and [POP_EST] < 9450000</Filter>
|
||||||
<PolygonSymbolizer>
|
<PolygonSymbolizer fill="#225ea8"/>
|
||||||
<CssParameter name="fill">#41b6c3</CssParameter>
|
|
||||||
</PolygonSymbolizer>
|
|
||||||
</Rule>
|
</Rule>
|
||||||
|
|
||||||
<Rule>
|
<Rule>
|
||||||
<Filter>[POP2005] >= 4320000 and [POP2005] < 9450000</Filter>
|
<Filter>[POP_EST] >= 9450000 and [POP_EST] < 25650000</Filter>
|
||||||
<PolygonSymbolizer>
|
<PolygonSymbolizer fill="#225ea8"/>
|
||||||
<CssParameter name="fill">#225ea8</CssParameter>
|
|
||||||
</PolygonSymbolizer>
|
|
||||||
</Rule>
|
</Rule>
|
||||||
|
|
||||||
<Rule>
|
<Rule>
|
||||||
<Filter>[POP2005] >= 9450000 and [POP2005] < 25650000</Filter>
|
<Filter>[POP_EST] >= 25650000 and [POP_EST] < 1134000000</Filter>
|
||||||
<PolygonSymbolizer>
|
<PolygonSymbolizer fill="#122F7F"/>
|
||||||
<CssParameter name="fill">#225ea8</CssParameter>
|
|
||||||
</PolygonSymbolizer>
|
|
||||||
</Rule>
|
</Rule>
|
||||||
|
|
||||||
<Rule>
|
<Rule>
|
||||||
<Filter>[POP2005] >= 25650000 and [POP2005] < 1134000000</Filter>
|
<ElseFilter/>
|
||||||
<PolygonSymbolizer>
|
<!-- This will catch all other values - in this case just India and China -->
|
||||||
<CssParameter name="fill">#122F7F</CssParameter>
|
|
||||||
</PolygonSymbolizer>
|
|
||||||
</Rule>
|
|
||||||
|
|
||||||
<Rule>
|
|
||||||
<ElseFilter/> <!-- This will catch all other values - in this case just India and China -->
|
|
||||||
<!-- A dark red polygon fill and black outline is used here to highlight these two countries -->
|
<!-- A dark red polygon fill and black outline is used here to highlight these two countries -->
|
||||||
<PolygonSymbolizer>
|
<PolygonSymbolizer fill="darkred"/>
|
||||||
<CssParameter name="fill">darkred</CssParameter>
|
<LineSymbolizer stroke="black" stroke-width=".7"/>
|
||||||
</PolygonSymbolizer>
|
|
||||||
<LineSymbolizer>
|
|
||||||
<CssParameter name="stroke">black</CssParameter>
|
|
||||||
<CssParameter name="stroke-width">.7</CssParameter>
|
|
||||||
</LineSymbolizer>
|
|
||||||
</Rule>
|
</Rule>
|
||||||
|
|
||||||
</Style>
|
</Style>
|
||||||
|
@ -120,28 +94,28 @@ And here is the xml file:
|
||||||
<Rule>
|
<Rule>
|
||||||
<!-- Only label those countries with over 9 Million People -->
|
<!-- Only label those countries with over 9 Million People -->
|
||||||
<!-- Note: Halo and Fill are reversed to try to make them subtle -->
|
<!-- Note: Halo and Fill are reversed to try to make them subtle -->
|
||||||
<Filter>[POP2005] >= 4320000 and [POP2005] < 9450000</Filter>
|
<Filter>[POP_EST] >= 4320000 and [POP_EST] < 9450000</Filter>
|
||||||
<TextSymbolizer name="NAME" face_name="DejaVu Sans Bold" size="7" fill="black" halo_fill= "#DFDBE3" halo_radius="1" wrap_width="20" spacing="5" allow_overlap="false" avoid_edges="false" min_distance="10"/>
|
<TextSymbolizer size="7" fill="black" face-name="DejaVu Sans Bold" halo-fill="#DFDBE3" halo-radius="1" wrap-width="20">[NAME]</TextSymbolizer>
|
||||||
</Rule>
|
</Rule>
|
||||||
|
|
||||||
<Rule>
|
<Rule>
|
||||||
<!-- Only label those countries with over 9 Million People -->
|
<!-- Only label those countries with over 9 Million People -->
|
||||||
<!-- Note: Halo and Fill are reversed to try to make them subtle -->
|
<!-- Note: Halo and Fill are reversed to try to make them subtle -->
|
||||||
<Filter>[POP2005] >= 9450000 and [POP2005] < 25650000</Filter>
|
<Filter>[POP_EST] >= 9450000 and [POP_EST] < 25650000</Filter>
|
||||||
<TextSymbolizer name="NAME" face_name="DejaVu Sans Book" size="9" fill="black" halo_fill= "#DFDBE3" halo_radius="1" wrap_width="20" spacing="5" allow_overlap="false" avoid_edges="false" min_distance="10"/>
|
<TextSymbolizer size="9" fill="black" face-name="DejaVu Sans Book" halo-fill="#DFDBE3" halo-radius="1" wrap-width="20">[NAME]</TextSymbolizer>
|
||||||
</Rule>
|
</Rule>
|
||||||
|
|
||||||
<Rule>
|
<Rule>
|
||||||
<!-- Those with over 25 Million get larger labels -->
|
<!-- Those with over 25 Million get larger labels -->
|
||||||
<Filter>[POP2005] >= 25650000 and [POP2005] < 1134000000</Filter>
|
<Filter>[POP_EST] >= 25650000 and [POP_EST] < 1134000000</Filter>
|
||||||
<TextSymbolizer name="NAME" face_name="DejaVu Sans Book" size="12" fill="white" halo_fill= "#2E2F39" halo_radius="1" wrap_width="20" spacing="5" allow_overlap="false" avoid_edges="true" min_distance="10"/>
|
<TextSymbolizer size="12" fill="white" face-name="DejaVu Sans Book" halo-fill="#2E2F39" halo-radius="1" wrap-width="20">[NAME]</TextSymbolizer>
|
||||||
</Rule>
|
</Rule>
|
||||||
|
|
||||||
<Rule>
|
<Rule>
|
||||||
<!-- Those with over 25 Million get larger labels -->
|
<!-- Those with over 25 Million get larger labels -->
|
||||||
<!-- Note: allow_overlap is true here to allow India to sneak through -->
|
<!-- Note: allow_overlap is true here to allow India/China to sneak through -->
|
||||||
<Filter>[POP2005] >= 1134000000</Filter>
|
<Filter>[POP_EST] >= 1134000000</Filter>
|
||||||
<TextSymbolizer name="NAME" face_name="DejaVu Sans Book" size="15" fill="white" halo_fill= "black" halo_radius="1" wrap_width="20" spacing="5" allow_overlap="true" avoid_edges="true" min_distance="10"/>
|
<TextSymbolizer size="15" fill="white" face-name="DejaVu Sans Book" halo-fill="black" halo-radius="1" wrap-width="20" allow-overlap="true" avoid-edges="true">[NAME]</TextSymbolizer>
|
||||||
</Rule>
|
</Rule>
|
||||||
</Style>
|
</Style>
|
||||||
|
|
||||||
|
@ -152,9 +126,7 @@ And here is the xml file:
|
||||||
<StyleName>countries_label</StyleName>
|
<StyleName>countries_label</StyleName>
|
||||||
<Datasource>
|
<Datasource>
|
||||||
<Parameter name="type">shape</Parameter>
|
<Parameter name="type">shape</Parameter>
|
||||||
<!-- FIXME -->
|
<Parameter name="file">ne_110m_admin_0_countries.shp</Parameter>
|
||||||
<!-- Note: 'TM_WORLD_BORDERS_SIMPL-0.3' is the name of the shapefile (without the .shp file extension) -->
|
|
||||||
<Parameter name="file">/PATH/TO/THE/TM_WORLD_BORDERS_SIMPL-0.3</Parameter>
|
|
||||||
</Datasource>
|
</Datasource>
|
||||||
</Layer>
|
</Layer>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue