2

I´m new to QGIS yet very exited of the learning curve and automatisms it provides. But at some point i dont manage to work around a problem. I want to label a vector layer with differnt attributes in a netdocumantation. I have 7 attributes for different types of cables, the attribute type is boolean, so that i just have to checkbox which cables are inside the trench. When i label the trench now, i get empty lines where the cables arent checkboxed. How could i get rid of these?

enter image description here

3
  • 1
    what happens if you take out the else statments?
    – Ian Turton
    Commented May 19, 2023 at 16:16
  • you can even take out also the = true statements
    – jpinilla
    Commented May 19, 2023 at 17:26
  • 1
    Hi Toby, welcome to GIS SE. Great to have you here. Be aware: you should add your code attempt/epxressions always as text, not as image. Posting a screenshot in addition is always welcome.
    – Babel
    Commented May 19, 2023 at 20:43

2 Answers 2

2

The advantage of this solution is that it works automatically, without any manual intervention: you can use the expression without any change regardless how your fields are named and how many fields you add (or delete or rename).

So to automatize the process and avoid the need to manually repeat the expression for every single attribute field, you can use the functions attributes() and map_akeys() to get a list of all fieldnames.

Then check which of these fields contain the value true and return the names of of these fields only. Use this expression that will output a list with each fieldname in a separate row, avoiding any empty rows:

array_to_string (
    array_filter (
        array_foreach(
            map_akeys(attributes()),
            if (
                attribute (
                    get_feature_by_id (@layer, $id),
                    @element
                )='true',
                @element,
                ''
            )
        ), 
        @element <>''
    ),
    delimiter:='\n'
)

The expression, used here as source of a dynamic label. It takes into consideration only those fields that contain as value the text string true and in this case returns the fieldname, with each name on a separate row, but without any empty rows:

enter image description here

4
  • 1
    Wow Thats awesome. Exactly what I was looking for. Thanks for that.
    – Toby
    Commented May 31, 2023 at 15:03
  • is there a possibility to add also add Numbers instead of the boolean? Like i have two times of one Attribute?
    – Toby
    Commented Jun 1, 2023 at 13:52
  • Not sure what you mean? Can you explain?
    – Babel
    Commented Jun 1, 2023 at 14:13
  • Like I Have two or more times of the same attribute. so i'm not just checkboxing it, but instead have a Nummer from 1 - 10 it can select from. And later, in the Labeling I will get like "1x SNRV 22x7 or, 2x SNRV 8x7" for example.
    – Toby
    Commented Jun 12, 2023 at 10:10
2

Each of the case when then statement produce a string, either with some content or empty. After that, you add a new line and proceed with the next variable. Consequently, you see empty lines where the boolean variable is false.

To overcome this, move the new line \n where you set the value instead of concatenating it later

case ... then 'abc\n' else '' || case ...

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.