User:Moresby/Understanding Mapnik/Setting a background colour

From OpenStreetMap Wiki
Jump to: navigation, search
Understanding Mapnik
A Mapnik tutorial
Starting with Python
Using XML and CSS
CartoCSS and PostGIS
010-background.png - a completely blank map

A simple way of checking that you have Mapnik set up correctly is to start with the very basics. Here we create a map with nothing on it - just a background colour.

 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 # Save the map as a PNG image.
13 mapnik.render_to_file(m, '010-background.png', 'png')

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

python 010-background.py

If everything is set up correctly, you should see no error messages, and you should see a new file in your working directory called 010-background.png. This is a new map image, and should be a light-coloured rectangle 480 pixels wide by 320 pixels high, as shown above. If this works OK, you are ready to move on to the next step: plotting points.