How to make beautiful lollipop call out labels in QGIS

Posted by: Tim Sutton | in QGIS | 3 months, 1 week ago | Comments
Call out labels are a handy cartographic instrument for attaching labels to features on the map where you want the label to be offset from the feature being labelled. It allows you to prevent the map becoming overcrowded. I call the variant I describe here 'lollipop' labels because the 'callout line' is rendered with a decorative ball at the end.
Generating the callout geometry

I am using logic like this to make callout labels using a geometry generator:
make_line(
closest_point($geometry,
make_point( "auxiliary_storage_labeling_positionx" , "auxiliary_storage_labeling_positiony" )),
make_point( "auxiliary_storage_labeling_positionx" , "auxiliary_storage_labeling_positiony" )
)
The line runs from the closest point along the edge of the polygon to the bottom left corner of the label box. With this configuration it works well when my ‘lollipop’ callout label is north-east of the polygon being labelled:
But doesn’t work when the label is e.g. south-west of the polygon as the label falls over the call out line:

Configuring data defined label alignment

To address that I used data defined label alignments in the "Layer Properties ->  Label -> Placement" options for my layer:


Here are the expressions I used for horizontal:


if (
  X(closest_point($geometry,
  make_point( "auxiliary_storage_labeling_positionx" , "auxiliary_storage_labeling_positiony" ))) >
  X(make_point( "auxiliary_storage_labeling_positionx" , "auxiliary_storage_labeling_positiony" )),
  'Right', 'Left’)



Which gives this:



And Vertical:

if (
  Y(closest_point($geometry,
  make_point( "auxiliary_storage_labeling_positionx" , "auxiliary_storage_labeling_positiony" ))) <
  Y(make_point( "auxiliary_storage_labeling_positionx" , "auxiliary_storage_labeling_positiony" )),
  ’Top’, ‘Bottom’)


Which gives this:



Now with everything in place you can use the label move tool to shift your labels to where you want them. QGIS will then generate a nice lollipop callout to each label with the lollipop's circle arriving at the top / left / bottom / right corner as appropriate.
Current rating: 5

Comments

Template by Blacktie Mezzanine theme by CodingHouse