2

My first map shows typical two-language map in Finland. I have used first rule green (finnish name) and after that rule magenta (swedish name). Labeling looks now a quite fine but still I want to add missing street names (marked red circles). I want to fill those areas using smaller font and shorten forms about name ending.

enter image description here

Next I create new label rule (black color) where I reduce size of font and I want to prioritize this rule lower than green and magenta. I have set priority value as lower as possible and also z-index is zero or under. Green has 1001 and magenta has 1000. Priority order should be green, magenta, black.

enter image description here

enter image description here

Next I check result which is not wanted. There black has replaced many label places from green and magenta rule. How to set black rule really under green and magenta? Is this bug in QGIS labeling or is there some settings which need to do?

enter image description here

  • While it could be helpful to provide your labeling rules, to me it seems you set the black labels priority to the highest possible one (2nd screenshot, bottom). – Erik Oct 17 at 11:39
  • Do you have some rule template which I can try to set green and magenta as highest as possible and black rule as lowest as possible? – Mickeler Oct 17 at 11:43
  • Just slide the block at the bottom to the left for black? – Erik Oct 17 at 11:45
  • Thanks, I replaced the settings and the final result pictures using lowest priority level. Unfortunately there is not big changes about original. – Mickeler Oct 17 at 11:58
1

The point of rule-based labeling is that each label has a "rule" or filter, so it only displays in certain situations. In this case, you want the smaller black labels to display only when the road is long enough for all three labels, you want the larger magenta to display only when the road is long enough for both two labels, and you want the green labels always.

Put filter expressions in the text box called "filter" ("Suodatin" in your version of QGIS)

enter image description here

To test whether the road is long enough for all three labels, we must compare the length of the road with the length of the labels.

  • Measure the road length with this function: $length.
  • Measure the length of the labels with this expression (where "name1" "name2" and "name3 are the attribute table fields used for the labels):

    length("name1") + length("name2") + 0.8*length("name3")`. 
    

    length("name3") is multiplied by a conversion factor (in this case 0.8) because the "name3" label is a smaller font than the other two. Calculate your own conversion factor by dividing the font size of the "name3" label by the font size of the other two labels.

  • For the top priority (green) labels, use no filter expression

  • For the medium priority (magenta) labels, use a filter like this: $length > (length("name1") + length("name2"))*50

    Where that expression says 50, substitute a value that converts the label length into the same units as the road length. It's probably easier to find the right value by trial and error than to try to calculate it. If you want the labels to work at different map scales, include the @map_scale variable.

  • For the smaller black labels, use a filter expression like this: $length > (length("name1") + length("name2") + 0.8*length("name3"))*50

enter image description here

Note: Ignore the formulas in the screenshots. I heavily edited this answer based on feedback, so the screenshots don't match the formulas in the text.


Here's some unsolicited advice. Feel free to ignore it.

I find the green labels on your map hard to read. They're just a bit too bright of a green, and the gaps in the white background don't help. I suggest using a slightly darker green, and using a solid white background or white text buffer.

  • I'm not sure I understood exactly how you wanted to prioritize your labels, but hopefully this answer gives you the information you need to set them up as you need them. Maybe you want three different road length categories, where the longest roads get green and pink labels, medium-length roads get green labels, and short roads get black labels. – csk Oct 17 at 15:44
  • How about those missing street names which length are over 100 metres? My idea is fill street name labels in priority order, at first Green rule, if there is still space then rule Magenta and if there is still empty space rule Black. – Mickeler Oct 18 at 4:25
  • 1
    Oh, this is more complicated than I first thought. I think we'll need to use the string length function to check how long the road name is, and compare it to the length of the road segment. I'll update my answer. It will take some trial and error of your part to find the right measurements. – csk Oct 18 at 14:13
  • Do you think, is this QGIS bug which should be reported? github.com/qgis/QGIS/issues – Mickeler Oct 21 at 5:51
  • Which part of this seems like a bug? – csk Oct 21 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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