New Labeling in Quantum GIS

The new labeling setup in QGIS has been around for over a year now, and in the upcoming version it will become the default, replacing the old labeling. This new engine brings some advanced options that are quite worth learning, such as bulding labels from expressions, and conditional labeling. I’ll expand on some of these tricks that have already appeared in other QGIS blogs

The basics of the New Labeling already are included in theĀ QGIS Documentation. Data defined label placement has been improved and expanded compared with the old labeling. Now we can have columns in the layer’s attribute table for rotation of the label, buffer size and color, and even text alignment.

In Nathan W’s blog he introduced Expression based labeling. Now we can construct labels using combinations of math functions and string functions to manipulate any attribute column the way we want. Currently, in versions 1.8.x, find and click the “ABC” button to access the “Layer Label settings” window. Stay tuned for the next version of QGIS, when this New Labeling will become the default, and be merged into regular Layer Properties window. In the Layer Label settings window, after checking the checkbox to “Label this layer with” you can click the ellipsis button […] to open the full expression builder window, and use your imagination to create a variety of informative labels.

Here’s a classic example. If I add an Area column (using the Field calculator, $area function) the values I get, in a projected coordinate system. are square meters. But suppose I want to label a polygon layer with hectares? The label expression will be:
"Area" / 10000
But that’s not enough. This expression returns a real number with a long string of digits after the decimal point. So we instead can enter into the expression calculator:
toint( "Area" / 10000)
to round the number to an integer. And one more improvement: what about adding the “ha” tag as units to the labels?
toint( "Area" / 10000) || ' ha'

We also have the ability now to create conditional labels thanks to Martin Dobias. One example appears in Underdark’s blog. Here’s another straight forward example: I have a point layer of rain gauges. I want to label all those gauges with precipitation greater than 2 mm. No label at all should appear when the gauge shows < 2 mm.
CASE WHEN ("precip" >= 2) THEN "precip" END
Of course several WHEN {condition} THEN {label} pairs can be chained together to get several different categories of labels.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>