Advertisements

How to Label Only Selected Features in QGIS

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.

The problem

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:

  1. Select the features you want to label
  2. Open attribute table
  3. If you don’t have label attributes ready yet: Add two type “real” columns called e.g. “label_x” and “label_y”
  4. Invert the selection (3rd button in attribute table window)
  5. 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)
  6. Close field calculator and attribute table
  7. Save your edits
  8. Open the labeling dialog and set “data defined settings” – “x coordinate” and “y coordinate”
  9. Enable “Label this layer” and specify the label field
  10. Done

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!

Advertisements
4 comments
  1. Stephan van den Akker said:

    – Right-click a vector layer, choose Properties
    – Choose “Labels” on the left hand side
    – Choose tab “Label Properties” on the right hand side
    – On the top panel “Basic Label Options”: check “Label only selected features”.
    Done!

    • underdark said:

      Hi Stephan,
      You’re right, this works perfectly fine with old style labeling but not with the newer more flexible one.
      Thanks for posting!

  2. Hello,
    I’m using similar method, but insted X,Y value I’m setting font size to zero.

    Great blog!
    Cheers

    • underdark said:

      Hi Piotr,
      That works great too. Thank you!

%d bloggers like this: