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?
2 Answers
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:
-
1Wow Thats awesome. Exactly what I was looking for. Thanks for that.Toby– Toby2023-05-31 15:03:26 +00:00Commented 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– Toby2023-06-01 13:52:25 +00:00Commented Jun 1, 2023 at 13:52
-
Not sure what you mean? Can you explain?Babel– Babel2023-06-01 14:13:43 +00:00Commented 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– Toby2023-06-12 10:10:35 +00:00Commented Jun 12, 2023 at 10:10
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 ...


elsestatments?= truestatements