Friday, July 1, 2011

ArcPy - Looking inside Group Layers

A group layer contains other layers. Group layers help organize related kinds of layers in a map and can be used to define advanced drawing options. For example, suppose you have two layers on a map representing railroads and highways. You could group these layers together and name the resulting layer Transportation Networks. If you need to, you can even create nested group layers (groups of group layers). (source: esri)

Now, let's say you have to do some analysis on layers inside the group layers, you can loop the layers as follows:


import arcpy
mxdPath = r"c:\temp\mapDoc.mxd"
mxd = arcpy.mapping.MapDocument(mxdPath)
layers = arcpy.mapping.ListLayers()
for layer in layers:
if layer.isGroupLayer:
for subLayer in layer:
print "This layer is in a group layer: " + str(subLayer.name)



This will print a list of layer contained in a groupLayer.

4 comments:

John said...
This comment has been removed by the author.
John said...

Can you do that using python in ArcGIS 9.3.1 (no ArcPy)? I have a python script that does a select&export on layers in the Table of contents, problem is it doesn't recognize Group layer as feature layer and can't read the feature layers inside the group layers. If you have an idea how it can be done, I'd love to hear it.

John

Andrew said...

At 931, the only way I know how to determine between a group layer and a feature layer is to create is to use ArcObjects. Version 10 has a property on the layer class called isGrouplayer which returns a boolean value.
If possible, I would upgrade to 10 to get this function or write a custom .NET GP tool to get the functionality you want.

Anonymous said...

i sometimes had trouble accessing layers that are inside a grouplayer.
it´s the backspace character that´s causing it.

lyrlist = ["New\layer1", "New\New2\layer2"]

layers = [l.split("\\")[-1] for l in lyrList]

with that little list comprehension you just get the name of the layer...