TransWikia.com

Copying all visible layers to new group

Geographic Information Systems Asked on June 12, 2021

I would like to copy all visible layers in a project to a new group. Does anyone know of a quick way of doing this?

Background:
We have a Project that acts as a library for all the spatial data we hold.

When creating output-oriented Projects, we identify the data required in the library, copy it to a new Group and then that new Group becomes the basis of the new Project. Being able to batch copy all visible (displayed) layers to the new group would massively speed up the new Project generation process and keep the workflow (and datasets) clean and simple.

One Answer

Use the following script.

root = QgsProject.instance().layerTreeRoot()
group = root.addGroup("group")

for layer in root.children():
   if layer.isVisible() and layer != group:
       _layer = layer.clone()
       group.addChildNode(_layer)

enter image description here

Note: If there is another visible group layer including visible/invisible layers, that group layer is copied with its all layers to new group layer.

Answered by Kadir Şahbaz on June 12, 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