Archive

Archive for the ‘OpenStreetMap’ Category

Import OpenStreetMap data into GeoServer/PostGIS

December 2, 2009 24 comments

It is possible to download map data from the OpenStreetMap (OSM) dataset in a number of ways. The full dataset is available from the OSM website download area. This dataset (Planet.osm) is a large file, currently about 4GB when compressed and about 100GB when uncompressed. It is also possible to select smaller areas to download. Data normally comes in the form of XML formatted .osm files (an OpenStreetMap markup language).

I have downloaded today a part of the city of Porto using the OSM API,  converted it into shape files, added a new store to geoserver (using those shape files) and finaly visualized the new layers in GoogleEarth and uDig.

  • Obtaining the contents from OpenStreetMap using the OSM API

First you will have to define your region specified by a bounding box which consists of a minimum and maximum latitude and longitude. An easy way to accomplish that task is to use Google Earth. Hovering the mouse in the desired corner of the rectangle to export, look at the coordinates and take notes (you only need two coordinates – opposite corners of the rectangle).

Then use those values to create the HTTP request using the folowing URL:

http://api.openstreetmap.org/api/0.6/map?bbox=left,bottom,right,top

for example:

http://api.openstreetmap.org/api/0.6/map?bbox=11.54,48.14,11.543,48.145

Where left is the maximum longitude, right is the value for the minimum longitude, bottom the minimum latitude and top the maximum latitude.

However there is a constraint, the API is limited to bounding boxes of about 0.5 degree by 0.5 degree. For larger areas you could try Osmxapi like so (this will also return a .osm file):

http://www.informationfreeway.org/api/0.6/map?bbox=11.54,48.14,11.543,48.145

The .osm file is not currently not supported by geoserver, so we got 2 ways to get around this. Convert it to shape files, or insert directly in PostGIS database. However I discovered a Web page that offers subsets of the original OSM dataset. They do this by dividing the datasets by country offering the information both in .osm or .shp files, very handy.

  • Convert .osm files to shape

Not easy to find, cause I did it in Windows OS, but there is an application out there called osm2shp, which comes with a GUI that does this conversion in a very easy way. If having trouble finding it, just ask me. If you are working in linux, you’re be glad to know that Ubuntu (and Debian-based OSes) repositories have this package.

  • Creating a datastore in geoserver uing the new shape files

The output of the previous convertion can be copied to the data folder in the geoserver path. Just create a folder (for instance osmdata) containing the shape files in:

path\to\geoserver\data_dir\data\osmdata

then create a new datastore using the GeoServer Web interface. (check this tutorial)

Just pay attention so that the projection of the source data be configured as 900913, the projection used in Google Maps, and the default for OpenStreetMap.

  • Direct input into PostGIS database

There is a converter that allows you to load the OpenStreetMap data into PostGIS called osm2pgsql.  This program is a package available on Debian-based distributions (such as Ubuntu), and is also available as a binary on Windows.

Running the command:

osm2pgsql -E 900913 -d myDataBase TheOsmFile.osm

This will process the XML information and load the data into a PostGIS database called “myDataBase”. The -E defines the projection of the source data, which in this case is 900913, the projection used in Google Maps, and the default for OpenStreetMap.

If successful, the database will contain the following tables:

planet_osm_line
planet_osm_point
planet_osm_polygon
planet_osm_roads

There are two different tables that contain line data, planet_osm_line and planet_osm_roads. The former includes railroads, subways, and other linear information.  The latter is made up exclusively of roads.  The planet_osm_point table has a range of data: subway stations, shopping centers, universities, and even brothels. Lastly the planet_osm_polygon table has, but is not limited to, parks, bodies of water, and even buildings in certain urban areas.

After these steps map info can be easily viewed in GIS apps like uDig/ArcView. The GeoServer Web interface also has directs links for KML for previewing the layer in GoogleEarth if installed.

Also today, I had a small rendez-vous with openLayers and I was able to visualize the new OSM data in a Web page. It is quite easy to create and customize, but have a peek in the sources that I used as a guide. I will have to explore this some day later.

Information sources:

http://blog.geoserver.org/2009/01/30/geoserver-and-openstreetmap/

http://wiki.openstreetmap.org/wiki/Getting_Data