Skip to main content
4 of 6
added 40 characters in body
user2856
  • 74.7k
  • 7
  • 124
  • 211

Empty @layer_name variable with aggregate function in QGIS expression

When putting together an answer for this question, I attempted to use the @layer_name variable in an aggregate function to make the expression more generic and easier to use when running the "Geometry by expression" tool as a batch process over multiple layers.

The full expression I was trying to use is:

scale( geometry:=$geometry, x_scale:=4, y_scale:=4, center:=centroid( aggregate(layer:=@layer_name, aggregate:='collect', expression:=@geometry) ) ) 

However, when I run the tool with that expression, the processing log shows the following error which I assume is because the @layer_name variable is empty:

Algorithm 'Geometry by expression' starting… Input parameters: { 'EXPRESSION' : "scale(\n geometry:=$geometry, \n x_scale:=4, \n y_scale:=4, \n center:=centroid(\n\taggregate(layer:=@layer_name, aggregate:='collect', expression:=@geometry)\n )\n)", 'INPUT' : '/path/to/small.shp', 'OUTPUT' : 'TEMPORARY_OUTPUT', 'OUTPUT_GEOMETRY' : 0, 'WITH_M' : False, 'WITH_Z' : False } Evaluation error: Cannot find layer with name or ID '' Execution failed after 0.04 seconds 

In the screenshot below are the parameters I set for the tool and you can see that the @layer_name variable is populated in the function/variable selector description on the right.

tool parameters

Notes:

  • the input layer is a permanent dataset, not a temporary layer.
  • the error is the same whether the output is a permanent dataset or temporary layer.
  • this isn't run in a model, but directly from the processing toolbox.

How can I avoid hardcoding the layer name in this expression so the tool or a workaround using another tool that can be batched and applied to multiple input layers?

user2856
  • 74.7k
  • 7
  • 124
  • 211