Is layer in grouplayer

1402
5
06-08-2011 12:58 AM
HenkZwols
Occasional Contributor
Hello all,

Is there a way in Python/Arcpy to find out if a featurelayer is in a grouplayer?

Greetings, Henk
Tags (2)
0 Kudos
5 Replies
AndrewChapkowski
Esri Regular Contributor
There is a layer property called isGroupLayer which returns a boolean value (true/false).

    path = r"C:\temp\ds.mxd"
    mxd = arcpy.mapping.MapDocument(path)
    df = arcpy.mapping.ListDataFrames(mxd)
    for frame in df:
        for layer in arcpy.mapping.ListLayers(mxd, "*",frame):
            print layer.name
            print "isFeatureLayer - " + str(layer.isFeatureLayer)
            print "isGroupLayer - " + str(layer.isGroupLayer)
    del mxd
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Here is an example on how to find out if a map layer is within a group layer:

import arcpy
from arcpy import env
from arcpy import mapping

env.workspace = r"C:\TEMP\PYTHON"

mxd = mapping.MapDocument(r"C:\TEMP\PYTHON\Airports.mxd")

dfs = mapping.ListDataFrames(mxd)

for df in dfs:
    lyrs = mapping.ListLayers(mxd, "", df)
    for lyr in lyrs:
        if lyr.isGroupLayer == True:
            print lyr.name

del mxd
0 Kudos
HenkZwols
Occasional Contributor
Thanks, that works. I suggest to rename this property to isInsideGroupLayer :confused:

Is there a way to findout what the name of the grouplayer is?

Greetings, Henk
0 Kudos
JakeSkinner
Esri Esteemed Contributor
The below code will probably give you more of what you are looking for.  It will print the Group Layer and the map layer. 

Ex:  Townships of PA\Delaware County

'Townships of PA' is the Group Layer, and 'Delaware County' is the Map Layer.

import arcpy
from arcpy import env
from arcpy import mapping

env.workspace = r"C:\TEMP\PYTHON"

mxd = mapping.MapDocument(r"C:\TEMP\PYTHON\Airports.mxd")

dfs = mapping.ListDataFrames(mxd)

list = []

for df in dfs:
    lyrs = mapping.ListLayers(mxd, "", df)
    for lyr in lyrs:
        list.append(lyr.longName)

for s in list:
    if "\\" in s:
        print s

del mxd
0 Kudos
HenkZwols
Occasional Contributor
Thanks for your support

Greetings, Henk
0 Kudos