Make your first map

rweait's picture

make your first map with your OpenStreetMap tile server
OpenStreetMap provides Free geodata and Free Software tools to use that data. A previous article demonstrates building an OpenStreetMap tile server. This article demonstrates populating it with OpenStreetMap geodata and having your server create a nice picture of a map for you.

It's a big world and the data provided by OpenStreetMap reflects that. While the world isn't getting any larger, the data provided by OpenStreetMap is getting larger. From 2004 to 2009 the data set doubled in size approximately every six months. In March 2009 there were approximately 330 million objects in the database.

map of North America with shaded area related to article
To duplicate every function of OpenStreetMap for the entire globe is a substantial undertaking and requires substantial infrastructure investment. This article covers an interesting but limited set of functions and can be accomplished without building a data centre. The goal is to use a 2006 era PC to create a tile server for a selected area of the planet. To build a tile server for the complete database requires a substantial server.

I've chosen an area in North America that includes Montreal on the east, Washington DC on the south, Minneapolis on the West and Lake Superior on the north. I'm going to have to remember

--bbox -94,38,-71.5,50

your mileage will vary.

If it happens that you are interested in an area that is also a single political boundary, you may find that somebody has already made a database extract for you. Check for one that you like in the list of sources of extracts.


Changes to Mapnik have rendered portions of this tutorial outdated. Be sure to check the README files.

Import data

We put many of the software tools in place in an earlier article. Now we need to load the OpenStreetMap data so that we can use it. There are several approaches that you might take. You might just load all of the OpenStreetMap data at once. That will not be covered in this article as it requires a substantial server. The goals of this article are more modest. We'll look at using Osmosis to import a reduced planet file to OpenStreetMap.

Install dependencies for Osmosis.

sudo apt-get install sun-java6-jdk
This should return without errors.

Get latest osmosis and install it.

tar xvfz osmosis-latest.tar.gz
cd osmosis-0.30

Your version number may vary.

Apply bounding box to planet file.

This reduces the size of the planet file to only the area we will import.
./bin/osmosis --read-xml /home/username/planet-090311.osm.gz --bounding-box left=-94 bottom=38 right=-71.5 top=50 --write-xml /home/username/GreatLakes.osm.gz
This will take some time.


Import a complete extract. Extracts exist for countries and states in the USA. This one is from If your interest lies in a single jurisdiction, one of these extracts may be just right for you.

Import the data

Okay, we've acquired the data and trimmed it to a reasonable size. Now we import it to the local OpenStreetMap PostGIS database. The length of time required will increase with larger datasets and slower computer and less RAM. Count on this running over night. For kicks use the time command to find the duration of this activity.
time ./osm2pgsql --slim -d gis GreatLakes.osm.gz
And this will take a while as well. But you can watch the out progress similar to below.

Using projection SRS 900913 (Spherical Mercator)
Setting up table: planet_osm_point
Setting up table: planet_osm_line
Setting up table: planet_osm_polygon
Setting up table: planet_osm_roads

Reading in file: ../planet-090311.osm
Processing: Node(319877k) Way(11648k) Relation(0k)

Test it! - Make your first map

This is what the tile server is all about, turning data in to images. Okay, let's not get ahead of ourselves here. There are a lot of moving pieces and they all have to be working to get what we expect. When things go wrong, we can get things that are just subtly wrong, or they can be completely Bozo. So one piece at a time.

Test the coastlines

You'll need to edit ~/mapnik/set-mapnik-env to let it know that your database is called "gis" not "osm" as shown here.

# This is the name of the database where the OSM data is stored.
export MAPNIK_DBNAME='osm'

Change it to gis. You should not need to make any other changes in ~/mapnik/set-mapnik-env.

Make a testing copy of one of the default files.
cd ~/mapnik

Edit your copy ~/mapnik/

    # Change the following for different bounding boxes and zoom levels
    # Start with an overview
    # World
    bbox = (-180.0,-90.0, 180.0,90.0)

    render_tiles(bbox, mapfile, tile_dir, 0, 5, "World")

Change the 5 to a zero for now. Disable the render_tiles lines that follow this section by adding a # at the beginning of each line. You want a quick test render at first. Save it.

Set up and execute the render with these three commands. Do not omit the leading periods on lines two and three.
source set-mapnik-env
./customize-mapnik-map >$MAPNIK_MAP_FILE

The last line above should generate the five following lines.

render_tiles( (-180.0, -90.0, 180.0, 90.0) /home/username/mapnik/osm.xml /home/username/mapnik/tiles/ 0 0 World )
World [ 0 - 0 ]:  0 0 0 p: (-180.0, -85.051128779806589) (180.0, 85.051128779806604) 
World [ 0 - 0 ]:  0 0 1 p: (-180.0, -89.990752516489039) (180.0, -85.051128779806589) 
World [ 0 - 0 ]:  0 1 0 p: (180.0, -85.051128779806589) (540.0, 85.051128779806604)  
World [ 0 - 0 ]:  0 1 1 p: (180.0, -89.990752516489039) (540.0, -85.051128779806589) 

And more importantly, the script will have created your first four map tiles in the ~/mapnik/tiles directory tree. Go have a look now at ~/mapnik/tiles/0/0/0.png It should be a little tiny map of the World. Like this.

If so, pat yourself on the back. Your tile server has just created tiles to serve.
You'll probably stop reading here and run off to do more rendering. I understand. Come back and finish the article later. You are giddy with your new mappy-server-ish-ness. And who would deny you that joy? Not I, good neo-cartographer, not I.

What if it didn't work?

No little World map? Not to worry. Did you get a tile at all? Perhaps an all blue tile of about 1209 bytes? Double check the World Boundaries section in . The files from three sources should all end up in the ~/mapnik/world_boundaries directory. Not in ~/mapnik. When you are ready to try again, delete the tiles/0/0/0.png first. The script won't replace a tile that exists.

Did you get a warning or error when you rendered tiles? Use #osm or comments below to discuss. I'll put more debugging into the article as we go.

Test your area of interest

What? It did work? And you are still reading this? Fantastic. Glad to have you back after your celebration. Now is a good time to check your area of interest for map data beyond the outlines of the continents.

Make a copy of for your area of interest.

Edit . It contains a block of comments in the middle, with some default rendering instructions.

#  Change this to the bounding box you want
ll = (-6.5, 49.5, 2.1, 59)

z = 10
imgx = 500 * z
imgy = 1000 * z

Change the bounding box ll = to your bounding box for your area of interest. Change the constants from 500 and 1000 to 50 each.

imgx = 50 * z
imgy = 50 * z

Save it.

Set up and execute the render with these three commands. Do not omit the leading periods on lines two and three.
source set-mapnik-env
./customize-mapnik-map >$MAPNIK_MAP_FILE

This should return without errors and create a new file, ~/mapnik/image.png Go have a look. I'll be right here listening for your exclamations of delight. Here the one from my example.
first area map to check data upload

How was that? Easy, right?

Update your data

Who am I kidding? You won't read this far today. You're off rendering the planet to zoom = infinity[1]. Updates will go in the next article.

Credits and References

This tutorial is one of a series of tutorials for OpenStreetMap beginners. Find more tutorials here.

Map images and data © 2009 CCBYSA OpenStreetMap and contributors.
The great gang at #osm and the talk mailing list.
Thanks to bretth, cmarqu_ and firefishy
Thanks to Steve Coast.
Thanks dbaker for copy editing and clarifying suggestions.

[1] Try to resist the temptation to render to the planet to scale 1:1. You'll have trouble finding a printer large enough, and if you print it, where would you put it?


This archied article was orignailly published on Fri, 03/20/2009 - 15:49.