How to build your own OSM Server Part 1 - PostGIS and Mapnik

Introduction

This is a simple copy and paste HOWTO document on setting up your own map server. This document is part of a series of 4-5 articles that cover buildings a map service. I assume that most of the readers are familiar with the the basics of using Ubuntu GNU/Linux and tools around it. You will also need some knowledge about the Open Street Maps tools.

One thing I will not discuss much about is PostGIS and PostgreSQL since my knowledge is limited and I am still not experience enough to talk in detail about it. I would suggest reading the documents that are available on their website.

Requirements

Hardware

This is the bare minimum configuration that is required to get up and running.

  • RAM: 8GB
  • Hard Disk: 500GB (at least 7200 RPM)
  • CPU: 2.5 GHz +

Software

  • OS: Ubuntu 10.04 (Server)

You will also need a really fast Internet connection, unless you have a local mirror of Ubuntu and the Planet dumps.

Setting up the base system

Add a production user in this case we will use “prodeng”

# sudo adduser prodeng
# sudo adduser prodeng admin

Ensure the system is up to date

# sudo apt-get update
# sudo apt-get upgrade

Install all the basics development tools

# sudo apt-get install -y build-essential subversion autoconf screen unzip python-setuptools

Create the maps environment

# cd /mnt/
# sudo mkdir src bin planet
# sudo chown prodeng:prodeng src bin planet

Configure Operating System

# sudo sh -c "echo 'kernel.shmmax=268435456' > /etc/sysctl.d/60-shmmax.conf
# sudo service procps start

Getting the planet-latest dump

Download the latest planet database

Notes:

  • Try to use a Planet mirror if you can.
  • The planet dump is over 12GB+ so you may want to use screen here
# cd /mnt/planet
# wget http://planet.openstreetmap.org/planet-latest.osm.bz2

osm2pgsql

Install osm2pgsql

# cd /mnt/bin
# svn co http://svn.openstreetmap.org/applications/utils/export/osm2pgsql/
# cd osm2pgsql
# ./autogen.sh
# ./configure
# make

PostGIS

Install PostGIS 8.4

# sudo apt-get install -y postgresql-8.4-postgis postgresql-contrib-8.4
# sudo apt-get install -y postgresql-server-dev-8.4
# sudo apt-get install -y libgeos-dev libpq-dev libbz2-dev proj libxml2-dev

Configure PostGIS 8.4

Edit and make the following changes to the configuration file: /etc/postgresql/8.4/main/postgresql.conf

shared_buffers = 128MB
checkpoint_segments = 50
maintenance_work_mem = 256MB
autovacuum = off

Restart PostGIS 8.4

# sudo service postgresql-8.4 restart

Setup PostGIS for OSM

# sudo -u postgres -i
# createuser prodeng
# createdb -E UTF8 -O prodeng gis
# createlang plpgsql gis
# exit
# psql -f /usr/share/postgresql/8.4/contrib/postgis.sql -d gis
# echo "ALTER TABLE geometry_columns OWNER TO prodeng; ALTER TABLE spatial_ref_sys OWNER TO prodeng;" | psql -d gis
# psql -f /usr/share/postgresql/8.4/contrib/_int.sql -d gis
# psql -f /mnt/bin/osm2pgsql/900913.sql -d gis

Installing Mapnik

Install Mapnik Dependencies

# sudo apt-get install -y libltdl3-dev libpng12-dev libtiff4-dev libicu-dev
# sudo apt-get install -y libboost-python1.40-dev python-cairo-dev python-nose
# sudo apt-get install -y libboost1.40-dev libboost-filesystem1.40-dev
# sudo apt-get install -y libboost-iostreams1.40-dev libboost-regex1.40-dev libboost-thread1.40-dev
# sudo apt-get install -y libboost-program-options1.40-dev libboost-python1.40-dev
# sudo apt-get install -y libfreetype6-dev libcairo2-dev libcairomm-1.0-dev
# sudo apt-get install -y libgeotiff-dev libtiff4 libtiff4-dev libtiffxx0c2
# sudo apt-get install -y libsigc++-dev libsigc++0c2 libsigx-2.0-2 libsigx-2.0-dev
# sudo apt-get install -y libgdal1-dev python-gdal
# sudo apt-get install -y imagemagick ttf-dejavu

Install Mapnik

# cd /mnt/src
# svn co http://svn.mapnik.org/tags/release-0.7.1/ mapnik
# cd mapnik
# python scons/scons.py configure INPUT_PLUGINS=all OPTIMIZATION=3 SYSTEM_FONTS=/usr/share/fonts/truetype/
# python scons/scons.py
# sudo python scons/scons.py install
# sudo ldconfig

Confirm Mapnik Install

# python
>>> import mapnik
>>>

Installing Mapnik-tools

Install Mapnik Tools

# cd /mnt/bin/
# svn co http://svn.openstreetmap.org/applications/rendering/mapnik

Install World Boundry

# cd /mnt/bin/mapnik
# mkdir world_boundaries
# wget http://tile.openstreetmap.org/world_boundaries-spherical.tgz
# tar xvzf world_boundaries-spherical.tgz
# wget http://tile.openstreetmap.org/processed_p.tar.bz2
# tar xvjf processed_p.tar.bz2 -C world_boundaries
# wget http://tile.openstreetmap.org/shoreline_300.tar.bz2
# tar xjf shoreline_300.tar.bz2 -C world_boundaries
# wget http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/10m-populated-places.zip
# unzip 10m-populated-places.zip -d world_boundaries
# wget http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/110m-admin-0-boundary-lines.zip
# unzip 110m-admin-0-boundary-lines.zip -d world_boundaries

Osmosis

Install Osmosis Dependencies

# sudo apt-get install -y openjdk-6-jre-headless

Install Osmosis

# cd /mnt/src
# wget http://dev.openstreetmap.org/~bretth/osmosis-build/osmosis-latest.tgz
# tar zxvf /mnt/src/osmosis-latest.tgz -C /mnt/bin/

Importing the planet dump into PostGIS

Load the planet info into the database

Notes:

  • The import can take any where between 4-6 days.
  • You may want to use screen here
# cd /mnt/bin/osm2pgsql
# ./osm2pgsql -S default.style --slim -d gis -C 4096 /mnt/planet/planet-latest.osm.bz2

Generating your first map

# cd /mnt/bin/mapnik
# ./generate_xml.py --dbname gis --user username --accept-none
# ./generate_image.py

This will generate the map of England and store it in a file called image.png

7 comments

Christian's picture

Hi, thanks for this great

Submitted by Christian (not verified) on Wed, 08/31/2011 - 21:21.

Hi,

thanks for this great tutorial.
I just tested it with a newer version of osm2pgsql. With this new version, it will stop working when you install the intarray module for postgresql. So ommit this step:
psql -f /usr/share/postgresql/8.4/contrib/_int.sql -d gis

When you want to remove the already installed intarray module, just use:
psql -f /usr/share/postgresql/8.4/contrib/uninstall__int.sql -d gis

Marko's picture

Can i get reverse geocode

Submitted by Marko (not verified) on Fri, 03/25/2011 - 18:03.

Can i get reverse geocode from this osm server?

Marko's picture

Great tutorial, but how to

Submitted by Marko (not verified) on Thu, 11/18/2010 - 20:23.

Great tutorial, but how to make other maps? Not just England...

wnoronha's picture

You can modify the bounding

Submitted by wnoronha on Mon, 11/29/2010 - 17:52.

You can modify the bounding box in generate_image.py to generate a different image.

I am working on a follow up how to on creating your own OSM service.

Benjamin's picture

Nice howto, but how to go on?

Submitted by Benjamin (not verified) on Thu, 11/18/2010 - 14:18.

Nice howto, but how to go on? How to setup some kind of osm.org like webservice? Could you please continue or give some hints/links? That would be nice. Thanks

Skyvin's picture

This is really great

Submitted by Skyvin (not verified) on Sun, 10/31/2010 - 21:50.

This is really great tutorial. Great job! bravo! Thank you so much for sharing your experience.

Julio's picture

Hi, I'm very interested on

Submitted by Julio (not verified) on Mon, 09/13/2010 - 12:23.

Hi, I'm very interested on build my own OSM server, could you please continue with this series of posts?. I will apreciate very much.

Thank you.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <pre>, <apache>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Drupal theme by Kiwi Themes.