2

I'm working with QGIS 3.22.11-Białowieża. In my project, I have four layers and I need to show in the print composer the name of those that are visible, but only among the first three (I don't want to get the name of the fourth one that is always visible). The expression proposed in this post works well but it returns all visible layers (which is not what I want):

array_to_string(
  array_foreach ( 
   @map_layers,
   layer_property(@element,'name')
  )
)

To create the array of layers I'm interested in, I've tried the following

array_to_string(
  array_foreach ( 
   array (layer1, layer2, layer3),
   layer_property(@element,'name')
  )
)

But it returns the name of all layers in the new array, no matter if they're visible or not. I guess I need to use the is_layer_visible function, but all my attempts have failed. How should it be written?

1
  • Try with the @map_layers_ids variable.
    – Mayo
    Commented Dec 7, 2022 at 16:27

1 Answer 1

2

You're almost there. Use is_layer_visible() with another array_foreach() function:

array_foreach (
    array_foreach (@map_layers,layer_property (@element,'name')),
    if (
        is_layer_visible(@element),
        @element,
        ''
    )
)

However, be aware of the fact that is_layer_visible() is only available in some contexts, see the comments to QGIS expression function is_layer_visible() missing in QGIS 3.28 and this GitHub issue.

6
  • Mmmm... I'm doing something wrong. If I use this expression as it is, it returns an empty string or, if I wrap it up in an array_to_string function, it returns "true, true".
    – jpinilla
    Commented Dec 6, 2022 at 18:16
  • Yes, the expression returns true/false. What should the expression achieve?
    – Babel
    Commented Dec 6, 2022 at 18:50
  • Oh, sorry, I see my question wasn't clear enough. Please see my edited question.
    – jpinilla
    Commented Dec 6, 2022 at 19:25
  • Don't have access to QGIS right now, but hope it works like this: see edited answer
    – Babel
    Commented Dec 6, 2022 at 19:52
  • Back to another machine, right now I can't test the expression, since the function is_layer_visible() is missing... see: gis.stackexchange.com/questions/446911/…
    – Babel
    Commented Dec 6, 2022 at 20:50

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.