OpenStreetMap (OSM) Install on Windows – Part I – The Database

Recently I had a need to use OSM within a project I was developing. After loking at the documentation and quite a few web sites, I concluded two things:

  1. The documentationn that exists is targetted at the Linux community
  2. There did not seem to be a definitive way to get the system running on Windows

After some effort I managed to get the whole system running nicely under Windows. To save anyone else the pain, this is how to do it.

In this guide, OSM data is being held in a PostgreSQL database, where it is ultimately used to generate mapping tiles.

  1. Download and install PostgreSQL from here. At the time of writing, release 9.2.1 was being used.
  2. At the end of the install, you will be asked to run ‘Stack Builder’. Do this and select ‘Spatial Extensions’ and ‘JDBC Driver’. The installer will download these and update your install.
  3. Add the PostgreSQL \bin directory to your PATH. You will need this to run various command line tools
  4. From the command line, log in to the database using the command: psql -U postgres
  5. Now create a database using the command: CREATE DATABASE gis WITH ENCODING=’UTF8′ TEMPLATE=template_postgis_20 CONNECTION LIMIT=-1;
  6. The database has now been created so quit using the command: \q

Now the database exists, various things need to be done to the standard configuration to make is usable as our OSM data source.The first is to add support for legacy operations. Use the following command (The path many vary depending on where you installed the software. This presumes it went into the root of C:)

psql -U postgres -d gis -f “C:\PostgreSQL\9.2\share\contrib\postgis-2.0\legacy.sql”

The next step involves getting a patch file for PostGIS GiST indexes. If you really want to know what that means, have a look at the Wikipedia arcticle.

Patch File

Run this into PostgreSQL using the following command:

psql -U postgres -d gis -f “legacy-postgis-gist.sql”

For more information around this step, have a look at this article on github.

Now that the database is configured, we need some way of getting the map data into it. This is done via the use of a tool called osm2pgsql.

  1. Download the osm2pgsql tool from here
  2. Unpack the file to somewhere convenient
  3. Add the directory where you unpacked the file to your PATH
  4. Run the command osm2pgsql -h to verify the install [Should get a help screen]
  5. Replace the file named with a more up to date version from here

We now have the capability to process map data and a database to store it in. All we need is some map data.

As a demonstration, I’m going to set up the UK. The best site for data I have found is CloudMade. The data here has two advantages in that it covers the whole world and is regularly updated.

Download the following files. This is around 750MB in total

Now feed the data into the database. These command s presume you unpacked osm2pgsql into C:. If not, edit appropriately. Depending on the performance of your system, this may take some time to run.

osm2pgsql -W -m -U postgres -d gis -s -S C:\osm2pgsql\osm2pgsql\ united_kingdom.osm.bz2
osm2pgsql -W -m -U postgres -d gis -s -S C:\osm2pgsql\osm2pgsql\ united_kingdom.highway.osm.bz2
osm2pgsql -W -m -U postgres -d gis -s -S C:\osm2pgsql\osm2pgsql\ united_kingdom.administrative.osm.bz2
osm2pgsql -W -m -U postgres -d gis -s -S C:\osm2pgsql\osm2pgsql\ united_kingdom.coastline.osm.bz2

All done! – You now have a database of data and need some way to use it. All explained in part II.