Select to view content in your preferred language

How to select layer from table of contents programmatically?

3450
9
08-20-2012 02:09 PM
sarbajitgurung
Deactivated User
I need to select second layer from table of contents of an active data frame and then export that layer to a new feature class.

import arcpy

# Point to current map document
mxd = arcpy.mapping.MapDocument("CURRENT")

# Now how do I select the second layer from table of contents of current map document?

# Once I figure that out I can then export the layer by using "arcpy.CopyFeatures_management()"


Please help.
Tags (2)
0 Kudos
9 Replies
KenCarrier
Deactivated User
I don't believe the Layer object supports indexing but here is another method

1. Find out the what the data source is for the layer

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
     print lyr.dataSource


2. Next I would set a conditional statement

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
     if lyr.dataSource == #place the results from the previous script here
     # Do your export here.


This method in my opinion allows for more flexibility because even if the name of the layer changes in the TOC, the datasource is less likely to change especially if the source is within a gdb.
0 Kudos
sarbajitgurung
Deactivated User
Thanks Ken, I've followed your code but I've been unable to run the code. Here's the code:

import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.dataSource == r"C:\temp\test.shp"
    arcpy.CopyFeatures_management(lyr, r"C:\temp\output\testout.shp")


Any suggestions?
0 Kudos
KenCarrier
Deactivated User
I got this to work on my data with a local mxd.

mxd = arcpy.mapping.MapDocument("CURRENT") 
lyrs = arcpy.mapping.ListLayers(mxd)

for lyr in lyrs:
     if lyr.dataSource == r"C:\Temp\TestTopology.gdb\TaxParcels\parcels":
         print "True"
     else:
         print "False"


Hope this helps, it returns True for me. Are you running this in ArcMap or as a standalone script? I ran mine from within the mxd. It will just need some tweaking if you are running as a standalone.
0 Kudos
JasonMiller
Deactivated User
Syntax was incorrect... try this...

import arcpy
arcpy.env.overwriteOutput = True

# mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")
mxd = arcpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.dataSource == r"C:\temp\test.shp":
        arcpy.CopyFeatures_management(lyr, r"C:\temp\output\testout.shp")
    else:
        print lyr.dataSource + " not exported..."


NOTE:  You need to make sure that the "output" directory is already created... it will not automatically create it...

Also note that the "else:" statement is not really needed, but it might help you troubleshoot the path name...

Jason
0 Kudos
KenCarrier
Deactivated User
Jason,

good catch

I did not see he forgot the : after the if statement.
0 Kudos
sarbajitgurung
Deactivated User
Thanks Jason and Ken, that was a good catch indeed as my tiring eyes missed that colon. It runs well when I run it from Python window in ArcGIS but it crashes when I run it in PythonWin. The only thing I've changed in PythonWin is:

#mxd = arcpy.mapping.MapDocument("CURRENT")
mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")


i.e., instead of referring to CURRENT map document, I've used the path as I need to automate this task. Any suggestions?
0 Kudos
JasonMiller
Deactivated User
First, make sure that your C:\temp\output\  directory already exists, because it will not be created automatically...

Second, I edited my previous post to include the line   arcpy.env.overwriteOutput = True   which can go just after your import statement...  It will make so that no error is thrown when you try to overwrite an existing file... What probably happened is that it ran fine the first time (in ArcGIS) because your output folder was empty, but when you ran it again (in PythonWin) the shapefile already existed..


If neither of those is of any help, then please post your error code...

Jason
0 Kudos
sarbajitgurung
Deactivated User
Thanks Jason. I know the output directory won't be created automatically, so I created it before running the script. I'm running the following script on PythonWin. It doesn't give me any error, it just crashes when I run this script. Is this a common problem with PythonWin? It works when I run it from IDLE in ArcGIS.

import arcpy
arcpy.env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.dataSource == r"C:\temp\test.shp":
        arcpy.CopyFeatures_management(lyr, r"C:\temp\output")
    else:
        print lyr.dataSource + "Not exported!"
0 Kudos
sarbajitgurung
Deactivated User
I would also prefer not to hard code the layer path, since I have a shapefile joined with a table in the table of contents and that is the shapefile I want to export, not the original shapefile without join. In ArcObject, we could use pMap.Layer(i) where i (0,1,2,3 etc.) refers to the layers' order in the table of contents. Here's the concept of the code, although I couldn't get it to run.

import arcpy
arcpy.env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")

# export the second layer in the table of contents

for lyr(1) in arcpy.mapping.ListLayers(mxd):
        arcpy.CopyFeatures_management(lyr(1), r"R:\Corp Data Mgt\SGurung\temp\output")
0 Kudos