Some time ago I had the idea of importing some of the data of OpenStreetMap (OSM) into a database — as they’ve become much better quality over the last few years — and find special objects systematically. This information could populate a databasetable and could be used by our sales department.
As I use to work with Linux (Ubuntu), my description is related to such an environment. On my notebook I have a basic Ubuntu installed (with recent version Lucid, and a Gnome desktop). I added PostgreSQL 8.4, PostGIS 1.4, OSM2PGSQL and QGIS 1.4 (from the standard repository of Ubuntu). To work in PostgreSQL with a GUI, I installed PGAdminIII in addition to this. After installing the software you just have to tweak some things and soon you have a useable GIS with OSM-data. But let’s do it step by step …
Preparing the Database
After installing PostgreSQL and PostGIS, you have already activated the spatial extension for the databaseserver. In my case, I could use psql (textbased client for PostgreSQL) in a terminal immediately to connect to PostgreSQL, but not via PGAdminIII. In PGAdminIII they expect a password, which you have to set in psql at first. So you go and open a terminal window in Linux (e.g. bash) and create a new databaseuser using createuser gisadmin (as far as you only test this case on a local PC, you can answer the question about the superuser with YES). After that you create a fresh database with the command createdb mygis. To avoid problems with PGAdminIII you should set the password for the user gisadmin right now. To do so, you connect to PostgreSQL by psql -d meingis and use the command \password gisadmin. Than you have to enter the new password twice (you’ll be asked to). Now you can disconnet from the server by \q.
Back in the terminal, you have to activate the procedural language in PostgreSQL by createlang plpgsql mygis. Now you have to find the files postgis.sql and spatial_ref_sys.sql in the filesystem. In my case they were found in /usr/share/postgresql/8.4/contrib. To really find them, it’s important not to have the packet postgis-common installed, but the one that is bundled by Ubuntu! The necessary tables for really using PostGIS in the database mygis will be created by pgsql -d mygis -f /usr/share/postgresql/8.4/contrib/postgis.sql and pgsql -d mygis -f /usr/share/postgresql/8.4/contrib/spatial_ref_sys.sql.
Detailed information about installing of PostGIS can be found here…
So, where’s the OSM-data …?
Well, at this point we have a fresh and clean GIS database, but without any data. Unfortunately you cannot use the webinterface of OpenStreetMap (OSM) (www.openstreettmap.org) to extract the data at once. You have to use the so called XAPI of OSM. In the beginning, I though about the XAPI as something very complicated just to get some data. But fortunately, this was completely wrong. The only energy you have to invest is to define the area you want to extract and the objects you want to see in your database. Let me give you an example of an area in Oberhausen (Germany) where we first simply extract all data unfiltered. For a more complexe query you should have a look at this description.
I assume, that you don’t have the geographic coordinates (Lon/Lat) of the area you want to extract at hand. You need the lower left corner and and the upper right corner. To get them, you can open the webinterface of OpenStreetMap and navigate to the area you’d like to extract. Place the centre of the map to the lower left corner of the area and point the mouse to the right left corner of the map, wher you find the link ”Permalink”. At the bottomline of your browser you can see the link (permalink) with the value for Lon and Lat. Write down the complete number. Repeat this with the right upper corner.
Next step is to really get our data. You can either put this line into your browser and safe the returned data into data.osm http://xapi.openstreetmap.org/api/0.6/map?bbox=LON-LU,LAT-LU,LON-RO,LAT-RO. Or you can use the following command in your terminal window wget http://xapi.openstreetmap.org/api/0.6/map?bbox=LON-LU,LAT-LU,LON-RO,LAT-RO -O data.osm. You certainly have to change the values after bbox= with the ones you have identyfied in your case. The resulting file can range from a few MB to a half GB, according to the area you’ve selected. If your file is empty — 0 kb — than you have to check the coordinates. This problem appears regulary …
Preparing the OSM-data
At this stage you still cannot use the OSM-data – but we will change this right now. We transfer the OSM-data (the file data.osm) into PostGIS using OSM2PGSQL. If you use the command for the first time you should use it like this: osm2pgsql -c -d mygis -U gisadmin -W -H localhost data.osm. The parameter -c stands for creating the new tables (create) or to clean the old ones before adding the new data. This is followed by the name of the database, the name of the user, an option for a passwordrequest, the hostname for the databaseserver and the name of the OSM-datafile, which we want to transfer.
If this executes without an error — which usually are errors in the part in front of the filename — you can find your OSM-data in the mygis database. Besides OSM2PGSQL you could use OSMOSIS. I won’t describe this here, but you can find information about this via http://wiki.openstreetmap.org.
Connecting PostGIS with QGIS
One or the other reader may find it useful, to analyse the OSM-data directly in the database with SQL. But the real strength compared to textbased datacollections is the visualization of the data in a map. As PostGIS finally is just a datacollection with a huge package of geospatial functions you cannot use it directly to display the data in a map. Certainly there is a great number of GIS that you could connect to PostGIS, but I usually care about QGIS. And ou can easily connect PostGIS with PostGIS.
Within the buttons at the top of QGIS you can find a blue barrel-like symbol with a ”plus” to add a PostGIS-layer. After clicking the symbol, a dialogue pops up. In this dialogue you have to add a database-connection if you use it for the first time. Click on the button ”New” and enter the required information (mygis, gisadmin, hostname aso.). After entering the information you should go and click on ”test connection”. If this test is successful you can leave the dialogue with the ”OK”-button. If this test fails, you have to check the entered data. So, if you successfully created the new connection, you have to activate it by clicking on ”Connect”. In the lower area of the dialogue you will see the available tables with their names. There should be at least four tables with names beginning with planet_osm_… You can find lines (…_line), points (…_point), areas (…polygon) and roads (…roads). In my case, most of the roads were inserted into the table containing the lines and not among the roads. That’s just a note I wanted to add. After this you should click on all the tables in the list, and finally click on ”Add” to add them to QGIS.
Nice lines …
… don’t make an appealing map. If you haven’t used QGIS before you should maybe go through a tutorial to learn about the basic functions and configuration of the objects. As a quick howto, I can tell you this: click on a layername with the right mouse-button (e.g. planet_osm_line) and the properties of the layer will open. Besides the appearence of the object you can change the caption. If you activate the checkbox ”Show caption” you should select the field, which contains the info you want to display. I guess it is named ”name”, if you haven’t put your info into an extraordinary field. You should play around with textheight and colour. If you want a special layer to be placed over another layer you can easily drag and drop it in the list.
If you successfully went through all this, there will surely come some questions about special usecases in your mind. Let me tell you this: the OSM-data is much better quality than you think. But please don’t fool me, if there’s no data in the area you wanted to analyse: why haven’t YOU already collected the data? OpenStreetMap is open to everyone, who can enrich OSM with his data. You can also simply place your question via email in my contact form (contactform). Maybe I can add a new entry in this blog to describe and solve your problem or I can assist you personally.