Select to view content in your preferred language

UpdateLayer symbology

1114
2
06-25-2011 11:05 PM
jenniferstarbuck
Deactivated User
I am trying to use  source_layers to update layers in a different mxd. The layers I am wanting to update are Rivers_1, Rivers_2, Rivers_3, Roads_1, Roads_2, Roads_3, and Buildings_1, Buildings_2, Buildings_3. The previous layers are in the mxd called final project.  The source layers I am using for the update are River, Roads, Buildings, these layers are from the source.gdb I have included the following script I used and the error I am getting. What I am trying to do is use the smybology from the source layers to replace the symbology for the layers in the final project mxd.

import arcpy
mxd = arcpy.mapping.MapDocument (r"D:\GIS_6340_Customization\starbuck.j\Final Project\final project.mxd")
df = arcpy.mapping.ListDataFrames (mxd, "final project")
updateLayer = arcpy.mapping.ListLayers (mxd, "River_1", df) 
updateLayer = arcpy.mapping.ListLayers (mxd, "River_2", df) 
updateLayer = arcpy.mapping.ListLayers (mxd, "River_3", df) 
updateLayer = arcpy.mapping.ListLayers (mxd, "Roads_1", df) 
updateLayer = arcpy.mapping.ListLayers (mxd, "Roads_2", df) 
updateLayer = arcpy.mapping.ListLayers (mxd, "Buildings_1", df) 
updateLauer = arcpy.mapping.ListLayers (mxd, "Buildings_2", df) 
updateLayer = arcpy.mapping.ListLayers (mxd, "Buildings_3", df)
sourceLayer = arcpy.mapping.Layer (r"D:\GIS_6340_Customization\starbuck.j\Final Project\Source.mxd\River.lyr")
sourcrLayer = arcpy.mapping.Layer (r"D:\GIS_6340_Customization\starbuck.j\Final Project\Source.mxd\Road.lyr")
sourceLayer = arcpy.mapping.Layer (r"D:\GIS_6340_Customization\starbuck.j\Final Project\Source.mxd\Buildings.lyr")
arcpy.mapping.UpdateLayer (df, updateLayer, sourceLayer, True)
mxd.saveACopy (r"D:\GIS_6340_Customization\starbuck.j\Final Project\Revised.mxd")

Error:
Traceback (most recent call last):
  File "D:\GIS_6340_Customization\starbuck.j\Final Project\Symbology.py", line 12, in <module>
    sourceLayer = arcpy.mapping.Layer (r"D:\GIS_6340_Customization\starbuck.j\Final Project\Source.mxd\River.lyr")
  File "D:\Desktop10.0\ArcPy\arcpy\arcobjects\mixins.py", line 256, in __init__
    super(LayerMixin, self).__init__(lyrfile)
  File "D:\Desktop10.0\ArcPy\arcpy\arcobjects\_base.py", line 47, in __init__
    for arg in args))
ValueError: Object: CreateObject Layer invalid data source
Tags (2)
0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
In order to update the symbology for layers within MXD using another MXD as the source, you will need to use the 'arcpy.mapping.ListLayers' function rather the the 'arcpy.mapping.Layer'.  Here is an example below on how you can accomplish this:

import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\temp\Final_Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]

lstLayers = arcpy.mapping.ListLayers(mxd, "Rivers*", df)

mxd2 = arcpy.mapping.MapDocument(r"C:\temp\Source.mxd")
df2 = arcpy.mapping.ListDataFrames(mxd2, "*")[0]

for layer in lstLayers:
    updateLayer = layer
    sourceLayer = arcpy.mapping.ListLayers(mxd2, "River", df2)[0]
    arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)
      
mxd.save()

print "Updated symbology successfully"

del mxd


You can copy/paste the syntax above to update the Roads and Building layers as well.
0 Kudos
jenniferstarbuck
Deactivated User
Thanks for the help I was able to get it to work.
0 Kudos