How can I get an array of all visible layer names automatically using QGIS expressions? In principle it should work with the variable @map_layers
. Using the following expression works fine for what I want to do, but only in a semi-manual way:
array (
layer_property(
array_get (
@map_layers ,
0
),
'name'),
layer_property(
array_get (
@map_layers ,
1
),
'name'
)
)
I have to add manually an array_get (@map_layers, [index])
statement for each layer. I would rather like to iterate and get all visible layers automatically, indifferent of how many layers are visible. In a first step, I calculate the no. of visible layers: array_length (array_first (array ( @map_layers )))
(abbreviated no_visible
in the next expression).
Where I'm stuck is iterating to create the layer name for for each visible layer. I tried the following expression, but it returns no results: No feature was found on this layer to evaluate the expression
.
array_foreach (
generate_series(0,[no_visible]),
layer_property (
array_get (
@map_layers ,
@element
),
'name'
)
)
What am I doing wrong?
Update: The expression in itself seems to be correct, even though the one offered by @lejedi76 is more elegant to achieve the same goal. However, the behavior of QGIS is quite buggy when using either of these expressions to select several (raster) layers at once as input for a tool in batch mode - see https://gis.stackexchange.com/a/385917/88814 for details.