Group layers

1786
14
08-21-2013 01:30 PM
AlexGole1
New Contributor II
Is there a script that allows you to cycle through multiple group layers and turn on and off group layers one by one?
Thanks,
Alex
0 Kudos
14 Replies
JeffBarrette
Esri Regular Contributor
You can iterate through all layers and test for Layer.isGroupLayer.  If it returns false, set visible = False.

Jeff
0 Kudos
AlexGole1
New Contributor II
I tried using the following code to iterate through group layers but it seems like "isGroupLayer" is not an attribute (error message). I am a bit confused about this one. I am just trying to turn on a layer, export it as PDF.l

Thanks,
Alex


import arcpy, os
myPath    = r"K:\Working\Alex_Gole\Try\\"
arcpy.env.workspace = myPath
myMap = arcpy.mapping.MapDocument(myPath + "MyMap.mxd")
layer = arcpy.mapping.ListLayers(myMap)
vislayer = layer.isGroupLayer
df = arcpy.mapping.ListDataFrames(myMap)[0]
# Clear visibility of all layers except for the community areas layer
for lyr in vislayer:
    print "Turning on Layer " + lyr.name
    lyr.visible = True
else:
    print "Turning off layer " + lyr.name
    lyr.visible = False
#Export to PDF
    print "exporting map"
    arcpy.mapping.ExportToPDF(myMap, myPath + str(vislayer.name) + ".pdf")
0 Kudos
AlexGole1
New Contributor II
import arcpy, os, sys
path = os.path.dirname(sys.argv[0])
mxd = arcpy.mapping.MapDocument(path + r"/MyMap.mxd")
outputPath = r"K:\Working\Alex_Gole\Try"
mapLyr = arcpy.mapping.ListLayers(mxd)[0]

for lyr in mapLyr:
    if lyr.isGroupLayer:
        print "Turning on group Layer " + lyr.name
        lyr.visible = True
    else:
        lyr.visible = False

if os.path.exists(path + "layers.pdf"):
    os.remove(path + "layers.pdf")
arcpy.mapping.ExportToPDF(mxd, os.path.join(outputPath, lyr.name + "layers.pdf"))


I am trying with this script but it does  not seem like it returns anything.
Thanks,
Alex
0 Kudos
MichaelVolz
Esteemed Contributor
Can you try removing the

if lyr.isGroupLayer:

and

else:

Then just try printing the lyr.name property to ensure that you are indeed looping through the layers.
0 Kudos
AlexGole1
New Contributor II
import arcpy, os
myPath = r"K:\Working\Alex_Gole\Try\\"
arcpy.env.workspace = myPath
myMap = arcpy.mapping.MapDocument(myPath + "MyMap.mxd")
layer = arcpy.mapping.ListLayers(myMap)
df = arcpy.mapping.ListDataFrames(myMap)[0]
# Clear visibility of all layers except for the community areas layer
for lyr in layer:
    if lyr.isGroupLayer:
        print "Turning on Layer " + lyr.name
        lyr.visible = True
    else:
        print "Turning off layer " + lyr.name
        lyr.visible = False
#Export to PDF
    print "exporting map"
    arcpy.mapping.ExportToPDF(myMap, myPath + str(lyr.name) + ".pdf")


Seems like this work. My only two worries here is that I dont see my legend being updated automatically and I dont see the name of the group layer as the PDF name when exported.

Can you try removing the

if lyr.isGroupLayer:

and

else:

Then just try printing the lyr.name property to ensure that you are indeed looping through the layers.
0 Kudos
JimCousins
MVP Regular Contributor
Alex,
In your first code entry, you alternate the slash and backslash for the map document concatenation.
For the legend update, you may need to do a refresh on the dataframe to show the changes.
For the myPath variable in your last entry, you have an additional "\" at the end.
Regards,
Jim
0 Kudos
MichaelVolz
Esteemed Contributor
Are you looking to export to pdf while looping thru each layer or are you just trying to do an export to pdf for the group layer?
0 Kudos
AlexGole1
New Contributor II
Are you looking to export to pdf while looping thru each layer or are you just trying to do an export to pdf for the group layer?


I am looking to export each group layer as a separate PDF.
0 Kudos
MichaelVolz
Esteemed Contributor
Seems to me that you should put the export to pdf statement in the GroupLayer true portion of the if else clause.  I would think it is currently exporting every layer out to a pdf since the export to pdf statement is outside the if else clause but within the for loop.
0 Kudos