Kartoza - How to Make Beautiful Lollipop Call Out Labels in QGIS
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.
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.
Tim Sutton
Tim started his career by working in Nature Conservation in South Africa. Some years later, around 1998, an opening in the newly formed GIS group in the organisation where he worked presented an opportunity to combine his love of the environment with his enthusiasm for computers. What followed quickly became a deep dive into GIS (including obtaining a Master's Degree in GIS and Environmental Studies) and Open Source. He discovered Linux, also around 1998, and became an ardent fan of using and creating Open Source software. The announcement of the first release of QGIS in 2002 was another key milestone, with Tim quickly becoming deeply involved in the development of QGIS, as well as helping to build the community platforms and governance structures around the project. Formerly the QGIS.org Board Chair, Tim was awarded perpetual Honorary QGIS PSC Member status in 2018 and continues to play an active role in many aspects of the QGIS project. Tim is also engaged in the broader Open Source GIS ecosystem, including having the honour of being an OSGEO Charter Member and promoting the wide array of world-changing Open Source GIS tools provided under the OSGEO umbrella (and beyond) to pretty much anyone who will listen.
No comments yet. Login to start a new discussion Start a new discussion