”— title: ‘QGIS Labels with leader lines’ layout: post tags: [] category: Uncategoried — Labels auto generate over the feature. This can get messy and confusing on a map, especially if there are lots of features close together. The solution is to move the labels out of the way and draw leader lines linking the features to the labels.
Set up labels:
<a href=”“https://gisdriverslicence.files.wordpress.com/2020/09/labelling.png””><img class=”“alignnone size-medium wp-image-47”” src=”“https://gisdriverslicence.files.wordpress.com/2020/09/labelling.png?w=300”” alt=”””” width=”“300”” height=”“127”” /></a>
https://docs.qgis.org/2.8/en/docs/training_manual/vector_classification/label_tool.html
Make labels moveable:
Chose a label and move it using the ‘move label’ tool. The first time you are asked to choose a primary key. This needs to be a field that is unique to every feature. Add an autoincrementing id or a virtual field with an $id expression if there is none available.
<a href=”“https://gisdriverslicence.files.wordpress.com/2020/09/move-label.png””><img class=”“alignnone size-medium wp-image-48”” src=”“https://gisdriverslicence.files.wordpress.com/2020/09/move-label.png?w=300”” alt=”””” width=”“300”” height=”“129”” /></a>
Create leader lines:
<a href=”“https://gisdriverslicence.files.wordpress.com/2020/09/leader-lines.jpg””><img class=”“alignnone size-medium wp-image-51”” src=”“https://gisdriverslicence.files.wordpress.com/2020/09/leader-lines.jpg?w=300”” alt=”””” width=”“300”” height=”“192”” /></a>
There are then numerous ways we can refine this system:
Only add leader to moved labels
Some labels can remain where they are autogenerated. In these cases we don’t need to add a leader line. So we can add some extra code to the geometry generator formula. <p style=”“margin:0;font-family:Calibri;font-size:11pt;””>IF( ““labelX”” IS NOT NULL, make_line(centroid($geometry),make_point(““labelX””, ““labelY””)), ‘’)</p>
Only label certain types of item:
Use expression based labelling and CASE WHEN function if you only want to label certain items.
<a href=”“https://gisdriverslicence.files.wordpress.com/2020/09/label-filtering.jpg””><img class=”“alignnone size-medium wp-image-53”” src=”“https://gisdriverslicence.files.wordpress.com/2020/09/label-filtering.jpg?w=300”” alt=”””” width=”“300”” height=”“87”” /></a>
<span style=”“font-weight:bold;”“>If item is within a polygon then label differently:</span>
For example, imagine a layer showing pubs, overlain over a polygon layer showing London boroughs. We want to label each pub with its name and the borough together e.g. ‘The Crown, Enfield’
Use refFunctions plugin and geomwithin expression to create a spatial join. Geomwithin has the syntax.
geomwithin(targetLayer,targetField)
We can then concanternate that with the pub name
““Pub”” || ‘, ‘ || geomwithin( ‘uk borough boundaries’, ‘Name’)
Alternatively we could use an if or case when logic to label features in different areas with differently.
https://gis.stackexchange.com/questions/152257/how-to-refer-to-another-layer-in-the-field-calculator
”