Error with arcpy.mapping.ListLayers - Please Help

4556
8
12-27-2012 05:44 AM
MatthewGerbrandt
New Contributor II
Hello, everyone. I have a layer file that points at a FeatureClass. This FeatureClass periodically receives new data. I need to update the layer file so that it recognizes the new data.

I'm trying to use the code found at: http://resources.arcgis.com/en/help/main/10.1/#/UpdateLayer/00s30000003p000000/ as a starting point but it's erroring out saying "AttributeError: 'Layer' object has no attribute 'symbology'"

Ive been grinding on this for a week now and I'm totally stumped here. The layer file does have symbology set up (see attached screenshot), so I've no idea what the problem is. I'm in desperate need of help and would appreciate any suggestions.

My code is as follows:

import arcpy, os
from arcpy import env

env.workspace = os.getcwd()


mxd = arcpy.mapping.MapDocument(env.workspace + r"\CoastalCurrentsData.mxd")

# Dataframe stuff - perform an arcpy.mapping UpdateLayer (using the above pre-authors layer file)
df = arcpy.mapping.ListDataFrames(mxd, "CoastalCurrentsData")[0]
updateLayer = arcpy.mapping.ListLayers(mxd, "RecentData", df)[0]
sourceLayer = arcpy.mapping.Layer(env.workspace + r"\USWC_1km_WGS_1984.lyr")
arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)

# Refresh the layers unique value symbology so that it is referencing the current data

lyr = arcpy.mapping.ListLayers(mxd, "RecentData", df)[0] # HERE'S WHERE THE ERROR HAPPENS #######

lyr.symbology.addAllValues()

arcpy.RefreshActiveView()
arcpy.RefreshTOC()
Tags (2)
0 Kudos
8 Replies
CarlSunderman
Occasional Contributor
try changing to this
sourceLayer = arcpy.mapping.Layer(env.workspace + "/USWC_1km_WGS_1984.lyr")

I tested your code and it works fine. Also I always try to use / instead of \ in path names
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Matthew,

You can simplify your code if you're updating a layer file in your MXD.  Try the following using the Python window within ArcMap:

import arcpy, os
from arcpy import env
env.workspace = os.getcwd()

mxd = arcpy.mapping.MapDocument(env.workspace + r"\CoastalCurrentsData.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.Layer("RecentData")

lyr.symbology.addAllValues()

arcpy.RefreshActiveView()
arcpy.RefreshTOC()
0 Kudos
MatthewGerbrandt
New Contributor II
Thank you for taking a look at my code. Unfortunately, I could not solve the problem with the code you provided. The code and error are as follows:

import arcpy, os
from arcpy import env

env.workspace = os.getcwd()


mxd = arcpy.mapping.MapDocument(env.workspace + r"\CoastalCurrentsData.mxd")

# Dataframe stuff - perform an arcpy.mapping UpdateLayer (using the above pre-authors layer file)
df = arcpy.mapping.ListDataFrames(mxd, "CoastalCurrentsData")[0]
updateLayer = arcpy.mapping.ListLayers(mxd, "RecentData", df)[0]
sourceLayer = arcpy.mapping.Layer(env.workspace + "/USWC_1km_WGS_1984.lyr")
arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)

# Refresh the layers unique value symbology so that it is referencing the current data

# HERE'S WHERE THE ERROR HAPPENS #######
sourceLayer.symbology.addAllValues()
# AttributeError: 'Layer' object has no attribute 'symbology'

arcpy.RefreshActiveView()
arcpy.RefreshTOC()
0 Kudos
CarlSunderman
Occasional Contributor
Wouldn't this be simpler. I just ran this and it again ran fine with the expected results

import arcpy

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

df = arcpy.mapping.ListDataFrames(mxd, "DataFrame")[0]

# Refresh the layers unique value symbology so that it is referencing the current data
lyr = arcpy.mapping.ListLayers(mxd, "TestingLayer", df)[0] 

lyr.symbology.addAllValues()

arcpy.RefreshActiveView()
arcpy.RefreshTOC()
0 Kudos
MatthewGerbrandt
New Contributor II
Hmmm... It looks like you're running code from within ArcGIS Desktop and I'm trying to run this as a stand-alone script. Perhaps that's the difference. I need this to run as a stand-alone automated script.

I've tried using a variation of your code (as follows) but the error remains. I've also attached a screenshot of the MXD in case that adds any clarity. I really appreciate your help.

import arcpy, os
from arcpy import env

env.workspace = os.getcwd()

mxd = arcpy.mapping.MapDocument(env.workspace + r"\CoastalCurrentsData.mxd")

df = arcpy.mapping.ListDataFrames(mxd, "CoastalCurrentsData")[0]

# Refresh the layers unique value symbology so that it is referencing the current data
lyr = arcpy.mapping.ListLayers(mxd, "RecentData", df)[0] 

lyr.symbology.addAllValues()
# AttributeError: 'Layer' object has no attribute 'symbology'

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

0 Kudos
BrianHebert
New Contributor

Hi Matthew,

Did you ever figure out why you were getting the error: 'Layer' object has no attribute 'symbology'

I'm running into the same problem.

Thanks for any help,

Brian

0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Matthew,

If you created the layer file with Single Symbol symbology, new features should automatically display once the feature class is updated.  You would only need to apply the code if you used Unique Values for the symbology.

You can test this:

1.  Add a feature class to ArcMap (symbology will default to Single Symbol)
2.  Create a layer file
3.  Remove the feature class and add the layer file to the MXD
4.  Save the MXD
5.  Start a new map document and add the feature class
6.  Start an edit session, add a new feature, save edits
7.  Open the previously saved MXD

The new feature should appear.
0 Kudos
JeffBarrette
Esri Regular Contributor
The symbology object only supports a limited number of renderers (e.g., Unique Value, Graduated Color, Graduated Symbol, and Raster Classified).

You are trying to .AddAllValues to a single symbol renderer.  You can't do that.

The help topic from http://resources.arcgis.com/en/help/main/10.1/#/UniqueValuesSymbology/00s30000005s000000/

shows a code sample:

import arcpy
mxd = arcpy.mapping.MapDocument("current")
lyr = arcpy.mapping.ListLayers(mxd, "Population")[0]
if lyr.symbologyType == "UNIQUE_VALUES":
  lyr.symbology.valueField = "SUB_REGION"
  lyr.symbology.addAllValues()
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd


Jeff
0 Kudos