Select to view content in your preferred language

Listing layers in a group

14539
11
06-21-2016 08:27 PM
AndrewGehlot
New Contributor II

Hello.

I am trying to use arcpy to list all of the layers in a group to apply symbology to all of them at once. When I use the arcpy.mapping.ListLayers(mxd, "Group Layer", df), it only returns the name of the group layer and not the layers within the group, so I am unable to iterate through them. I've tried several different queries, but all I seem to get it to do is list every single layer in the document (not what I want) or just the group layer name (also not what I want).

Does anyone know how to specify it to list all of the layers in the group? The ArcGIS help tells me it should be able to do that, listing first the name of the group layer followed by each layer within the group, but it is not working for me.

Tags (2)
11 Replies
DanPatterson_Retired
MVP Emeritus

Did you ever throw in the print statements to see what was being returned at each step?

Updating group layer symbology in arcpy

start with little things, like moving the data frame to the top of the table of contents and drop the wildcard or temporarily remove anything other than the group layer from there as well to see what is returned.  Once you get a basic case documented, then you can add other layers back in, and then try your wildcards and indexing

0 Kudos
AndrewGehlot
New Contributor II

Yes, I did. What I was getting returned was just the name of the group layer, and not the layers within it. I was able to make a workaround by using a wildcard because all of my layers began with the same phrase, but I won't be able to make that solution more general, as I'm working with far more data tiles that have different names.

0 Kudos
FC_Basson
MVP Regular Contributor

Try this function:

def listGroupLayer(glayername):
    mxd = arcpy.mapping.MapDocument('CURRENT')
    df = mxd.activeDataFrame
    layers = arcpy.mapping.ListLayers(df)
    for l in layers:
        if l.isGroupLayer and l.name == glayername:
            print "Layers in " + glayername + ":"
            glayers = arcpy.mapping.ListLayers(l)
            for gl in glayers:
                print gl.name
                # apply symbology
                arcpy.ApplySymbologyFromLayer_management(gl,r"C:\GIS\lyrfiles\mylayersymbology.lyr")

# use as 
listGroupLayer("MyGroupLayer")
AndrewGehlot
New Contributor II

Thanks! I'm not in the office today, so I can't test it, but I will as soon as I get back. The ApplySymbologyFromLayer_management tool, however, doesn't work for me because it only applies symbology if there is a polygon within the feature class present; however I'm trying to apply all symbology for when I need to add polygons, so I'm using the UpdateLayer function.

I should also mention that I'm brand new to Python and any programming language, so some of this stuff is a bit over my head.

0 Kudos
Oliver_Burdekin
Occasional Contributor II

I've been trying to get something similar working with Arcpro. I've tried the following:

p = arcpy.mp.ArcGISProject("CURRENT")

m = p.listMaps()[0]

for lyr in m.listLayers():
    if lyr.isGroupLayer:
        glayers = m.listLayers(lyr)
        for gl in glayers:
            print(gl.name)

I only have one group layer for testing (the group layer has 5 layers in it) and there are 2 non-group layers.

The output is just printing the name of the initial group layer rather than those layers within the group. I suspect it's due to the changes in arcpy from arcMap to arcPro, specifically between ListLayers and listLayers

0 Kudos
FC_Basson
MVP Regular Contributor

I think your second listLayers is implemented incorrectly.

This works for me in ArcPro:

proj = arcpy.mp.ArcGISProject('CURRENT')
m = proj.listMaps()[0]
ml = m.listLayers()
for l in ml:
   if l.isGroupLayer:
      print(l)
      lyrs = l.listLayers()
      for lyr in lyrs:
         print(lyr)‍‍‍‍‍‍‍‍‍
Oliver_Burdekin
Occasional Contributor II

Thanks you're right! Got it running now.

0 Kudos
SutanMufti
New Contributor

thanks, this works for me in arcpro too

0 Kudos
CatherineHall678
New Contributor III

Thank you! Got it running in 3.0 using Notebook--- 😁

0 Kudos