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
CASE WHEN type = 'motorway' THEN 9
ELSE 8 END
'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
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.