Skip to content

Instantly share code, notes, and snippets.

@kgjenkins
Last active February 12, 2023 13:29
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kgjenkins/1af82a8ffdc3c99bb886e6cd9cdc298f to your computer and use it in GitHub Desktop.
Save kgjenkins/1af82a8ffdc3c99bb886e6cd9cdc298f to your computer and use it in GitHub Desktop.
Labels with callout lines in QGIS

Labels with callout lines in QGIS


UPDATE: As of October 2019, QGIS 3.10 has built-in support for label callout lines!


Callouts, sometimes called leader lines, are lines between labels and features on a map. They are useful when constraints of space force a label to be moved away from the feature. The callout helps to reconnect the label to the feature it refers to.

image

There are probably multiple ways to do this, but here is one way using a geometry generator in QGIS 3.x:

callouts_qgis

1. Turn on labels for the layer

  • Style dock > Labels tab
  • Change "No labels" to "Show labels for this layer"
  • Select the field to "Label with"

2. Move individual labels

The upgrade to QGIS 3.0 has made label placement easier to customize. When you move a label, QGIS tracks the new location using hidden auxiliary storage fields that we'll use to draw the lines.

  • Make sure the Label Toolbar is enabled (View menu > Toolbars)
  • Select the "Move Label" tool image
  • Click and drag the to move the labels. (The first time you click to move a label, you will be asked for a primary key to use for tracking the labels. Pick a field that has an id or otherwise unique values within your layer.)

3. Add the callout lines

  • In the layer styling dock, click the green + to add a new marker component
  • Set "Symbol layer type" to "Geometry generator"
  • Set "Geometry Type" to "LineString / MultiLineString"
  • Enter the following expression:
make_line(
  make_point($x, $y),
  make_point( "auxiliary_storage_labeling_positionx" , "auxiliary_storage_labeling_positiony" )
)

image

4. Improve the label display

  • Add label buffers ("halos") so that the lines won't obscure the text of the label (3rd tab in the style dock)
  • Adjust the label placement (6th tab in the style dock), data defined > alignment
    • set the horizontal alignment to 'Center'
    • set the vertical alignment to 'Half'
@haubourg
Copy link

haubourg commented Jun 5, 2018

Hi, nice! Note there is a QEP for porting that to core qgis/QGIS-Enhancement-Proposals#47.
We probably will have a funder this summer for adding that in 3.4.
And, your idea is in my head around since the geometry generators landed in QGIS to improve EasyCustomLabeling https://github.com/haubourg/EasyCustomLabeling/, but I missed time !
(any PR welcome)

@ZKingDRFN
Copy link

Thanks for this tutorial!

Being able to use callouts for labels without having to add extra layers to the map is very useful.

@rjspiers
Copy link

rjspiers commented Oct 1, 2018

Hello - thanks for writing this really clear guide!

I've just used this method to add callout lines to polygon features by using the following expression in the geometry generator:

make_line(
  centroid($geometry),
  make_point( "auxiliary_storage_labeling_positionx" , "auxiliary_storage_labeling_positiony" )
)

@MicheleTobias
Copy link

Thank you for this tutorial! It was just what I needed. The video is great too!

@skylex72rus
Copy link

skylex72rus commented Feb 8, 2019

version with check size on papper:

if(
	  length( make_line(
		make_point($x, $y),
		make_point(  "auxiliary_storage_labeling_positionx" , "auxiliary_storage_labeling_positiony" )
	))/@map_scale *1000 >10  ---minimum length on papper in mm
,
	 make_line(
		make_point($x, $y),
		make_point(  "auxiliary_storage_labeling_positionx" , "auxiliary_storage_labeling_positiony" )
	)
,None
)

@zeddock
Copy link

zeddock commented Apr 6, 2019

@skylex72rus I could not get yours to work when on 3.6 and following this tutorial, but simply replacing with your txt. Both the original and the updated (auxiliary_storage_labeling_position) worked.
Please advise?

@kgjenkins
Copy link
Author

If you are labeling polygons, here's a technique for drawing the line to the nearest point of the polygon:
https://kartoza.com/en/blog/how-to-make-beautiful-lollipop-call-out-labels-in-qgis/

@kgjenkins
Copy link
Author

Note that QGIS 3.10 introduced built-in support for label callout lines!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment