2

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?

11
  • How did you "group" the layers - in the layers panel?
    – Babel
    Commented Jan 25, 2021 at 21:29
  • Yes, in the layers panel. Commented Jan 25, 2021 at 23:30
  • What you want to create is a virtual raster - a virtual "layer" is something completely different, it applies for vector data only. I edited the question accordingly.
    – Babel
    Commented Jan 26, 2021 at 8:24
  • Adding to my answer: maybe there is an option using Python. I can't help you in this. But probably you have more chances to get an answer to this if you ask a new question that focuses on that aspect: is it possible to access layer groups from the layer panel with python? Be aware that questions concerning python, it is normally expected that you already have some code and can show what you tried and where you're stuck.
    – Babel
    Commented Jan 26, 2021 at 10:38
  • OK, thanks for the edit but I don't know where I implied vector. Also, I did not intend to ask about python, although it may be doing the heavy lifting in the background, I am using the menus in QGIS 3 and have no code to show. I simply thought the menus had some option I overlooked. I was hoping the layer group was sufficient to filter the input of the dialog box rather than plucking them one by one from the 700 files in the project. I was asking if it possible to do with layer groups. Commented Jan 26, 2021 at 17:13

2 Answers 2

1

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.

4
  • Map themes at least can be accessed in the Model Builder, so there might be a way. Maybe ask a focused question here if/how it is possible to use map themes for what you want to achieve. Another possibility would be:1. Save a copy of your project. 2. delete all but one group 3. Create virtual raster of all layers left in the project 4. re-open the saved original project, repeat steps 2 and 3. This might be the most time-consuming option I see, but it's more a workaround than an elegant solution.
    – Babel
    Commented Jan 26, 2021 at 17:39
  • Sorry, I posted to soon and Stack Exchange timed out my edit. So I deleted it. Commented Jan 26, 2021 at 17:41
  • That most recent suggestion is what I will do. It preserves the groups and the project itself is throw away once I get each island exported as its on DEM. BTW I noticed this and thought it might be useful: Right click the group and choose 'Copy Group' paste that into a text editor and get an XML listing of every detail of every file in that group. Sort the lines and pull out the path to each file in the group. Useful for scripting I guess but I did not know all that XML was lurking below the surface. Commented Jan 26, 2021 at 17:45
  • Hope that this will work for you - indeed an interesting question, I hope there would be a better way. I included this last proposal in the answer above.
    – Babel
    Commented Jan 26, 2021 at 17:48
0

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')
  )
)

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.