Ever wondered how to create multi-line labels in QGIS? The new labeling engine has a “Multiline labels” option but it’s not so obvious how to create a usable labeling attribute. Here is how it works (credits to @nhopton on QGIS forum):
- Create a big enough text field (if the data doesn’t contain any yet).
- In Layer Properties – Fields, chose a “Text edit” edit widget for the label field.
- Enter the multi-line text into the label field. You can do this using Attribute Table or Feature Form.
A feature form with "Text edit" widget
- Activate labeling. You’ll have to tick “Multiline labels” option in Layer Labeling Settings – Advanced – Options. That’s it:
Simple multi-line label example
A common use case is the wish to show multiple attribute values in a feature’s label. Using Field Calculator, you can combine them into multi-line labels. All you need is to combine the fields with the || operator and add ‘\n’ (newline) wherever there should be a line break:
Field1 || '\n' || Field2
Populating a multi-line label field using Field Calculator
And finally, the result:
Multi-line labels displaying city name and population
Please read the updated version for QGIS 2.8 and up!
The aim of this post is to describe a method for labeling of a subset of features within a layer using new labeling functionality.
Often, we want to label only a few features in a layer. Of course we can export those features to a new layer and label them that way, but that requires creation of additional files and layers within your project. Things will start to get messy fast.
Another approach is to delete unwanted label texts from the attribute table. This either means that you have to duplicate a “name” attribute and then start deleting from the newly created attribute table column or that you actually delete values in the original column. Both approaches are problematic. Either you produce redundancy that gets difficult to maintain (two attributes have to be updated if the name of a feature changes) or you loose information from the attribute table.
The suggested solution
Let me present a different approach using new labeling tools. The idea is based on moving unwanted labels out of view. This approach avoids duplication of features and duplication/deletion of label texts. And this is the workflow:
- Select the features you want to label
- Open attribute table
- If you don’t have label attributes ready yet: Add two type “real” columns called e.g. “label_x” and “label_y”
- Invert the selection (3rd button in attribute table window)
- Open field calculator and fill “label_x” and “label_y” fields of the selected features with 0 values (or any coordinates outside your map extent)
- Close field calculator and attribute table
- Save your edits
- Open the labeling dialog and set “data defined settings” – “x coordinate” and “y coordinate”
- Enable “Label this layer” and specify the label field
If you change your mind about a feature and want to label it later on: Simply delete the values in “label_x” and “label_y” fields (so they read NULL).
This works quite well for me but I’m aware that it’s still not optimal. Another “data defined setting” like “show this label (true/false)” would be more intuitive.
Have you found better solutions to this problem? Please, post them!
Three new label editing tools found their way into the QGIS developer version:
- move label tool … move text labels to a new position
- rotate label tool … allows for interactive rotation of labels
- label property tool … opens a dialog where users can manipulate the properties and text of a label
These tools allow you to mix fixed label positions and automatic label positioning inside a project. If the x or y attribute value is NULL, the position will be set automatically. When a label position is changed using “move label tool”, the position is written into the attribute fields and the label position for this feature is fixed.
To use these tools on existing layers, add x, y and rotation attribute fields (type double). By default, all values will be NULL and thus the layer will be labeled automatically. Now you’re ready to move and rotate the labels as you like.
For more information check: www.sourcepole.com
A useful and powerful way to define layer symbology and labels is using data-defined properties. While many of the options are self-explanatory, others require knowledge about the valid options.
Note: These options are currently only available for the “old” labeling under “Advanced” tab.
Valid options are: left, right, bottom, top, bottom_left, bottom_right, top_left, top_right
I successfully used the color names red, blue, green, black, yellow, and orange. Alternatively, you can enter color hex-codes: red = #ff0000