8

I have some layers loaded in QGIS, in various groups. I want, with PyQGIS, do some stuff on layers from one of theses groups, like adding geometry type to the layer name.

This way, I can do it on all layers :

layers = QgsProject.instance().mapLayers().values()
for layer in layers:
   layer.setName(layer.name()+"_"+QgsWkbTypes.displayString(layer.wkbType()))

This way, I can access layer names from one specific group :

root = QgsProject.instance().layerTreeRoot()
my_group = root.findGroup('My group')
for layer in my_group.children():
   print(layer.name())
   if isinstance(layer, QgsLayerTreeLayer):
      layer.setName(layer.name()+"_"+QgsWkbTypes.displayString(layer.wkbType()))

The "print" of layer name works, but I can't change name, either get the geometry type. I get this error :

AttributeError: 'QgsLayerTreeLayer' object has no attribute 'wkbType'

Does someone know what is the correct way to do that?

3
  • Do you get any error? Sep 21, 2020 at 13:51
  • Yes, the Attribute Error (the last line of code in my post)
    – Cupain
    Sep 21, 2020 at 13:56
  • I missed that. :( Sep 21, 2020 at 14:09

2 Answers 2

13

You should add layer() method to layer object. layer is an instance of QgsLayerTreeLayer and does not have wkbType attribute.

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()))
1
  • Perfect ! Thank you.
    – Cupain
    Sep 21, 2020 at 14:09
5

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()))
6
  • Unfortunately, layers can have the same name, and different geometry. For instance, i have "railway_rail" as point, and "railway_way" as line. That's why I want to add the geometry type to the name
    – Cupain
    Sep 21, 2020 at 13:41
  • "railway_rail" and "railway_way" don't have the same name. So it's ok. Sep 21, 2020 at 13:45
  • my mistake, railway_rail and railway_rail
    – Cupain
    Sep 21, 2020 at 13:46
  • I updated my code, it works with your situation. Ask me if you need any explanations Sep 21, 2020 at 13:59
  • 1
    It works and it's smart, but Kadir Şahbaz' solution is easier and more efficient. Thanks for your help !
    – Cupain
    Sep 21, 2020 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.