Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

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).

share|improve this answer
1  
Thanks Jay, you rock! –  jksville Mar 28 '13 at 20:26
    
If this answer worked for you, please mark it as the accepted answer. Thanks! –  blord-castillo Mar 29 '13 at 17:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.