Managing layers from a specific group with PyQGIS
You should add layer()
method to layer
object. layer
is an instance of QgsLayerTreeLayer
. It doesn't have wkbType
attribute, but QgsVectorLayer
has.
Change layer.wkbType()
to layer.layer().wkbType()
. layer.layer()
returns the map layer associated with layer
.
root = QgsProject.instance().layerTreeRoot()
my_group = root.findGroup('My group')
for layer in my_group.children():
if isinstance(layer, QgsLayerTreeLayer):
layer.setName(layer.name() + "_" + QgsWkbTypes.displayString(layer.layer().wkbType()))
If each layer of your QGIS project has a unique name, you can use the following code.
The loop retrieves the name of the layers of your group then you will identify the layer with the mapLayersByName
method and you will be able to manipulate it easily.
root = QgsProject.instance().layerTreeRoot()
my_group = root.findGroup('My group')
layer_list = [layer.name() for layer in my_group.children()]
for layer in layer_list:
update_layer = QgsProject.instance().mapLayersByName(layer)
for lay in update_layer:
lay.setName(lay.name()+"_" +QgsWkbTypes.displayString(lay.wkbType()))