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.

#!/usr/bin/python

# Load the Python mapnik libraries.
import mapnik

# Create a new map.
m = mapnik.Map(480, 320)

# Set the background colour.
m.background = mapnik.Color('ghostwhite')

# Save the map as a PNG image.
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.