Conditional Labels in QGIS

The latest QGIS development build (1.9.90) has a new feature “expression based labeling” which can be used to create conditional labels. One typical use case would be if you want to label only certain (high-level) road classes in your road layer. By default, QGIS labels the features rather randomly:

default labeling

How can we label only the more important roads? Here is an example using OSM data imported into PostGIS using osm2po:

If you have loaded OSM using osm2po, your OSM table will contain a “clazz” attribute. (Check osm2po.config for the exact mapping.) To label only motorways, trunks, primary and secondary roads and nothing else, I wrote this labeling expression:

substr(osm_name, 0, (clazz = 11 or clazz = 13 or clazz = 15 or clazz = 21)*-1)

If clazz equals 11, 13, 15 or 21, the expression returns the value of osm_name. Otherwise it returns an empty string. (All checks will return false or 0 which causes the function to evaluate to substr(osm_name,0,0).) Kudos to Giuseppe Sucameli who explained this on the mailing list.

expression based "conditional" labels

About these ads
4 comments
  1. miblon said:

    Great little tutorial that shows the essentials!

  2. Perfect, I was struggling with the CASE ELSE statement without any advance. This works smooth!

Follow

Get every new post delivered to your Inbox.

Join 3,404 other followers

%d bloggers like this: