User:Moresby/Understanding Mapnik/CSS styling with Cascadenik

From OpenStreetMap Wiki
Jump to: navigation, search
Understanding Mapnik
A Mapnik tutorial
Starting with Python
Using XML and CSS
CartoCSS and PostGIS
260-text.png - map generated using Cascadenik

First published in 2010, Cascadenik was an early attempt to apply some of the concepts of Cascading Style Sheets (CSS) to Mapnik. A map design is specified using a CSS-type syntax, and Cascadenik converts this into the appropriate XML input for Mapnik. Development of Cascadenik ceased in March 2013, although the ideas behind it fundamentally influenced the design of CartoCSS.

For example, the following Cascadenik input files provides a pretty good version of our example map. This is 260-text.mml:

 1 <?xml version='1.0'?>
 2 <Map srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
 3   <Stylesheet src='260-text.mss'/>
 4   <Layer name='line_layer' class='road_lines road_labels'>
 5     <Datasource>    
 6       <Parameter name='file'>data-roads.csv</Parameter>
 7       <Parameter name='type'>csv</Parameter>
 8     </Datasource>
 9   </Layer>
10   <Layer name='point_layer' class='town_points town_labels city_points city_labels'>
11     <Datasource>
12       <Parameter name='file'>data-places.csv</Parameter>
13       <Parameter name='type'>csv</Parameter>
14     </Datasource>   
15   </Layer>
16 </Map>

And this is 260-text.mss:

 1 /* Set background colour to 'ghostwhite'. */
 2 Map {
 3   map-bgcolor:      #f8f8ff;
 4 }     
 5 
 6 /* Specify how to draw the road labels. */
 7 .road_labels [type != 'rail'] name {
 8   text-face-name:   'DejaVu Sans Book';
 9   text-size:        10;
10   text-fill:        #000;
11 
12   text-placement:   line;
13   text-halo-radius: 2;
14 }
15 
16 /* For rail, we have a wide dashed line. */
17 .road_lines [type = 'rail'] {
18   line-color:       #000;
19   line-width:       2;
20   line-dasharray:   6, 2;
21 }
22 
23 /* Roads are narrow, black lines. */
24 .road_lines [type = 'road'] {
25   line-color:       #000;
26   line-width:       1;
27 }
28 
29 /* Main roads are wider, black lines. */
30 .road_lines [type = 'mainroad'] {
31   line-color:       #000;
32   line-width:       2;
33 }
34 
35 /* Main roads are wide, light blue lines. */
36 .road_lines [type = 'motorway'] {
37   line-color:       #add8e6;
38   line-width:       4;
39 }
40 
41 /* Towns are marked with a small circle. */
42 .town_points [type = 'town'] {
43   point-file:       url('circle_red_8x8.png');
44 }
45 
46 /* Towns names are drawn in black. */
47 .town_labels [type = 'town'] name {
48   text-face-name:   'DejaVu Sans Book';
49   text-size:        10;
50   text-fill:        #000;
51 
52   text-halo-radius: 3;
53   text-dy:          7;
54 }
55 
56 /* Cities are marked with a larger circle. */
57 .city_points [type = 'city'] {
58   point-file:       url('circle_red_16x16.png');
59 }
60 
61 /* City names are drawn in red. */
62 .city_labels [type = 'city'] name {
63   text-face-name:   'DejaVu Sans Book';
64   text-size:        12;
65   text-fill:        #f00;
66 
67   text-halo-radius: 3;
68   text-dy:          11;
69 }

Save the first of these files as 260-text.mml and the second as 260-text.mss and run:

python cascadenik-compile.py 260-text.mml 260-text.xml

This will create the following Mapnik XML input file 260-text.xml:

<Map background-color="rgb(248,248,255)" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
  <Style filter-mode="first" name="line style 3">
    <Rule name="rule 1">
      <Filter>([type]='mainroad')</Filter>
      <LineSymbolizer stroke-width="2" />
    </Rule>
    <Rule name="rule 2">
      <Filter>([type]='motorway')</Filter>
      <LineSymbolizer stroke="rgb(173,216,230)" stroke-width="4" />
    </Rule>
    <Rule name="rule 3">
      <Filter>([type]='rail')</Filter>
      <LineSymbolizer stroke-dasharray="6, 2" stroke-width="2" />
    </Rule>
    <Rule name="rule 4">
      <Filter>([type]='road')</Filter>
      <LineSymbolizer />
    </Rule>
  </Style>
  <Style filter-mode="first" name="point style 15">
    <Rule name="rule 8">
      <Filter>([type]='city')</Filter>
      <PointSymbolizer file="/home/moresby/work/circle_red_16x16.png" />
    </Rule>
    <Rule name="rule 9">
      <Filter>([type]='town')</Filter>
      <PointSymbolizer file="/home/moresby/work/circle_red_8x8.png" />
    </Rule>
  </Style>
  <Style filter-mode="first" name="text style 14 (name)">
    <Rule name="rule 6">
      <Filter>([type]='city')</Filter>
      <TextSymbolizer clip="false" dy="11" face-name="DejaVu Sans Book" fill="rgb(255,0,0)" halo-radius="3" size="12">[name]</TextSymbolizer>
    </Rule>
    <Rule name="rule 7">
      <Filter>([type]='town')</Filter>
      <TextSymbolizer clip="false" dy="7" face-name="DejaVu Sans Book" halo-radius="3">[name]</TextSymbolizer>
    </Rule>
  </Style>
  <Style filter-mode="first" name="text style 6 (name)">
    <Rule name="rule 5">
      <Filter>not (([type]='rail'))</Filter>
      <TextSymbolizer clip="false" face-name="DejaVu Sans Book" halo-radius="2" placement="line">[name]</TextSymbolizer>
    </Rule>
  </Style>
  <Layer name="layer 8" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
    <StyleName>line style 3</StyleName>
    <StyleName>text style 6 (name)</StyleName>
    <Datasource>
      <Parameter name="file">/home/moresby/work/data-roads.csv</Parameter>
      <Parameter name="type">csv</Parameter>
    </Datasource>
  </Layer>
  <Layer name="layer 16" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
    <StyleName>text style 14 (name)</StyleName>
    <StyleName>point style 15</StyleName>
    <Datasource>
      <Parameter name="file">/home/moresby/work/data-places.csv</Parameter>
      <Parameter name="type">csv</Parameter>
    </Datasource>
  </Layer>
</Map>

Now use Mapnik to turn this into a map:

python generate-map.py 260-text.xml

You should see no error messages, and you should see a new file in your working directory called 260-text.png. This should be almost identical to the previous map we produced, as shown above.

External links