Can someone reproduce the strange behavior I encounter in QGIS? See below ("My workflow") for project settings and workflow. Basically, I want to use GDAL Build virtual raster
in batch mode, using Autofill... / Calculate by Expression...
with an expression to select all visible raster layers in the project as input for the tool. In principle, I got it to work, but in an unpredictable manner. De-activating all plugins except Processing does not seem to resolve the problem.
The problem and what I tried
In QGIS 3.8, new options for autofilling batch Processing were introduced, see changelog.
However, in a use case (see below), I observed a strange behavior when using this to select all visible (raster) layers (grouped in layer panel) using two different expressions that should produce the same output. However, depending on the version and the machine, only one of the two expressions generates the correct output, in the other case no layer is selected.
I now had the possibility to test it on several machines (all Windows 10), and with different QGIS versions. Which one of the two expression works is not (yet?) predictable for me.
Based on the experince on the same machine (if not stated otherwise), I got the following results:
- QGIS 3.8.3: short expression works, long expression does not
- QGIS 3.10.0: short expression works, long expression does not
- QGIS 3.12.0: short expression works, long expression does not
- QGIS 3.14.0: long expression works, short expression does not work
- QGIS 3.16.3.: short expression works, long expression does not work
On three other machines, contrary to the results above, I get the following results:
- QGIS 3.16.0: long expression works, short expression does not work
- QGIS 3.16.0: long expression works, short expression does not work
- QGIS 3.16.3: long expression works, short expression does not work
Differences in versions 3.10/3.12. vs. 3.14/3.16
Up to version 3.12., the preview in the expression builder correctly shows the output. However, using the long expression, I get a preview like (with 3 layers visible): [ 'gt30e100s10', 'gt30e100n90', NULL ]
- so the NULL
value could be a hint where the problem is. But if I use array_length ([...])-1
, I get a previw like [ 'gt30e100s10', 'gt30e100n90' ]
, but still no selection is made when I click OK. I works fine however with the short expression, as stated above.
From 3.14 onwards, it always get the message: No feature was found on this layer to evaluate the expression
- but this does not affect output, as I get the correct selection once pressed OK (in several cases only, not always, as stated above).
In 3.10 and 3.12, when apllying the expression with OK, I get this error - even if the the selection is made correctly:
AttributeError: 'BatchInputSelectionPanel' object has no attribute 'value'
Traceback (most recent call last):
File "C:/PROGRA~1/QGIS3~1.10/apps/qgis/./python/plugins\processing\gui\wrappers.py", line 209, in widgetValue
return self.value()
File "C:/PROGRA~1/QGIS3~1.10/apps/qgis/./python/plugins\processing\gui\wrappers.py", line 806, in value
return self.widget.value()
AttributeError: 'BatchInputSelectionPanel' object has no attribute 'value'
In versions 3.14 and 3.16, I don't get this error.
My workflow
You find my project file here, but probably it is of little use as I can't provide the layer sources (linked below form the original provider) for copyright issues. However, setting up the project configuration is straightforward and you might also use other data.
This is the setting/workflow I used (for more details see this answer and Automatically create an array of all visible layer names in QGIS).
Create a new project and add a few raster layers - at least 4. I used GOTOPO30 DEM rasters (EPSG:4326), available from USGS Earthexplorer: I'm not sure if that plays a role, so probably using exactly this raster data set is not the issue, you could use other data as well.
In the layer panel, group the rasters to several groups:
Toggle visibility: only one group should be visible, the others un-checked/invisible.
Open
Menu Processing / Toolbox / GDAL / Build virtual raster
Click
Run as Batch Process…
In the column for
Input layers
, selectAutofill / Calculate by Expression…
Paste one of the following two expression: long one or short one in the expression builder window (see screenshot 1 below). Note: starting with QGIS 4.14, I get the output preview below the input field for the expression saying
No feature was found on this layer to evaluate the expression
(correct output previw for QGIS 3.8 to 3.12) - but strangely enough, that does not affect the output: I have this message also when the layers are correctly selected after pressing OK. But could it be a hint for what's going on? Anyway, if you pasted the following expression, click OK:
long expression
array_foreach (
generate_series(
0,
array_length (
array_first (
array (
@map_layers
)
)
)
),
layer_property (
array_get (
@map_layers ,
@element
),
'name'
)
)
short expression
array_to_string(
array_foreach (
@map_layers,
layer_property(@element,'name')
)
)
- What does the
[number] inputs selected
show (see screenshot 2)? Are the visible layers selected?
Screenshot 1, showing steps 4 to 7: Build virtual raster / Run as Batch Process… / Input layers / Autofill / Calculate by Expression… / [paste the second expression]
Screenshot 2, showing a correct output: 6 (all visible) raster layers are selected. In th "buggy" version, I get 0 layers selected, even if there are visible layers in the project: