arcpy: get access to layer that's in group layer?

2365
15
09-21-2018 08:16 AM
helenchu
Occasional Contributor II

Hi

I created a model builder and scripted it out to python.  My issue is it only works if the layer is outside of group layer.  How can I modify it to works in both inside and outside of group layer?  Thank you very much for your help.  

import arcpy,sys, os
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\GIS\MyGeodatabase.gdb"
mxd = arcpy.mapping.MapDocument("CURRENT")
layerName = 'street'
lyr = arcpy.mapping.ListLayers(mxd, layerName)[0]
fc = arcpy.GetParameterAsText(0)

Street = "Street"

selCount = 0
if lyr.getSelectionSet(): # If there are selected features
selCount = len(lyr.getSelectionSet())
arcpy.AddMessage("{0} has {1} features selected.".format(fc, selCount))
try:
# Process: Copy Features
arcpy.CopyFeatures_management(Street , street_CopySelected, "", "0", "0", "0")

0 Kudos
15 Replies
JoshuaBixby
MVP Esteemed Contributor

Saying something "doesn't work" often isn't specific enough for people to offer much feedback.  Does it generate an error when run within a group layer?  If so, what is the error, including the trace back.  Or, does it give unexpected results?  If so, what are you expecting and what are you seeing?

0 Kudos
helenchu
Occasional Contributor II

It just doesn't find that layerName so the script did nothing.  Thanks.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

When the layer is nested under a group layer, what does the following code give for results:

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    print lyr.longName
helenchu
Occasional Contributor II

TopLayerGroup\Street

I replaced Street = lyr.longName and it still does not work.  It gives my the selection count but not doing the copy features.  Thanks.

My code looks like this:

df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd,"",df): #loop layers
if lyr.name == "Street":
Street= lyr.longName
selCount = 0
selCount = len(lyr.getSelectionSet())
arcpy.AddMessage("{1} features selected.".format(fc, selCount))
try:
# Process: Copy Features
arcpy.CopyFeatures_management(lyr.Street, street_CopySelected, "", "0", "0", "0")
del df

except Exception as e:
print e.args[0]

0 Kudos
RandyBurton
MVP Regular Contributor

Can you post a screenshot of how your layers are organized?

Also, it helps to see formatted code: Code Formatting... the basics++

0 Kudos
helenchu
Occasional Contributor II

0 Kudos
helenchu
Occasional Contributor II
# Import arcpy module
import arcpy,sys, os
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\GIS\MyGeodatabase.mdb"
mxd = arcpy.mapping.MapDocument("CURRENT")
layerName = 'Street'
lyr = arcpy.mapping.ListLayers(mxd, layerName)[0]
fc = arcpy.GetParameterAsText(0)



Street = "Street"
street_CopySelected = "street_CopySelected"


df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd,"",df):     #loop layers

          if lyr.name == "Street":
            arcpy.AddMessage("name: " + lyr.longName)
            layerName = lyr.longName
            Street = lyr.longName
            selCount = len(lyr.getSelectionSet())
            arcpy.AddMessage("{1} features selected.".format(fc, selCount))
            try:
                # Process: Copy Features
                arcpy.CopyFeatures_management(Street, street_CopySelected, "", "0", "0", "0")
                del df

            except Exception as e:
                print e.args[0]
0 Kudos
RandyBurton
MVP Regular Contributor

Thanks for formatting.  It helps to see the indentation.

Regarding line 23:

selCount = len(lyr.getSelectionSet())

If lyr.getSelectionSet() == None, then len will return an error.  You might want to check first:

if lyr.getSelectionSet():
    selCount = len(lyr.getSelectionSet())
    # do other things with the layer selection‍

Since you are using MapDocument("CURRENT"), are you planning to only use the tool inside ArcMap?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Python is case sensitive, i.e., "Street" != "street".  In your original code, you are using both.  Trying being consistent with your case.