4

What the code does successfully so far, is gets a list of the layers in the first data frame of the current map. What it doesn't succeed in doing is, joining the layer names to the gdb location using os.path.join(), so I can then do a clip against an existing feature class.

I think I need to convert the lyr names to a string before I can use it in a Clip_analysis, how do I do that?

Here is what I've got so far:

import arcpy
import arcpy.mapping
import os
#Set the current map
mxd = arcpy.mapping.MapDocument("CURRENT")
# Set the dataframe
df = arcpy.mapping.ListDataFrames(mxd)[0]
# Get list of Layers from toc
layers = arcpy.mapping.ListLayers(mxd,"",df)
#Set the output workspace
outWorkspace = r'C:\Users\jsommerville\Documents\ArcGIS\California\ProjectData.gdb'
for lyr in layers: 
 outFeatureClass = os.path.join(outWorkspace, lyr) 
 arcpy.Clip_analysis(lyr, "extent", outFeatureClass)

The error message I'm getting: 'Layer' object is not subscriptable

Jay Guarneri
2,1051 gold badge16 silver badges33 bronze badges
asked Mar 28, 2013 at 20:10

1 Answer 1

6

You are correct about needing to convert the layer name to a string. You can get the layer name as a string by calling lyr.name. The reason for this is each layer is a layer object, which won't work in os.path.join(). However, the object has a 'name' property that you can set or pass to other string functions. To use it, call os.path.join(outWorkspace,lyr.name).

answered Mar 28, 2013 at 20:18
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.