4

I have a project which has dozens of groups and subgroups. E.g.:

Main_Group > Sub_Group_1 > Sub_Group_1A > Layer_1A
                                        > Layer_2A

How can I access those layers which are contained within Sub_Group_1A?

I currently have this:

root = QgsProject.instance().layerTreeRoot()
for child in root.children():
  if isinstance(child, QgsLayerTreeGroup):
    if child.name() == "Main_Group":
        for subChild in child.children():
            if isinstance(subChild, QgsLayerTreeGroup):
                if subChild.name() == 'Sub_Group_1':
                    # What next?

I have read through the following sources and can get layers in Sub_Group_1 but not in Sub_Group_1A:

  • 2
    Would you like to access all layers in the tree or would you like to access specifically those of the Sub_Group_1A? – Germán Carrillo Apr 21 '16 at 15:05
  • 2
    @GermánCarrillo - Apologies, I should edit the question to make this clear. Specifically layers within Sub_Group_1A. If you can post an answer showing a more cleaner piece of code than what I posted, I will happily accept it :) – Joseph Apr 21 '16 at 15:07
  • Just asked because the other option I mentioned in the comment is also interesting, but it's a different one. – Germán Carrillo Apr 21 '16 at 15:16
  • 1
    @GermánCarrillo - Is that by any chance related to this post: PyQGIS: How to get the list of valid layers in TOC?? – Joseph Apr 21 '16 at 15:19
  • 2
    Exactly! I didnt't remember that answer :D – Germán Carrillo Apr 21 '16 at 15:39
3

There is a handy option in the QgsLayerTreeGroup class that you can use: findGroup. It traverses the whole tree. So, in your case, this would be enough:

root = QgsProject.instance().layerTreeRoot()
subGroup1A = root.findGroup('Sub_Group_1A')
for child in subGroup1A.children():
    if isinstance(child, QgsLayerTreeLayer):
        child.layerName()
  • 2
    Fantastic stuff! Many thanks =). I noticed this was briefly mentioned in the first source referenced, guess I overlooked it. – Joseph Apr 21 '16 at 15:15
1

Eugh, I guess one method is to:

  1. Search the children of Main_group, then
  2. Search the children of Sub_Group_1 and then
  3. Search the children of Sub_Group_1A.

Basically repeating the loops, I'm hoping there is a much nicer and efficient way of accessing those layers but for now, here's the code I used:

root = QgsProject.instance().layerTreeRoot()
for child in root.children():
    if isinstance(child, QgsLayerTreeGroup):
        if child.name() == "Main_Group":
            for subChild in child.children():
                if isinstance(subChild, QgsLayerTreeGroup):
                    if subChild.name() == 'Sub_Group_1':
                        if isinstance(subChild, QgsLayerTreeGroup):
                            for sub_subChild in subChild.children():
                                if isinstance(sub_subChild, QgsLayerTreeGroup):
                                    if sub_subChild.name() == 'Sub_Group_1A':
                                        for layers in sub_subChild.children():
                                            print layers.layerName()

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.