Home > GeoServer, OpenStreetMap, PostGIS, shp, uDig, Windows > Import OpenStreetMap data into GeoServer/PostGIS

Import OpenStreetMap data into GeoServer/PostGIS

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

  1. Felipe
    January 11, 2010 at 10:56 pm

    Hi: Do you mind explaining how this works, I have been trying to do this for a long time and have not been able to do it yet. I am using windows and I have a postgresql and postgis databases set up on my pc but I can’t import osm data because i don’t know how to install the osm2pgsql tool. I am not a programmer so I would really appreciate it if you can explain this in plain english or some step by step for dummies(like me). I am running qgis 1.5 and would like to render my maps with this program.
    “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.”

    I can’t find the way to run the code below, is it typed on the command line or the osgeo4w shell?
    Running the command:
    osm2pgsql -E 900913 -d myDataBase TheOsmFile.osm

    • January 12, 2010 at 9:59 pm

      Hi Felipe.
      Actually I haven’t try that until now. I only posted that information for self future reference. I prefer to keep my data in the shape file format. That way it becomes easier to convert to any other formats including SQL.
      Finding the application I mentioned was not that easy, but here is the compiled version for Windows OSs.
      Try to follow these steps and in case of any trouble please tell me the item where you got stuck and give me a description.

      1. Create a database in pgadmin as stated in this previous post
      2. Assuming that you have downloaded the data from Open Street Map, you possess a XML file with .osm extention. Decompress de zip file containing the osm2pgsql binary and move the .osm file to the same folder of the previously uncompressed files.
      3. Open the Windows console and navigate to the folder containing the files.
      4. On the console write: osm2pgsql -U bduser -d dbname osmfile.osm where bduser is the database username, dbname the name the database you created using pgadmin, and of course osmfile.osm will be your .osm file.

      Here is the output that the execution gives me:

      Has you see it created 4 new tables on the database. Adding the layers to a qgis project should be easy (never tried it).

      I hope this helps,
      Regards

  2. Felipe Carrillo
    January 16, 2010 at 5:36 am

    Hi again, thanks for the reply, is this the regular command line, start -> programs -> accessories -> command prompt or is pgAdmin III? my osm2pgsql unzipped file is on my desktop and my california.osm file is right next to it but for some reason is not recognizing it. I created a database named “California” and I opened the windows command line and changed my directory like this: cd c:\Documents and Settings\Owner\Desktop and then entered but when I execute the command by pressing Enter I get the following error message:
    “osm2pgsql is not recognized as an internal or external command”
    Thanks for your help

    • January 16, 2010 at 12:55 pm

      Hi Felipe. That is a common error message indicating that you are not in the right folder. If you have run the command directly in the desktop folder and you got that error, I would say that while uncompressing the file, a new directory (containing the executable) was created over the desktop folder. Try this:
      1) download this batch script.
      2) open it with your notepad and replace YourPgDatabaseUserName, YourDatabaseName, YourOSMFileName with your settings.
      3) move the script to the same folder as the other files you have (all in the same folder)
      4) Double click on the osm2pgsql_script file 😛

      I thought you had more knowledge with the command line. I should have been more specific in the first reply…
      Hope you can do it now, and I’m glad to help.
      Regards

  3. Felipe Carrillo
    January 16, 2010 at 6:01 pm

    My PostgreSQL, postgis and pgadmin III are on c:\program files. Do I have to move them to my desktop or doesn’t matter. I think I am getting closser but after running the batch file with my database name I got the same error message. I changed the batch file to:
    osm2pgsql -U postgres -d california map.osm
    Where postgres is my default username or owner, california is the name of the database i created and map.osm is the name of the file that I downloaded from http://www.openstreetmap.org…Thanks

  4. Felipe Carrillo
    January 16, 2010 at 6:19 pm

    Nevermind, I think I got it,,,I’ll let you know how it went,,,,Thanks for your help

    • January 17, 2010 at 11:05 am

      Ok, but if you got tired to try it, send the .osm file to my mail and I will send you the SQL script to use directly on PgAdmin.

      My email is helderbnunes[at]gmail.com

      I really don’t get it… the same error message with the script on the same folder as the files :S
      Oh well… send it to me and I will do it for you.

  5. Felipe Carrillo
    January 17, 2010 at 6:55 pm

    Helder:
    I successfully ran the osm2pgsql file with created the files below:
    planet_osm_line
    planet_osm_point
    planet_osm_polygon
    planet_osm_roads

    Where are these saved on my computer?
    I need to convert these to shp in order to visualize them with a gis prorgram,correct? I am using quantum gis

    Do I need to use osm2shp tool to do this?

    • January 17, 2010 at 7:37 pm

      Ok, nice..
      “Where are these saved on my computer?”: That will be in your database. As you said before, you ran the command with california as being your database name. planet_osm_line, planet_osm_point, planet_osm_polygon and planet_osm_roads are the table names created and populated with the data in the osm file. You will see those opening pgAdmin and the database named california.
      “I need to convert these to shp in order to visualize them with a gis prorgram,correct?”: I never used qgis, however most of the spatial visualizers can import data directly from the database. I use the opensource software udig. It’s quite simple to add the new database layer. But in a quick google search I was able to find that qgis can connect to postgres database as well. So, there’s no need to convert the data to shape files. But if you have that need I can explore that… It may become useful to me too.

  6. Felipe Carrillo
    January 17, 2010 at 7:50 pm

    I have a plugin in qgis called “SPIT” but is asking for a shapefile but planet_osm_roads doesn’t have that extension. I also have Udig. It got downloaded with qgis but I have never used it before. I’ll give it a try. qgis is a nice gis free software if you want to try it.

  7. Shamitha
    April 27, 2010 at 6:10 am

    Hello,
    We followed the steps and downloaded osm file by name india.osm.administrative from cloudmade website. We stored this osm file in osm2pgsql folder in a common folder called as india. We tried to run the command in console but it gives an error:

    Connection to database failed: Server closed the connection unexpectedly
    This probably means the server terminated abnormally before or while processing the file.

    But the server is running and we followed the steps in ur blog to create the database. Database which we created is ‘Project’ and we used this command:
    osm2pgsql -U postgres -d Project india.osm.administrative

    Really appreciate your help!
    Thanks in advance!

    • April 27, 2010 at 9:10 am

      Hello Shamitha,
      I tried to create that error but with no success. Is that the only message the program gives you? Try to specify postres password, hostname and port. Something like:

      osm2pgsql -U postgres -W POSTGRES_PASS_HERE -P PORT_NUMBER_HERE -H HOST_NAME_OR_IP_HERE -d Project india.osm.administrative

      If this does not work try to check the database log to see if contains more useful information. In pgAdmin go to Tools > Database state (or similar because mine is in portuguese) to a quick message check on the log.

      If still no success I would try to visualize that OSM data in QGIS (I think it has an extension giving that possibility). My intension would be then to check the data for inconsistencies.

      I wish you good luck… 😉

      • Shamitha
        April 27, 2010 at 5:35 pm

        solved the problem! Thanks!! 🙂

  8. Anjana
    April 27, 2010 at 7:45 pm

    Hello sir,
    We followed all the steps specified in this blog. We converted the osm files using osm2pgsql,stored them in the database and even got results as you have described. But sir,we have to use geoserver to display these files as a part of our college project. Is it possible to directly display these files from geoserver using the option in data stores-Postgis,and later creating and publishing the layers accordingly?or do we have to convert it to shapefiles and try displaying this from geoserver. I tried to implement the former method but i was not able to display the map from the openlayers format present in geoserver-layer preview.

    So sir,can you please elaborate on the creation of shapefiles from the same planet.osm data and all the subsequent steps thats leads to the display of the map on the geosever-layer preview. And it would be of great help if youcould please notify me as to where i am going wrong and why the map is not getting displayed.

    thanks in advance:)

  9. Shamitha
    May 1, 2010 at 1:40 pm

    Hi,
    I need to convert osm to shapefiles. I downloaded the software for windows platform and tried converting small osm files. It worked fine. But wen I tried to convert the large osm file its not getting converted. The window is not respond. I took the data from planet.osm. The file’s name is changesets. I need to access the shapefiles in order to display using Geoserver. Can you please tell me what to do to get shapefiles..?
    Thanks in advance!!

    • May 1, 2010 at 5:24 pm

      Hello again,
      I know 3 possibilities. The first one is using qGIS with OSM plugin. Then you can load the osm files directly as a layer. I guess has support to export the layer as a different datatype, like shapefiles (not quite sure of this).
      The second approach could be converting directly using some kind of software that is able to convert directly from .osm to .shp like this one. Probably if you google for it, you’ll find some more of this apps.
      The last is to use the former osm2pgsql and insert the data in a database, and then pulling it again as shapefiles (an ugly option I know). The command to do this will be pgsql2shp. Never used this. Type “pgsql2shp -h” in the command line to see some help.

      Good luck again 😉

  10. Anjana
    May 11, 2010 at 3:01 pm

    hello,
    I need to know if it is possible to host geoserver on the internet after configuring it. Its that we have a project where we need to display maps from the mobile phone and we used Geoserver to configure the maps and their storage. Now we need to publish this Geoserver on the internet to access it in the mobile phone and display the maps which we stored. So if you could provide any help regarding hosting the geoserver, which is present on my localhost, to the internet…it would help us a lot.
    thank u!!

    • May 11, 2010 at 9:20 pm

      Wow seems an interesting project. I noticed a question you did before that I didn’t answered. Sorry about that, I must have got stuck on work. I did that before, geoserver interface is pretty easy to follow. I hope you accomplished what you were looking for.
      About the hosting, I don’t know if I understood the question. But once you have your server running on your local machine, you should be able to expose it to the internet accessing it using your IP address. You can also use a dynamic DNS like no-ip or dyndns (but there are others). This is my usual option. Probably if you are inside a private network you’ll have to configure your router to redirect incoming requests to your machine running the server. Make sure that the server machine doesn’t go to sleep/hibernate/shutdown, configuring properly the power options.
      But there are other options (paid ones) like tomcat hosting services, or remote virtual machines. Using this option usually people register a domain (again… paying for it) redirecting to those remote servers.
      Hope this helps… and sorry again for the lapse in your previous question.

  11. Shamitha
    May 11, 2010 at 3:16 pm

    Hello,
    Thanks for the reply. We tried Udig to solve the whole problem.
    Can you pls tell me how we can host data stored in geoserver be hosted on internet??

    • May 11, 2010 at 9:22 pm

      Hi,
      I just answered a similar question from “Anjana”. Working together? 🙂
      Regards

  12. Shamitha
    May 12, 2010 at 1:59 am

    Thanks for the reply!! ?Ya.. We both are working together. This is our final semester project.
    Really appreciate your help!!!
    Thanks again!! 🙂

  13. Anjana
    May 15, 2010 at 1:02 pm

    hello again,
    we need help regarding hosting our geoserver. its that we dont want to host it on the internet, we just want to make our geoserver accessible via the bluetooth to our mobile phones. we thought we will use the IIS web server present on windows to host our server….is it possible?we are really clueless since our J2ME application code asks for the url of the geoserver and it is not accepting localhost:8080/geoserver/….so we need to provide an IP for geoserver but still display the maps offline..please help us regarding this issue 😦

    thanks a lot for your support 🙂

    • May 16, 2010 at 11:30 am

      Hi,
      about hosting a j2ee application on an IIS server, it looks possible, but not directly because IIS can not . I will have to install a connector (isapi_redirect), see this.
      About “it is not accepting localhost:8080/geoserver/”: I’m guessing that the j2me application is running on a mobile device. So the DNS localhost does not apply. If you were already able to establish a network connection using bluetooth, so you should use the server’s machine network name. I haven’t got the experience in offline mapping apps, but of course you’ll have to cache the map images locally on the j2me application. Once I worked with J2ME record store, which I found to be useful for small amounts of info. A better way would be to cache them in the memory card.
      Once I was able to share an internet connection between computers using bluetooth. But now, being the second machine a mobile device (most likelly a phone) I don’t have a clue if that is possible. This article shows how to make a bt network connection using a PC and a pocket PC. However my mobile phone (nokia n70) misses those configuration options.
      Sorry about the vague answers but it’s all I know.
      Good luck 😉

  14. Anjana
    May 17, 2010 at 6:11 pm

    hello again,
    thanks a lot sir…that really helped.we will try with the caching operation and let you know how it worked soon 🙂
    thanks a gain 🙂

  1. No trackbacks yet.

Leave a comment