OpenStreetMap and GIS: Bridging the gap.

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”.
qgis2
Click to enlarge
  • Fill in the fields accordingly and click “OK”.
  • Connect to the database and select the required tables to import.
qGIS
Click to enlarge
  • Click Add.
qgis3
Click to enlarge

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.

Render Maps Quickly

If you want to render you OpenStreetMap data quickly into beautiful maps, Mapgen is the tool for you. A very easy to use, perl script, with less dependencies. Mapgen supports rendering to SVG, PNG and PDF. All you have to do is to get the package and then command!  The rules file is easy to tweak for your custom needs.

I have packaged the required stuffs into a tar.gz. Download it from here. The Manual is available here.

For the impatient, use the following command to get the map rendered.

perl mapgen.pl -in = file.osm -style=mapgenRules.csv -out=map.svg

A small area of Calicut

I managed to render a small area of Calicut in few seconds.

Rendering Maps: A quick how-to

After a bit of mapping at the Technopark, we planned to render the map offline. This being a step towards the Fort Cochin Heritage Walk Mapping. So, I banged into the #osm channel on irc.oftc.net with the question, the first answer was Mapnik. But Mapnik, essentially, requires you to generate the Postgresql database instance out of the .osm file using osm2pqgsql or osmosis.
But, the quick method is to use Osmarender. The following steps would let you render a good .osm file into a map.

  1. Dowload the xml or .osm data subset of your area of interest.
  2. If you do not have installed subversion system, type sudo apt-get install subversion
  3. Now, you need to get the latest source files of Osmarender, for that type  svn checkout http://svn.openstreetmap.org/applications/rendering/osmarender/
  4. cd to the directory. Probably cd /home/<user_name>/osmarender/
  5. Type  apt-get install xmlstarlet
  6. Now, call the magic!
  7. Type xmlstarlet tr ~/osmarender/xslt/osmarender.xsl  -s osmfile=<file_name>.osm  ~/osmarender/stylesheets/osm-map-features-z17.xml  > <destination_file_name.svg>
  8. You’re done!

Now you should get an svg of the map. I got something like this by tweeking the stylesheets a bit

Map
Map

You can further make additions to the stylesheet files and adjust the parameters to suit your needs. More information is available at the Osmarender wiki.