User:Moresby/Understanding Mapnik/Setting a background colour

From OpenStreetMap Wiki
Jump to navigation Jump to 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
 3 # Load the Python mapnik libraries.
 4 import mapnik
 6 # Create a new map.
 7 m = mapnik.Map(480, 320)
 9 # Set the background colour.
10 m.background = mapnik.Color('ghostwhite')
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 and run it by typing:


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.