Select to view content in your preferred language

Script to update multiple layers with layer files

3316
1
01-03-2011 05:01 AM
AllisonCharenko
Emerging Contributor
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.
Tags (2)
0 Kudos
1 Reply
ChrisMathers
Deactivated User
Try this. Itereate over a list of lists. Each sublist contains the feature class name and the path to the layer for that feature class. Use the two values with normal list indexing. You can do loops this way that take many inputs that change for each iteration.

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,r'roads.lyr path'], [rivers,r'rivers.lyr path'], [buildings,r'buildings.lyr path']]
for fc in fcList:
    fc = arcpy.mapping.ListLayers(mxd, fc[0],df)[0]
    updateLayer = arcpy.mapping.ListLayers(mxd,fc,df)[0]
    sourceLayer = arcpy.mapping.Layer(fc[1])
    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


You could also use a dictionary and .iteritems(). Which ever makes you happy. Neither is better than the other in this case.

fcDict = {roads:r'roads.lyr path', rivers:r'rivers.lyr path', buildings:r'buildings.lyr path'}
for fc,lyr in fcDict.iteritems():
    fc = arcpy.mapping.ListLayers(mxd, fc,df)[0]
    updateLayer = arcpy.mapping.ListLayers(mxd,fc,df)[0]
    sourceLayer = arcpy.mapping.Layer(lyr)
    arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)
0 Kudos