Many a times I have heard my GIS friends say that OpenStreetMaps are ‘just’ street maps and lack quality and ability to be used in efficient GIS applications. But this is not always true. I would like to make a comment that, GIS applications they mention still uses any kind of map data which are essentially gathered and processed through GPS or Satellite Images. OpenStreetMap is the culmination all of those sources. People around the planet are contributing to OSM through satellite images, raw GPS data and geo-referenced cadastral maps. OpenStreetMap is thus a wonderful resource that can be used into GIS applications and more over it is free!
Now, let us see how this could be done at the first level. Probably most of the leading GIS softwares uses PostgreSQL database with PostGIS extension to process geographic information. This note would help you to download the OSM data of the region of your interest and create a PostgreSQL database with duly applied postgis extensions. The database would fit into any GIS requirement as you expect.
A word about OSM layering of data. In OSM there are no layers in the traditional GIS sense. All features are in one big coherent
database. But still, when you are creating a database out of it, you are actually layering the entire data. More information on layering is available here.
Lets us begin with collecting the OSM data. There are many ways for you to download the OSM data sets. I prefer those from CloudMade. There are other sources like GeoFabrik. And of course the direct data sets from OpenStreetMap.org. The data might be in the default OSM xml style sheet which is called the .osm file.
Now that we have the data, we’ll use some tools to set the database up. Please ensure that you have PostgreSQL, PostGIS including the contrib packages. Osm2pgsql is the important tool which we would be using to straight away convert the OSM data set into a PostgreSQL database. You can install osm2pgsql from the source or packages listed here.
Next, we need to setup a database to load the OSM data. This could be done in the following steps.
- $ sudo -u postgres createuser <username>
- $ sudo -u postgres createdb -E UTF8 -O <username> <dbname>
- $ sudo -u postgres createlang plpgsql <dbname>
Now add the postgis extensions. Note the location of the lwpostgis.sql file may vary.
- $ sudo -u postgres psql -d <dbname> -f /usr/share/postgresql-8.3-postgis/lwpostgis.sql
Next give access to the user to update the postgis metatables.
- $ sudo -u postgres psql -d <dbname> -c “ALTER TABLE geometry_columns OWNER TO <username>”
- $ sudo -u postgres psql -d <dbname> -c “ALTER TABLE spatial_ref_sys OWNER TO <username>”
Apply 900913 diff which is not normally included with postgis, you can get it from the source of osm2pgsql. To add it you should run:
- $ sudo -u postgres psql -d <dbname> -f 900913.sql
Run, osm2pgsql, this should load the OSM data into the database we just created.
- $ osm2pgsql <osm_file> -d <database_name> -U <user_name>
Now you can import the database into any GIS software.
For the completion of this document, I would illustrate usage of the database with qGIS. Start qGIS with a new project.
- Click on the “Add a PostGIS layer” button on the toolbar.
- In the “Add PostGIS Tables” pop up, click “new”.
- Fill in the fields accordingly and click “OK”.
- Connect to the database and select the required tables to import.
- Click Add.
Now we have the OSM data in the GIS software, just like any other geographic data source.
Hope this quick guide helps! Please let me know if you have any difficulties.