Build your own OpenStreetMap Server

Build your own OpenStreetMap server.
Build your own what?
OpenStreetMap is the editable World map of everything. It is the Wikipedia of maps. It is to other on-line maps as Wikipedia is to Britannica. And it is awesome in every possible way.
OpenStreetMap is a massive project that started as Steve Coast's, frame-breaking idea in 2004. If I make a map of my neighborhood and give it away, and you make a map of your neighborhood and give it away, then we both have better maps.
As of March 2009 there are over 100,000 contributors making maps of their neighborhoods
and contributing them to this World wide effort.
OpenStreetMap makes the data and the software available to you with Free Software and free data licenses so that you can use, learn from, teach with, improve upon and share with others what you gain from OpenStreetMap. And you can build your own local copy of OpenStreetMap for your business, school, community group or personal interests.
The project operates on a massive scale as there is an incredible amount of data, there is more data every day, and there are more people using the data every day. OpenStreetMap has to run on several servers, including a handful of API servers and separate database, development, web and tile servers. This article does not cover the creation of a complete OSM datacentre.
It does cover creating a single server
from a typical PC circa 2006. These instructions build what OpenStreetMap calls a tile server
. That is, a computer that uses the OSM data set to create map images that are suitable for a web site. Not every OpenStreetMap function is supported, but you will be able to create a local map, keep it up to date and customize it for your own purposes.
The key compromise that is required to be able to build this system on a non-super-computer is that we'll only build the map for a small part of the world.
Why would I build my own?
Why indeed? OpenStreetMap.org is already freely available on the internet. Why not just use that? You can and you should. Eventually you may come up with an idea. You might want to make the map work a little differently for you. You might want a map for a special purpose.
Perhaps for cycling, http://www.opencyclemap.org/ OpenCycleMap is a wonderful example of what you can do with the tools and data of OpenStreetMap catalyzed by an idea. Created by Andy Allen and Dave Stubbs, OpenCycleMap uses OpenStreetMap data, then displays it in a way that is useful to cyclists with the emphasis placed on cycle trails, bike shops and bike parking. They've also added elevation contours and hill-shading as cyclists care about hills. Sometimes they are looking for a good challenging climb, and other times they just want to get home with the groceries. The brilliant work of the OpenCycleMap team was recognized with a Commendation from the British Cartographic Society as well as the prestigious Lolcat of Awesomness from the OpenStreetMap community at large.
Or maybe you need to have access to you map even when your internet provider is down. Or when the power is off. Or both. It won't take much for you to see the benefit of having your own piece of OpenStreetMap infrastructure. All you need to start is an idea and the thirst for knowledge.
There are a lot of moving parts to OpenStreetMap. I hope that these steps will make it easier for you to get your first map working. This article is intended to get from bare iron to a working local OpenStreetMap tile server. This one will collect OpenStreetMap data and allow you to render that data into images suitable for use on a web site. Future articles will cover how to use your new server and several customizations that you'll want to try.
Let's get started, shall we?
Install Ubuntu server
I used Hardy Heron 8.04 LTS for this tutorial. Choose the LAMP server and the ssh server.
This Ubuntu Hardy LAMP installation tutorial is a great start for somebody who hasn't installed one before.
Browse to it and get "It Works!" from apache to know that you've got it right.
codebrainzrevised this article for Ubuntu 9.04 Jaunty. It can be found at http://codebrainz.ca/index.php/2009/09/27/installing-mapnik-and-friends-on-ubuntu-904/
Add some general tools
sudo apt-get install subversion unzip
Get the Data
Start to download your planet file or extract. It's very big. Plan to use another console or screen to continue while downloading. Download from one of the mirrors like this one, for a faster download.
wget http://ftp.heanet.ie/mirrors/openstreetmap.org/planet-latest.osm.bz2
Get back to the download tomorrow. We've got things to do until then.
Install the Database
A Word About username
As a simplifying assumption, this article will have you install all of the tools to work with the same user. This user is the same user for the Ubuntu login.
Set up the database to hold your copy of the OSM data. We'll use postgresql with PostGIS extensions to make the geographic
stuff easier.
First we install the dependencies.
sudo apt-get install postgresql-8.3-postgis postgresql-contrib-8.3
It should respond like this
Configuring postgresql.conf to use port 5432... * Starting PostgreSQL 8.3 database server ...done. Setting up postgresql-8.3-postgis (1.3.3-1) ...
edit /etc/postgresql/8.3/main/postgresql.conf in four places. These changes help with the large quantities of data that we are using.
shared_buffers = 128MB # 16384 for 8.1 and earlier
checkpoint_segments = 20
maintenance_work_mem = 256MB # 256000 for 8.1 and earlier
autovacuum = off
edit /etc/sysctl.conf
kernel.shmmax=268435456
Russ Nelson and balrog-k1n remind us that the above only takes effect after a reboot. Making this work immediately requires the following.
sudo sysctl kernel.shmmax=268435456
Disable the two pg_maintenance tasks which would otherwise cause vacuums to interrupt our database loads. We don't want loading the database to get interrupted.
crontab /etc/cron.d/postgresql-common
Add a #
before each line to disable it.
Reenable them if you're doing anything else with postgres, or performance will slowly degrade.
Restart postgres to enable the changes
sudo /etc/init.d/postgresql-8.3 restart
It should restart as above.
Configuring postgresql.conf to use port 5432... * Starting PostgreSQL 8.3 database server ...done. Setting up postgresql-8.3-postgis (1.3.3-1) ...
create the database
Substitute your username for username
in two places below. This should be the username that will render maps with mapnik.
sudo -u postgres -i
createuser username # answer yes for superuser
createdb -E UTF8 -O username gis
createlang plpgsql gis
exit
PostGIS
Set up PostGIS on the postresql database.
psql -d gis -f /usr/share/postgresql-8.3-postgis/lwpostgis.sql
This should respond with many lines ending with
... CREATE FUNCTION COMMIT
Substitute your username for username
in two places in the next line. This should be the username that will render maps with mapnik.
echo "ALTER TABLE geometry_columns OWNER TO username; ALTER TABLE spatial_ref_sys OWNER TO username;" | psql -d gis
# Should reply with
ALTER TABLE ALTER TABLE
Enable intarray
psql gis < /usr/share/postgresql/8.3/contrib/_int.sql
Replies with many lines ending with
CREATE FUNCTION CREATE OPERATOR CLASS
Mapnik from source
Use Mapnik for rendering pretty maps. We'll use both the mapnik.org project and some OpenStreetMap.org mapnik-tools and we'll call them both Mapnik
at various times depending on context. Sorry about that. This section deals with the Mapnik.org project.
Install some dependencies.
sudo apt-get install libltdl3-dev libpng12-dev libtiff4-dev libicu-dev
sudo apt-get install libboost-regex-dev libboost-iostreams-dev
sudo apt-get install libboost-filesystem-dev libboost-thread-dev
sudo apt-get install libboost-python1.34.1 libboost-python-dev
sudo apt-get install libfreetype6-dev libcairo2-dev libcairomm-1.0-dev
sudo apt-get install libboost-program-options-dev python-cairo-dev
sudo apt-get install libboost-serialization-dev
sudo apt-get install imagemagick
Get some more dependencies
sudo apt-get install build-essential libxml2-dev libgeos-dev libpq-dev libbz2-dev proj
proj above is called libproj-devel on OpenSuse - reported by kW on #osm
To avoid confusion, we'll put the Mapnik.org project software in ~/src/mapnik, also known as /home/username/src/mapnik
Get the source from svn
mkdir src
cd src
svn co http://svn.mapnik.org/trunk/ mapnik
cd mapnik
Configure mapnik
Note, Updated 14 Aug 2009
Are you having trouble with ImportError: No module named mapnik
after upgrading to Jaunty? Here is a fix for ImportError: No module named mapnik
python scons/scons.py
Please note that the optional boost system is not included in Boost 1.34 so the Boost System warning will persist.
Install mapnik
sudo python scons/scons.py install
Let python know about mapnik
sudo ldconfig
Install osm2pgsql
Get the latest osm2pgsql from svn.
svn co http://svn.openstreetmap.org/applications/utils/export/osm2pgsql/
Should reply with several lines and end with
Checked out revision 14116.
Your revision number will vary.
Build osm2pgsql
cd osm2pgsql
make
This should return without warnings or errors.
Set the Spatial Reference Identifier (SRID) on the new database.
psql -f 900913.sql -d gis
Should reply with
INSERT 0 1
OpenStreetMap Mapnik-tools
This is the other
Mapnik. That is to say that these are the tools that OpenStreetMap uses to use Mapnik effectively. These will be kept in ~/mapnik also known as /home/username/mapnik.
Get the latest mapnik from svn.
cd ~
svn co http://svn.openstreetmap.org/applications/rendering/mapnik/
Let's make some oceans
Stephen Wright once said, It's a small World but I wouldn't want to paint it.
I feel the same about the outlines of the continents. I wouldn't want to have to walk along each of the edges of the Earth with a GPS. OpenStreetMap uses three different sources for this coastline information. Each is freely licensed for use with OpenStreetMap. Each is stored in a different compressed file format. So we get to use a couple of different tools.
The coastline information needs to be in the mapnik directory by default. You can move it elsewhere but you'll have to fix some other settings.
cd mapnik
World boundaries
wget http://tile.openstreetmap.org/world_boundaries-spherical.tgz
tar zxvf world_boundaries-spherical.tgz
That created the world_boundaries directory that we'll use for the rest of this coastline data.
Coastlines from OpenStreetMap
wget http://hypercube.telascience.org/~kleptog/processed_p.zip
Alternate locations of processed_p, for when hypercube is slow/down
http://www.las.ic.unicamp.br/pub/gentoo/distfiles/processed_p-20090514.z...
http://beta.letuffe.org/ressources/
http://tile.openstreetmap.org/processed_p.tar.bz2
unzip processed_p.zip
mv coastlines/* world_boundaries/
rmdir coastlines
That created a new coastlines directory that we won't use. We've moved the data to the world_boundaries directory.
Shorelines for low-zoom layers
cd world_boundaries
wget http://tile.openstreetmap.org/shoreline_300.tar.bz2
tar xvjf shoreline_300.tar.bz
This one didn't create a directory at all so we unpacked it directly into world_boundaries
Testing and Your First Map
Part two of this article is expected in late March. And here it is, Make Your First Map with your OpenStreetMap tile server.
This article is one of a series that involve customizing OpenStreetMap. Please use comments below to request additional OpenStreetMap topics or to offer feedback.
References / Credits
All of the helpful folks on #osm on irc.oftc.net especially jburgess and springmeyer
All of the helpful folks on the OSM mailing list http://lists.openstreetmap.org/
http://wiki.openstreetmap.org/wiki/Mapnik
http://mapnik.org/
http://www.kelvinism.com/tech-blog/installing-mapnik-ubuntu-710/
http://www.ubuntugeek.com/ubuntu-804-hardy-heron-lamp-server-setup.html
Thanks dbaker and acant for copy editing help.
Thanks nelson, codebrainz, Ldp, balrog-k1n and kW for more updates.

Articles © 2009
Map images and data © 2009
Why do we need the Shape files ?
Hello,
I am very impressed by your articles and find them extremely helpfull etting started with Mapnik and OpenStreets. I just have a question regarding the world_boundary shape files in this article. Are these shape files used in any form to generate the Maps in your followup article " Make Your First Map with your OpenStreetMap tile server." ?
It seems that your second article utilizes only OSM files in PostGIS only. Where do these world_boundary shape files come into play ?
Please shed some light.
Thanks and keep up the good work.
Marchato.
The shape files are invoked
The shape files are invoked by ~/mapnik/osm-template.xml at various zoom levels. They provide shapes for coastlines and built up areas at zooms from 600000:1 to 250000000000:1 in the default template.
I hope this helps.
Resource missing
The server hosting http://hypercube.telascience.org/~kleptog/processed_p.zip has been down for a week now. I've been unable to find a mirror for its' resources (the osm data is mirrored in quite a few places but not the coastlines) and my google kung fu has failed me. Help would be appreciated. Thanks for the great article.
You might try these, though
You might try these, though they might not be current.
http://www.las.ic.unicamp.br/pub/gentoo/distfiles/processed_p-20090514.z...
http://beta.letuffe.org/ressources/
http://tile.openstreetmap.org/processed_p.tar.bz2
Some links are
Some links are broken:
http://tile.openstreetmap.org/world_boundaries-spherical.tgz
Appears to be a temporary
Appears to be a temporary problem. It worked when I checked it.
Shoreline data
The shoreline_300 file's missing projection (*.prj) which is a problem when rendering tiles.
Because HyperCube.telascience.org is down, you could use the following data has replacement:
http://www.nga.mil/portal/site/nga01/index.jsp?epi-content=GENERIC&itemI...
If anyone knows a better source for the shoreline, let me know!
Shoreline Data - Projection problem solved!
Hi!
I found out the problem was the projection of the world_boundaries_m, shoreline_300, world_bnd, processed_p, builtup_area, places. Those shapefile are projected in older Spheric Mercator. What happens is that the roads are over lake or sea because there's an Y-axis error. You gotta reproject those file in EPSG:900913 with ogr2ogr.
The solution is there:
http://www.nabble.com/%22scale%22-a-ShapeFile-in-Y-direction--td23473800...
Hope it helps!
Francis
Boost python
If boost python is missing (ubuntu jaunty) you must install libboost-python-dev from the repositories (not listed above).
Cheers
Missing dependency
If you get the following error compiling Mapnik
Run this to fix it -
Thanks for a really helpful tutorial!
Update
Thanks for writing this tutorial, there is such a little amount of info on the web. I have tried to install this setup at least 4 or 5 times and only once did it work, but never directly following your directions explicitly.
Mapnik simply won't install. The one time it did work, was when I first installed python-mapnik from the repos, which I'm assuming dragged in some other needed dependency. After finally getting everything working on the antique 8.04 (and everything in your second part worked), I decided to upgrade to 8.10 and then to 9.04. Everything fell apart again and nothing works. I can't post exact errors because every time I try something, another thing breaks and a new error message occurs.
I'm just wondering if you ever get the chance if you could make a new post about how to do this with a new version of ubuntu, starting with a fresh/clean install and outlining the steps and dependencies. You would be my HERO :)
Here's just a few remarks on this particular tutorial:
edit /etc/sysctl.conf
kernel.shmmax=268435456* means add that at the end of the file
-----------------
Disable the two pg_maintenance tasks which would otherwise cause vacuums to interrupt our database loads. We don't want loading the database to get interrupted.
crontab -e /etc/cron.d/postgresql-common* that command doesn't do anything on any version of ubuntu I've tried, but on 8.04 I could go into /etc/cron.d/postgresql-common and edit the file directly. on 9.04 nothing like this existed.
------------------
Substitute your username for "username" in two places below. This should be the username that will render maps with mapnik.
* this doesn't work if your username is 'user'. found that out the hard way.
------------------
Some of the links didn't work the first few times I tried this, but the last time they seemed to work.
Anyway thanks again for shedding some light on setting this stuff up, since there's not a whole lot of info on doing the complete setup with the database and everything. I have my fingers crossed about a new tutorial.
planet
I only want the map of a small twon.
Do I really need the 7GB planet files?
Can I get a Smaller option?
Look at OSM extracts for
Look at OSM extracts for smaller areas of interest. Some information on OS Extracts of smaller areas of interest.
Thanks!
Great tutorial, very helpful. Thanks a lot!
sudo apt-get install
sudo apt-get install build-essential libxml2-dev libgeos-dev libpq-dev libbz2-dev proj
This must be done BEFORE configuring mapnik...
Thanks for great tutorial!
Thank you, and thank you.
Thank you, and thank you. I've applied your patch.
Editing the crontab
Two comments on this section:
Disable the two pg_maintenance tasks which would otherwise cause vacuums to interrupt our database loads. We don't want loading the database to get interrupted.
crontab -e /etc/cron.d/postgresql-common1. This file is not present on Ubuntu 9.10 Karmic
2. the option "-e" does not allow a file argument afterwards and is used to edit the user's crontab. Correct syntax should be:
crontab /etc/cron.d/postgresql-commonThank you for the note
Thank you for the note regarding Karmic.
I've applied your patch re: -e.
PostGIS setup
The first step of the PostGIS setup
psql -d gis -f /usr/share/postgresql-8.3-postgis/lwpostgis.sqlrequires to be logged in as postgres user as well. If you would be following your tutorial the "exit" command in the section before would have logged you out and thus showing a "psql: FATAL: Ident-Authentication for user failed" error.
THANKS - and update for Karmic Ubuntu 9.1.0
Thanks for the step-by-step page. I have joined the raft load of people looking for a detailed step by step procedure to set up a tile server. I started with a bare Desktop x64 Karmic Ubuntu 9.1.0. I found that postgresql 8.3 not only doesn't install on Karmic, but it hoses the install - I couldn't uninstall it, so I reloaded Ubuntu.
Here's what I did to get a working copy - my first steps are clear and I believe correct, but at the end, you still need to muddle about and pick the best of several pages to proceed.
Step 1:
Install Mapnik from Ubuntu package Ref: http://trac.mapnik.org/wiki/UbuntuInstallation
sudo apt-get install libmapnik0.6 mapnik-utils python-mapnik
Step 2:
sudo apt-get install libboost1.38-dev libboost-filesystem1.38-dev \
libboost-iostreams1.38-dev libboost-program-options1.38-dev \
libboost-python1.38-dev libboost-regex1.38-dev \
libboost-thread1.38-dev \
libxml2 libxml2-dev libfreetype6 libfreetype6-dev libjpeg62 \
libjpeg62-dev libltdl7 libltdl7-dev libpng12-0 libpng12-dev libgeotiff-dev libtiff4 \
libtiff4-dev libcairo libcairo-dev python-cairo python-cairo-dev libcairomm-1.0-1 \
libcairomm-1.0-dev ttf-dejavu ttf-dejavu-core ttf-dejavu-extra libgdal1-dev python-gdal \
postgresql-8.4 postgresql-server-dev-8.4 postgresql-contrib-8.4 libsqlite-dev subversion \
build-essential python-nose
sudo apt-get install python-elementtree
sudo apt-get install libgeos-dev libpq-dev libbz2-dev proj
# note, the postgis package is not available for postgres-8.4, so install it from source...
wget http://postgis.refractions.net/download/postgis-1.4.0.tar.gz
tar xzf postgis-1.4.0.tar.gz
cd postgis-1.4.0
./configure && make && sudo make install
Execute "Setting Up the Database" from http://codebrainz.ca/index.php/2009/09/27/installing-mapnik-and-friends-...
---> Continue from "Install osm2pgsql" step above
(psql -d gis -f /usr/share/postgresql-8.3-postgis/lwpostgis.sql
does not exist, instead use
psql -d gis -f /usr/share/postgresql/8.4/contrib/postgis.sql
[The instructions in the postgres README are wrong - the arguments are backward]
before 'exit' part of create database)
Resume at this page: "Install osm2pgsql"
No write permission
I am relatively new to Ubuntu. The PostgreSQL installation fails because it has no write permission.
ldconfig deferred processing now taking placeuser@ubuntu:~$ edit /etc/postgresql/8.3/main/postgresql.conf
Warning: unknown mime-type for "/etc/postgresql/8.3/main/postgresql.conf" -- using "application/octet-stream"
Error: no write permission for file "/etc/postgresql/8.3/main/postgresql.conf"
Moreover I am not able to edit the config files. The file properties say that I am not the owner of the files.
During the installation i did get a message telling me that a newer version of PostgreSQL (8.4) is available.
location of postgresql-common
i am currently installed it on ubuntu 9.04 jaunty. i found the location of postgresql-common should be in /etc/logrotate.d/postgresql-common. /etc/cron.d/postgresql-common does not exits
mapnik src.
current version of mapnik requires boost 1.41 or greater. The old version has been tagged as 0.7.0
Post new comment