User:Moresby/Understanding Mapnik/Placing images at points

From OpenStreetMap Wiki
Jump to: navigation, search
Understanding Mapnik
A Mapnik tutorial
Starting with Python
Using XML and CSS
CartoCSS and PostGIS
021-points-images.png - points shown with red circles

When we want to add points to our map, we are likely to want to represent them as more than just a small black square. Mapnik allows us to put the image of our choice at the points we plot, as we can see from this program.

 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 point symbolizer.
13 point_symbolizer = mapnik.PointSymbolizer(mapnik.PathExpression('circle_red_16x16.png'))
14 
15 # Create a new rule and add the symbolizer.
16 r = mapnik.Rule()
17 r.symbols.append(point_symbolizer)
18 
19 # Create a new style and add the rule.
20 s = mapnik.Style()
21 s.rules.append(r)
22 
23 # Add the style to the map.
24 m.append_style('basic_style', s)
25 
26 # Specify that our data is coming from a CSV file called "data-places.csv".
27 ds = mapnik.CSV(file='data-places.csv')
28 
29 # Create a new layer for the map, called "main_map" and add the data
30 #   source and style to that layer.
31 l = mapnik.Layer('main_map')
32 l.datasource = ds
33 l.styles.append('basic_style')
34 
35 # Add the layer to the map.
36 m.layers.append(l)
37 
38 # Zoom to the part of the map we are interested in.
39 m.zoom_to_box(mapnik.Box2d(0, 0, 480, 320))
40 
41 # Save the map as a PNG image.
42 mapnik.render_to_file(m, '021-points-images.png', 'png')

To run this program, we need the file circle_red_16x16.png which is the image we want to draw on the map. You could use any PNG image for this, but the file used in this demonstration can be obtained by pasting the following data into a file called circle_red_16x16.png.base64:

iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A
/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94MHg86Jm0uBusAAAK/SURBVDjL
dVJbSFRRFF37nHlex3xcL7eihyJqD8OyIoIy6AnZR0EQ+FEREthPIQUifVgQCSGk9BLC/CgkoqiI
LCOSSEqCCKJSjERLHBx1rFGbuY+z+5gyM1s/h3NYa7HPWltiGpaV7UXk3Yfk5cZV7zZNS8spWhHs
PVKu8OCRi1lAMx9Ol2zI29jXX2G4aocfbCoGfhDC4UCgrTU/t7H+Ym03copnMWCmBwX5FYXx+LnU
oKYxwLZtw3EcuI4DKEVxoljnXLP6wL7dV1BV4wCA/CXGw4L8Y6sdt0HTUiQLQawUHGZi1yWXmWzb
Btu2L3tycmfxl4Fvd8/UvFpbkJM0OP+4tWjT2PdbQU0jBQgCwEqRqxRYKShmKNeFYiY7YfFCyyrx
REef3GluGRAAsG4wfCKkaV7FLKb+RARi/jswIrBSRBOTwa29/UfRfEmI4sMHU7OYNzMRExH/kzDR
30kLAcuyMC82vqmw840utvR81v1EWf9rBUQAc/L8BcWMgG1nLo2MGIIBAa8P8HiSxBngaZOAeYrD
YGKAxMvFC6Nx5qjQdcDrBU8zmW5HzIDfD5meDm9GBuI+/1hfetqo6Lh2fWxIuS/IcUjoOsScOYAQ
U2ICgGAQ0jThMQyIUAiBUAhfU0OdrxfMGxYg4ufz59bHhoZcEDGlpIAMA8IwIE0TwjQhdR2QElAK
BLBl29aT7EWXUXPWFgBQfajsRacUtVYkQgCYiACPByQlIMRvISAE+xJxemYaDc15uU//bOL9Vr5X
vr+jqPsTGaPR9QG/X0JKcHKhku1ZFuLj43abkVVXv2bVqfCFxsSUQemu7fjYdN29XVXZ7otE2sXg
YIij0XQ3FvMkJibskcmJyHsp2lqW5FXWrVzeFL7QmNizpxRdXT2z9A4AN5vkyvYOPXd4NJMB8Tkr
c+Rtbs4Ijp90ZlJ/AlJPN18OgwxVAAAAAElFTkSuQmCC
and then running the following command:
base64 -d < circle_red_16x16.png.base64 > circle_red_16x16.png
This image was generated by scaling image icons/png/32x32/others/circle_red.png from the Open Icon Library to 16x16 pixels.

Save this program in a file called 021-points-images.py and run it by typing:

python 021-points-images.py

You should see no error messages, and you should see a new file in your working directory called 021-points-images.png. This is a new map image, and should be a light-coloured rectangle 480 pixels wide by 320 pixels high, with some small red circles marking the positions of the items in the data file, as shown above