User:Moresby/Understanding Mapnik/Using filters to represent data

From OpenStreetMap Wiki
Jump to: navigation, search
Understanding Mapnik
A Mapnik tutorial
Starting with Python
Using XML and CSS
CartoCSS and PostGIS
050-filters.png - towns, cities, roads and motorways marked according to size

So far we have plotted all features on a map in the same way: all points are drawn the same, or all roads are drawn the same. Maps often use different styles of drawing to represent different categories of features: points marking towns can be different colours or sizes depending on population, say, or roads can be different widths depending on their importance. The data files we have been using have some very basic categorisations of the towns and roads, and here we use those labels to affect the way they are represented on the map, using Mapnik filters.

 1 #!/usr/bin/python
 2 
 3 # Load the Python mapnik libraries.
 4 import mapnik
 5 
 6 # Create a new map.
 7 m = mapnik.Map(480, 320)
 8 
 9 # Set the background colour.
10 m.background = mapnik.Color('ghostwhite')
11 
12 # Create a stroke object for railways.
13 stroke_rail = mapnik.Stroke()
14 stroke_rail.color = mapnik.Color('black')
15 stroke_rail.dasharray = [ (6, 2) ]
16 stroke_rail.width = 2
17 
18 # Create point and line symbolizers.
19 point_symbolizer_city = mapnik.PointSymbolizer(mapnik.PathExpression('circle_red_16x16.png'))
20 point_symbolizer_town = mapnik.PointSymbolizer(mapnik.PathExpression('circle_red_8x8.png'))
21 line_symbolizer_rail = mapnik.LineSymbolizer(stroke_rail)
22 line_symbolizer_road = mapnik.LineSymbolizer()
23 line_symbolizer_mainroad = mapnik.LineSymbolizer(mapnik.Color('black'), 2)
24 line_symbolizer_motorway = mapnik.LineSymbolizer(mapnik.Color('lightblue'), 4)
25 
26 # Create new rules and add the symbolizers.
27 r_city = mapnik.Rule()
28 r_city.symbols.append(point_symbolizer_city)
29 r_city.filter = mapnik.Filter('[type] = "city"')
30 
31 r_town = mapnik.Rule()
32 r_town.symbols.append(point_symbolizer_town)
33 r_town.filter = mapnik.Filter('[type] = "town"')
34 
35 r_rail = mapnik.Rule()
36 r_rail.symbols.append(line_symbolizer_rail)
37 r_rail.filter = mapnik.Filter('[type] = "rail"')
38 
39 r_road = mapnik.Rule()
40 r_road.symbols.append(line_symbolizer_road)
41 r_road.filter = mapnik.Filter('[type] = "road"')
42 
43 r_mainroad = mapnik.Rule()
44 r_mainroad.symbols.append(line_symbolizer_mainroad)
45 r_mainroad.filter = mapnik.Filter('[type] = "mainroad"')
46 
47 r_motorway = mapnik.Rule()
48 r_motorway.symbols.append(line_symbolizer_motorway)
49 r_motorway.filter = mapnik.Filter('[type] = "motorway"')
50 
51 # Create new styles and add the rules.
52 s_point = mapnik.Style()
53 s_point.rules.append(r_town)
54 s_point.rules.append(r_city)
55 
56 s_line = mapnik.Style()
57 s_line.rules.append(r_rail)
58 s_line.rules.append(r_road)
59 s_line.rules.append(r_mainroad)
60 s_line.rules.append(r_motorway)
61 
62 # Add the styles to the map.
63 m.append_style('point_style', s_point)
64 m.append_style('line_style', s_line)
65 
66 # Specify our data sources.
67 ds_point = mapnik.CSV(file='data-places.csv')
68 ds_line = mapnik.CSV(file='data-roads.csv')
69 
70 # Create new layers for the map, add the data sources and styles to
71 #   those layers.
72 l_point = mapnik.Layer('point_layer')
73 l_point.datasource = ds_point
74 l_point.styles.append('point_style')
75 l_line = mapnik.Layer('line_layer')
76 l_line.datasource = ds_line
77 l_line.styles.append('line_style')
78 
79 # Add the layers to the map. We want the points to appear in front of the
80 #   lines, so we add the line layer first.
81 m.layers.append(l_line)
82 m.layers.append(l_point)
83 
84 # Zoom to the part of the map we are interested in.
85 m.zoom_to_box(mapnik.Box2d(0, 0, 480, 320))
86 
87 # Save the map as a PNG image.
88 mapnik.render_to_file(m, '050-filters.png', 'png')

Save this program in a file called 050-filters.py and run it by typing:

python 050-filters.py

You should see no error messages, and you should see a new file in your working directory called 050-filters.png. This is a new map image, and should be a light-coloured rectangle 480 pixels wide by 320 pixels high, with towns, cities and varieties of roads separately shown, as shown above.