OpenStreetMap(OSM) cartography using QGIS (part 2)
In my previous blog post l mentioned that l would share on how to download OSM data, load it into Postgres, connect database to QGIS and view OSM data in QGIS. I will further explain how l created the styles in my swatch board using QGIS new symbology....but lets first download the data
OSM test dataset
The OSM project provides free spatial data which you can download and use for your own projects without any restrictions. Visit http://wiki.openstreetmap.org to learn more about OSM. My test dataset was Bayern in Germany therefore l downloaded OSM data for this region from CloudMade downloads site. You can find data for various places in different countries in the world from this site or alternatively use OSM data from Geofabrik. To download data do:
wget -c http://downloads.cloudmade.com/europe/germany/bayern.osm.bz2
Loading data into Postgres
I used osm2pgsql to import OSM data into Postgres. This article has general information on osm2pgsql: http://wiki.openstreetmap.org/wiki/Osm2pgsql. You can also use OSMOSIS to load OSM into Postgres. To install osm2pgsql do:
sudo apt-get install osm2pgsql
then,
time osm2pgsql -s -v -d osm -C 3000 bayern.osm.bz2
to load the data into Postgres, where osm is the name of the database and bayern.osm.bz2 is the data to be iMported into Postgres. If import is successful, connect the database to QGIS.
Connecting PostGIS to QGIS
There are various GIS packages which one can use to connect to PostGIS but l personally prefer QGIS, mostly because of its clean user-friendly interface. To connect to PostGIS from QGIS, choose Add PostGIS Layer from Layer in the Menu bar. Alternatively click on the Add PostGIS Layer icon which appears in the toolbar. A dialog where you must enter your database credentials will pop-up.
After entering your database information, Click Test Connect. If connection is successful, Click OK to remove the dialog and Connect to view PostGIS tables. The tables should appear as shown below, with names starting with planet_osm. The type column in the dialog below shows the geometry of the feature.
Note that there are two linestring types. In my case, one of the linestrings (planet_osm_roads) represented roads only and the other (planet_osm_line) had both roads and other line features such as rivers. I noticed that some of the roads appeared in the lines table but not in the roads so you must check both tables to avoid omissions. Click on the tables in the list and ADD, to view the features in QGIS.
After connecting the OSM database to QGIS, I opened the attribute tables to analyze the tables. Unlike the normal QGIS shapefiles, OSM stores everything in one huge database. Features are made up of nodes(basic unit, lat-long coordinates), ways(series of nodes) and relations. Since l had never used OSM data before l read up on the format of OSM data and how attributes were defined from the OSM wiki page http://wiki.openstreetmap.org/wiki/. For more detailed information of how to use OSM data in QGIS you can read this article http://www.qgis.org/wiki/Using_OpenStreetMap_data
OSM External Data - Coastlines
One of the problems l faced was on finding good quality large scale global coastline datasets. I decided to search external data for coastlines after realizing that the coastlines l had downloaded from OSM were not accurate (in most cases my roads would go over the coast). To make sure that my map wouldn't be spoiled by incorrect coastlines l thought having a set of coastlines at various scales (1: 10mil, 1: 2mi, 1: 1mil, 1: 250 000 and 1: 70 000) would be best. But this seemed to be an impossible mission since l only got 1:10mil and 1:1mil (VMap). The naturalearthdata site with coastlines at 10mil, 50 mil and 110 mil can be helpful but there are no 1:2mil, 250th and 70th coastlines. I found this site NOAA coastline extractor but the 1:70th medium resolution coastlines only partially covers the US and the rest of the world is omitted. I asked online but still didn't get the coastlines l wanted and ended up using administrative boundaries http://gadm.org/world at small scales.
Applying QGIS Styles to OSM data
To apply swatch board styles to OSM roads using QGIS right click the layer name (in my case planet_osm_roads) and click Properties. Click the New Symbology button from the Symbols tab and click OK when asked if you wish to use the new symbology implementation for the layer. The Properties dialog box will slightly change. The advantage of using new symbology is that you can create your own styles, save them within QGIS and quickly re-access them.
Click the dropdown next to Single Symbol and select Categorised. The Categorised symbol renderer renders all features from a layer using a single user-defined symbol, which color reflects the value of a selected feature’s attribute. Click the drop-down next to column and choose highway as the attribute. The highway tag in OSM is the primary tag used for any kind of road, street or way. Click classify and you will get a list of all the road classes, though they will have the same symbol.
To apply individual styles to road classes, double click the symbol to open the a Style Selector and click Change. A Symbol properties dialog which you can use to create your own symbol will appear.
To create a symbol with a fill and outline color, select Simple Line as your Symbol Type. Use Color to set color , Pen width to set the width of the road and Pen style to set the style of the line (solid, dash or dot). Click the Add symbol layer button at the left of the Symbol Properties dialog to add the outline color for the road. A new Simple Line will be added to the Symbol layers list. Use Color to set the color and increase the Pen width to a value slightly higher than the fill. Click the blue arrow to move the outline color below fill color. You will be able to see the symbol you created from the symbol preview.
If you're happy then click OK and select the Save as Style button to save the style you created. Create as many styles as you like in the same manner and save them. The Style manager stores all your styles and allows you to add, edit, remove, export and import styles. After creating and saving styles you should have a list of all your styles.
My Layer Properties dialog after categorizing and styling the roads is shown below.
Part of my roads rendered using QGIS new symbology with categorized classes.