acharenko

Script to update multiple layers with layer files

Discussion created by acharenko on Jan 3, 2011
Latest reply on Jan 4, 2011 by clm42
I am looking to modify this script to update multiple layers at the same time with layer files.

[INDENT]import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0]
updateLayer = arcpy.mapping.ListLayers(mxd, "Rivers", df)[0]
sourceLayer = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr")
arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd, sourceLayer[/INDENT]

For example, I have three layers (roads, rivers, buildings) that I would like updated with my layers files (roads.lyr, rivers.lyr and buildings.lyr). 

Here's what I wrote:

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

# loop through layers to get layer names, replace layer file
fcList = [roads, rivers, buildings]
for fc in fcList:
    fc = arcpy.mapping.ListLayers(mxd, fc,df)[0]
    updateLayer = arcpy.mapping.ListLayers(mxd,fc,df)[0]
    sourceLayer = arcpy.mapping.Layer(r"C:\LayerFiles\fc.lyr")
    arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)

# refresh view and table of contents, save changes to mxd
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
mxd.save()
del sourceLayer, mxd
[/INDENT]

It's the underlined line that I am having trouble with.  What I want the script to do is look at the input layers in the fcList and update with the appropriate layer file with the same name from my computer.
Any help with this would be appreciated.

Outcomes