3

Labeling a line layer in QGIS with many features, not all labels can be drawn without overlapping. Best result in the sense to show as many labels as possible without overlap I got using Parallel mode with allowed positions above and below line, see screenshot:

enter image description here

As you can see, I have set a distance of 12 of the labels from the line. It would now be great to have at least two or three varying distances for the labels, than more labels could be placed. A workaraound would be to use a case when ... condition, like: case when $id %5 = 0 then 20 when $id %4 = 0 then 15 (...) end. However, this is not flexible at all:

enter image description here

Is there an option to allow QGIS to autonomously choose from e.g. two or three fixed distances (like: 5, 10, 15) in a way to automatically optimize label placement?

4
  • To make it a little more scalable, you could use an array of predetermined distances and index it with the modulo of the id. case when id % 3 then array_get(@value_array, id % 3). This would mean every third label is at a set height, etc. (I'm not sure what the modulo operator is off the top of my head). Thinking about it, I guess this is only useful for evenly spaced labels.
    – Matt
    Dec 11, 2022 at 16:36
  • That does not realle improve the case when... workaround I was thinking about, unfortunately.
    – Babel
    Dec 11, 2022 at 17:00
  • 1
    I’m not sure how to do it, but it could be a solution to first check the density of marks within a certain distance. If it is high, we should vary the distance more… so we could use the density to multiple a factor?
    – katagena
    Dec 11, 2022 at 19:28
  • 1
    I was working on a solution where lines representing the horizontal space occupied by the all the labels (at a fixed height) were collected into a multiline (which overlaps where they are dense), and then checking for the number of intersections of a combination of start point/end point for each individual label line with the collected geometry to determine the height. Unfortunately, I'm not quite there with the expression, but perhaps it gives you an idea. (it relied on representing the horizontal length of the labels in real-world meters, based on how many characters were in the label text)
    – Matt
    Dec 11, 2022 at 19:53

1 Answer 1

2

Playing around a little bit with the settings, there is an astonishingly easy way to achieve that: simply create a random value for the distance: rand(5,20)

enter image description here

2
  • 99% of the time, the solution is much simpler than it first appears. Could you combine this with an array of your original fixed values (5, 10, 15) and index it with rand, for a more uniform look, or does that not give enough options to the label placement engine?
    – Matt
    Dec 11, 2022 at 21:05
  • 2
    Yeah, simply use rand (1,3)*5 to get either 5, 10 or 15
    – Babel
    Dec 11, 2022 at 21:30

Your Answer

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

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