TransWikia.com

How to build Virtual Raster using grouped DEM layers as input with QGIS 3?

Geographic Information Systems Asked by Richard Rasch on July 27, 2021

I have compiled about 700 DEM tiles (each in its own layer) for some islands. I would like to create a virtual raster for each island. I grouped the DEM layers by the island name in the layer panel. But when I try to choose input layers, the groups are not available to choose from and I am forced to choose from the original 700 layers. Is there a workaround for this or a clever way to export the layers within the group so I can load them as files?

2 Answers

You can run Build virtual raster in batch mode and than define a pattern as for which layers or files you want to use. If your raster layers already have in their name some characteristics that allow you to group them together based on that, it's going to be easy. Often, DEM files are named after the extent/area they are located - something like gt30e140n90 for example.

If not, it's going to be more difficult as you don't have an easy way to access the groups you created in the layer panel, e.g. via QGIS expressions. They seem to be pure visualization tools to organize the layer structure. So you must use a workaround. I see two (probably three) possibilities:

  1. Rename all layers by adding for expample a prefix that represents the group - like 1_ for group 1, 2_for group 2 etc. As an alternative, you could also save all the layers from the same group to an own folder. Than you are able to run the tool to build a virtual raster in batch mode and select a pattern for the layer- or filename, using autofill/Fill down, as described here: https://courses.spatialthoughts.com/advanced-qgis.html#batch-processing, but using expressions to define the layer-/file-name pattern to select the ones you want to use.

  2. Save a copy of your project. Than delete all but one group. Create a virtual raster of all layers left in the project. Than re-open the saved original project, repeat steps 2 and 3. This might be the most time-consuming option I see: not very elegant, but efficient.

  3. Using map themes instead of groups. I did not test it and I am not sure if it is going to work, just to give you an idea: define each group as map theme. Toggle visibility so that you see only one group, than create a new map theme. Repeat for all groups. Creat a Model using Model builder/Graphic modeler. There, you have the possibility to set map themes as input.

Correct answer by Babel on July 27, 2021

Thanks to the help of user @lejedi76 in this answer, there is a somewhat more elegant solution to your problem. It consists of the idea to use the variable @map_layers that returns a list of all visible layers:

  1. Toggle visibility for just one group of layers, than run create virtual raster in batch mode.

  2. For input layers, use Autofill / Calculate by Expression… and paste this expression (see below for an alternative):

array_foreach (
    generate_series(
        0,
        array_length (
            array_first (
                array ( 
                    @map_layers  
                )
            )
        )
    ),
    layer_property ( 
        array_get (
            @map_layers , 
            @element
        ),
        'name'
    )
)

Now, all the visible rasters are selected as input and you can run the tool. Just set another group to be visible and do the same again. You might even create a model to shortcut all the manual work.

Remark: I got the impression that using the expression to select visible layers as input can behave quite buggy. Sometimes it works, sometimes not (that's why I asked the question linked above). After several testing, each time restarting QGIS, I have the impression that the above expression works better than the one that follows. I'm still not sure about the cause (might have to do with a conflict with a plugin installed?). What could help: first select manually a few input layers, than de-select them (without running the tool). Afterwards use the expression for selecting input layers.

PS: As an alternative expression for step 2, this much simpler expression (tribute to @lejedi76) should also work. I managed it to run, however, in my configuration it works less reliable.

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

Answered by Babel on July 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP