Select to view content in your preferred language

How to tell if a layer is in a group layer

3525
4
Jump to solution
05-01-2014 05:57 AM
Zeke
by
Regular Contributor III
Is there a way to tell if a layer is in a group layer? I was writing out the datasource for layers in a map, which worked fine, but can't tell if a layer is in a group or not. Can test whether a layer is a group layer or a feature class. Some layers are in group layers, some not. Some in group layers in group layers. This isn't a big deal, but would be nice to format the output in a directory tree style. Thanks.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor
 for lyr in arcpy.mapping.ListLayers(mxd):    if lyr.isGroupLayer:         #Then lyr is a Group Layer          for sublyr in lyr:              #now sublyr is an item in the group 



or something to this effect. 

(above is pulled from existing tool that is just to show a general idea and will not work exactly for your needs)

View solution in original post

0 Kudos
4 Replies
MathewCoyle
Frequent Contributor
I don't know of an explicit method of testing if it is a group layer or not, but you can test if it supports datasetname which I believe only would apply to group layers or basemap layers.

layers = arcpy.mapping.ListLayers(mxd)
for lyr in layers:
    if lyr.supports('datasetname'):
        print('probably an fc layer')
    else:
        print('probably some other kind of layer')


Edit: Ah I think I see your issue now. Try This.

layers = arcpy.mapping.ListLayers(mxd)
for lyr in layers:

    if lyr.isGroupLayer:
        group_lyr = lyr.name
        print('This is a group layer')
    elif lyr.longName.split(os.sep)[0] == group_lyr:
        print('lyr is in group layer')



Edit Edit: James has a better method.
0 Kudos
JamesCrandall
MVP Frequent Contributor
 for lyr in arcpy.mapping.ListLayers(mxd):    if lyr.isGroupLayer:         #Then lyr is a Group Layer          for sublyr in lyr:              #now sublyr is an item in the group 



or something to this effect. 

(above is pulled from existing tool that is just to show a general idea and will not work exactly for your needs)
0 Kudos
Zeke
by
Regular Contributor III
Thanks both!
0 Kudos
T__WayneWhitley
Frequent Contributor
Very interesting - I was tripped up by GroupLayers with making a value parameter list using the ToolValidator.
James code almost works, gave me an idea anyway - the think is I needed something to keep 'drilling in', so to speak...what's that called, recursion?

Anyway, so I ended up doing this (below) with a list comprehension that works whether the layer is part of a group or not:
lyrList = sorted(set([os.path.basename(str(lyr)) for lyr in arcpy.mapping.ListLayers(mxd) if not lyr.isGroupLayer]))


...took some trial and error, since I'm no expert with list comprehensions, but it was sure interesting!  Moreover it fixed this problem:
http://forums.arcgis.com/threads/108553-Want-to-run-my-Python-script-as-a-script-tool-in-a-custom-to...

Thanks guys...for helping me track this down.
Wayne
0 Kudos