Select to view content in your preferred language

'Layer' object is unsubscriptable

830
3
02-17-2012 08:58 AM
ZackBartlett
Regular Contributor
I am attempting to list all of the shapefiles in the "Layers" dataframe of my Table of Contents. I've also created a list of layer files (.lyr) in a specified folder. My goal is to assign a layer file in this folder to a shapefile in my TOC based on common characters between
the two. I've set up both of these lists in my script. One block of code generates and prints the TOC shapefiles, another block generates the layer files in the specified folder.

For example:

An entry in layer file list looks like this:

N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles\031g04_9_0_LX_2420009_1.lyr

An entry in a TOC shapefile list looks like this:

031g04_9_0_LX_2420009_1

This I've managed to do without error.

What I want to do next is take each shp in the TOC and match it to it's corresponding layer file in the folder.

I was hoping to do that by comparing a subsection of each element in each list.

For example, I would extract from the layer the "_9_0_LX_2420009_1" [59:-4] text string. If it equals same subsection of the shp TOC list, "_9_0_LX_2420009_1" [6:], I would apply the corresponding layer file to the shapefile. My code is pasted below.


# Author: Zachary Bartlett
# February 15, 2012
# Location: N:\DOCUMENTATION\PYTHON\Canvec_Symbolization\canvec_autosym.py

# This tool searches the ArcMap TOC for Canvec shapefiles and symbolizes them
# based on layer files in:
#   N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles

import arcpy, glob, os

arcpy.env.Workspace = r"N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles"
arcpy.env.OverwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

layer_file_list = glob.glob(str(arcpy.env.Workspace) + "\*.lyr")

arcpy.AddMessage("Layer File List: ")
for layer in layer_file_list:
    arcpy.AddMessage(layer)
print layer_file_list
arcpy.AddMessage("-------")

TOC_shp_list = arcpy.mapping.ListLayers(mxd, "", dataFrame)

arcpy.AddMessage("Table of Contents Shapefile list: ")
for shp in TOC_shp_list:
    arcpy.AddMessage(shp)
print TOC_shp_list
arcpy.AddMessage("-------")

for shp in TOC_shp_list:
    shp_ext = shp[6:]
    for lyr in layer_file_list:
        lyr_ext = lyr[59:-4]
        print lyr_ext
        if lyr_ext == shp_ext:
            ApplySymbologyFromLayer_management(shp, lyr)
        del lyr, lyr_ext
    del shp, shp_ext

arcpy.RefreshTOC()
arcpy.RefreshActiveView()


When I run this script I get the following error:

<type 'exceptions.TypeError'>: 'Layer' object is unsubscriptable
Failed to execute (canvecautosym).

I even tried removing some of the problematic code and running the following (I simplified the for loop):

# Author: Zachary Bartlett
# February 15, 2012
# Location: N:\DOCUMENTATION\PYTHON\Canvec_Symbolization\canvec_autosym.py

# This tool searches the ArcMap TOC for Canvec shapefiles and symbolizes them
# based on layer files in:
#   N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles

import arcpy, glob, os

arcpy.env.Workspace = r"N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles"
arcpy.env.OverwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

layer_file_list = glob.glob(str(arcpy.env.Workspace) + "\*.lyr")

arcpy.AddMessage("Layer File List: ")
for layer in layer_file_list:
    arcpy.AddMessage(layer)
print layer_file_list
arcpy.AddMessage("-------")

TOC_shp_list = arcpy.mapping.ListLayers(mxd, "", dataFrame)

arcpy.AddMessage("Table of Contents Shapefile list: ")
for shp in TOC_shp_list:
    arcpy.AddMessage(shp)
print TOC_shp_list
arcpy.AddMessage("-------")

for shp in TOC_shp_list:
    shp_ext = shp[6:]
    arcpy.AddMessage(shp_ext)
    print shp_ext
    del shp, shp_ext

arcpy.RefreshTOC()
arcpy.RefreshActiveView()


When I run this script I get the same error as before.

Everything up to the first for loop runs OK.

Any ideas?
Tags (2)
0 Kudos
3 Replies
RaphaelR
Deactivated User
hey Zack,

didn´t look at your code in detail, but my guess is you´re trying to access the layer object, not it´s name (which you need to compare the "subsections").
the help page has a nice overview and some code samples as well:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/
0 Kudos
JeffBarrette
Esri Regular Contributor
I suspect the problem is occuring with your "for shp in TOC_shp_list" block.  Your second line is stripping away characters from a layer object, not a string.  You could change it to something like

shp_ext = shp.name[6:]

Jeff
0 Kudos
ZackBartlett
Regular Contributor
Thanks Jeffrey! It worked perfectly. 

I suspected it must be some issue of treating a list item as a string, I just didn't know any method to convert it to a string.

Thanks again.
0 Kudos