Guide to Advanced Labeling for OSM Roads

Advanced labeling in QGIS new labeling engine is mostly about data-defined settings. Almost any property of the label can be controlled.

For this example, we will try to mimic the look of the classic Google map with it’s line and label styles. The data for this post is from the OpenStreetMap project provided as Shapefiles by Cloudmade.

After importing the roads into PostGIS using PostGIS Manager Plugin, we can create a view that will contain the necessary label style information. The trick here is to use CASE statements to distinguish between different label “classes”. Motorway labels will be bigger than the rest and the buffer color will be the same color as used for the corresponding lines.

DROP VIEW IF EXISTS v_osm_roads_styled;

CREATE VIEW v_osm_roads_styled AS
SELECT *, 
CASE WHEN type = 'motorway' THEN 9
     ELSE 8 END
     as font_size,
'black'::TEXT as font_color,
false as font_bold,
false as font_italic,
false as font_underline,
false as font_strikeout,
false as font_family,
1 as buffer_size,
CASE WHEN type = 'motorway' THEN '#fb9139'::TEXT
     WHEN type IN ( 'primary','primary_link','secondary','secondary_link') THEN '#fffb8b'::TEXT
     ELSE 'white'::TEXT END 
     as buffer_color
FROM osm_roads;

In QGIS, we can then load the view and start styling. First, let’s get the line style ready. Using rule-based renderer, it’s easy to create complex styles. In this case, I’ve left it rather simple and don’t distinguish between different zoom levels. That’s a topic for another post :)

Google-style rules for OSM road data

Now for the labels! In “Data defined settings”, we can assign the special attributes created in the database view to the settings.

Completed "Data defined settings"

To achieve an even better look, go to “Advanced” tab and enable “curved” and “on line” placement. “Merge connected lines to avoid duplicate labels” option is very helpful too.

Finally – after adding some water objects (Cloudmade natural.shp) – this is what our result looks like:

Google-style OSM map

This solution can be improved considerably by adding multiple zoom levels with corresponding styles. One obvious difference between the original Google map and this look-alike is the lack of road numbers. Tim’s post on “shield labels” can be a starting point for adding road numbers the way Google does.

About these ads
9 comments
  1. Gabriel Giroux said:

    Woooh
    What a nice tips !!!

    Thank Underdark

  2. looks osm :)

    • underdark said:

      … and I was hoping it looks google :)

      • googlosm then!

  3. Underdark,
    Many thanks for your post – it’s awesome :). I would also add to import OSM file directly, one can use the osm2pgsql tool. If anyone uses this method, then they will need to create primary index columns in the road, line and polygon tables. The tables are planet_osm_roads, planet_osm_line and planet_osm_polygon respectively. Also, the field to create view (thus customising the fonts) is “highway” instead of “type”.
    Since your blog is one of the best around regarding QGIS and PostGIS Manager, it would be useful for future readers to have an article on postgis (and possibly installation) – only a suggestion.
    Again, many thanks.

    • underdark said:

      Thank you for the kind words.
      I tried osm2pgsql once with an osm file of whole Austria and it did not work for me. Maybe I should give it another try with a smaller file.
      Regarding PostGIS, do you have any specific topics in mind besides installation? Installation is highly operating system dependent and it’s rather trivial on Windows if I remember correctly.

      • You are right about installation because it’s not the same on all operating system. What I really meant was a brief explanation on setting up postgis like the schema, spatial and geometry table or the templates. Then add some links for further info :-)
        On installation, the main problem I had was under Ubuntu I was installing Postgresql and Postgis separately (using apt-get). But really, the package to be installed is postgresql-xxxx-postgis-xxx.

        The other thing is I thought (at the beginning) the process was to import the OSM data from QGis to Postgis and then back. I came under this impression because QGIS has an OSM plugin but it skews the map. Then I could be the only one who had this misunderstanding.

        As I said, it’s only a suggestion :-) You are doing a great!

Follow

Get every new post delivered to your Inbox.

Join 3,187 other followers

%d bloggers like this: