# User:Moresby/Understanding Mapnik/Using layers to draw points and lines

040-layers.png - towns and roads on one map

So far we have drawn points or lines, not both. To put both lines and points on the same map, we are going to make use of Mapnik's layers. A map can have several layers, and Mapnik draws each of them in turn, with details on each new layer potentially covering up content from previous layers. This approach is sometimes referred to as the painter's algorithm. Here we put our lines in one layer, and our points in a different layer, ensuring that the points layer is painted after the lines layer, to get the effect we want.

``` 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 point and line symbolizers.
13 point_symbolizer = mapnik.PointSymbolizer(mapnik.PathExpression('circle_red_16x16.png'))
14 line_symbolizer = mapnik.LineSymbolizer()
15
16 # Create new rules and add the symbolizers.
17 r_point = mapnik.Rule()
18 r_point.symbols.append(point_symbolizer)
19 r_line = mapnik.Rule()
20 r_line.symbols.append(line_symbolizer)
21
22 # Create new styles and add the rules.
23 s_point = mapnik.Style()
24 s_point.rules.append(r_point)
25 s_line = mapnik.Style()
26 s_line.rules.append(r_line)
27
28 # Add the styles to the map.
29 m.append_style('point_style', s_point)
30 m.append_style('line_style', s_line)
31
32 # Specify our data sources.
33 ds_point = mapnik.CSV(file='data-places.csv')
34 ds_line = mapnik.CSV(file='data-roads.csv')
35
36 # Create new layers for the map, add the data sources and styles to
37 #   those layers.
38 l_point = mapnik.Layer('point_layer')
39 l_point.datasource = ds_point
40 l_point.styles.append('point_style')
41 l_line = mapnik.Layer('line_layer')
42 l_line.datasource = ds_line
43 l_line.styles.append('line_style')
44
45 # Add the layers to the map. We want the points to appear in front of the
46 #   lines, so we add the line layer first.
47 m.layers.append(l_line)
48 m.layers.append(l_point)
49
50 # Zoom to the part of the map we are interested in.
51 m.zoom_to_box(mapnik.Box2d(0, 0, 480, 320))
52
53 # Save the map as a PNG image.
54 mapnik.render_to_file(m, '040-layers.png', 'png')
```

Save this program in a file called `040-layers.py` and run it by typing:

```python 040-layers.py
```

You should see no error messages, and you should see a new file in your working directory called 040-layers.png. This is a new map image, and should be a light-coloured rectangle 480 pixels wide by 320 pixels high, with a series of red circles interconnected by black lines, as shown above.