2

In my project, I have a shapefile with line geometry. Sometimes there are more than one lines that follow the exact same route like the others, but they differ in their description. I would like QGIS to display the labels of each one of those features with a callout line pointing from the feature to the label.

The problem is that QGIS distributes the individual labels along the line.

Is there any way that QGIS bundles the labels in case there is more than one and displays them parallel under each other?

I have attached a screenshot for better understanding.

Here is how QGIS does it right now (bad)

Here is how QGIS does it right now (bad)

This is how it should look like

This is how it should look like

Edit: I could advance in finding an answer for the question. By creating an additional "Labels" line layer that will extract the information I need from the shapefiles it touches. I only have to draw the lines myself in the place where I want the labels to be placed at. I use the label expression

geomnearest('my layer', 'Field')

Now I have the problem, that there are more than one layers that I would like to have included in that labeling. The lines of these layers are in the exact same place sometimes, but not all the same. So wherever I place the "label line", I want the field information extracted from the features affected, whatever is in that location.

I have tried with aggregate(...), but that seems to list up all the feature names of that layer, not only of the ones in that place. I have tried to combine many geomnearest(...) code lines (one for every layer) and combine them with || and '\n' but the arising problem here is that once a layer is not present, the whole expression seems to not work anymore. How can I tell QGIS to just ignore a layer, in case it is not affected? I have tried to add a try(...) function, but that didn't work either.

Here is a code sample:

try(geomnearest('Layer1', 'Field') || '\n' ||
try(geomnearest('layer2', 'Field') || '\n' ||
try(geomnearest('Layer3', 'Field') || '\n' ||
try(geomnearest('layer4', 'Field') || '\n' ||
...

But like I said, once one of the layers is not there, QGIS does not seem to retrieve any information anymore. I had the idea to find an expression that first checks which ones of my layers are affected at the spot of labeling and then only those layers are called by the function or expression, but I did not find a suitable code for that.

Another idea that I had is that I could merge all layers into one layer and then use the geomnearest(...) function, but whenever I have updated line layers, I would have to merge all lines again. I would want to just have to overwrite the shapefiles and QGIS automatically will detect and re-label the lines. Is there maybe something like a "Virtual Field" for layers, where QGIS creates a virtual merged layer of whatever input shapefiles (as long as the same geometry, of course). I tried the Virtual Layer (as the name suggests it could be the answer for that), but I could not confirm that this does what I am having in mind.

1 Answer 1

1

You may try to merge the labels to only get one line with all the labels attached to the same line by using a dedicated virtual layer e.g below screenshot. Not a complete answer, but one solution to solve your issue maybe?!

enter image description here

1
  • Didn't work for me, but the idea seems to be good. Can you try that again maybe, and include more than one layer to extract information from? The fields would be the same in all layers ("Label" and "Length").
    – Kai
    Commented Oct 2, 2021 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.