The layer to be updated (ul) does not get applied with the style from the source layer (sl) and the code executes without throwing any error.

import os
import csv
import arcpy
#Set home geodatabase.
arcpy.env.workspace =  "\ArcGIS\Python_test"
#Set shapefile path to variable.
PointFile = "\ArcGIS\Python_test\Well_data.csv"
#Import well location shapefile template CSV.
#Make well location event layer.
arcpy.MakeXYEventLayer_management("Well_data_View", "Easting", "Northing", 'Well_plot_EventLayer', 'WGS 1984 UTM Zone 43N')
#Convert temporary event layer to feature class for saving.
arcpy.FeatureClassToFeatureClass_conversion("Well_plot_EventLayer", "\ArcGIS\Python_test", 'Well_plot')
#Delete event layer.
#Reference the map document (.mxd).
mxd = arcpy.mapping.MapDocument("\ArcGIS\Python_test\Python_workspace.mxd")
#Reference dataframe.
df = arcpy.mapping.ListDataFrames(mxd)[0]
#Reference layer to be updated and reference layer.
ul = arcpy.mapping.Layer("\ArcGIS\Python_test\Well_plot.lyr")
sl = arcpy.mapping.Layer("\ArcGIS\Python_test\Well_SR.lyr")
#Update the layer. THIS DOES NOT WORK!!
arcpy.mapping.UpdateLayer(df, ul, sl)
  • 1
    You appear not to have saved your MXD. – PolyGeo Dec 14 '19 at 20:04
  • I have saved my .mxd hence I am able to reference its path on my hard disk to a variable in my code. – Toxic Blast Dec 16 '19 at 2:32
  • 1
    But you have not saved it at the end of your code which means that when you check it the state of your MXD is as before you ran it. – PolyGeo Dec 16 '19 at 3:30
  • ie, add mxd.save() to the end of the script. – Son of a Beach Dec 16 '19 at 3:34
  • Additionally, UpdateLayer() is for layers in a map document, not layers in a layer file. You appear to be updating a layer from a layer file, not a layer in the map. Should ul actually be defined something like, ul = arcpy.mapping.ListLayers(mxd, "Well Plot", df)[0] ? (You can save the map layer back to a .lyr file afterwards if that is what you actually want.) See: desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/… – Son of a Beach Dec 16 '19 at 3:44

Are you attempting to change a layer in a .mxd map document or are you attempting to update a layer in a .lyr file?

In either case there are two problems with your code:

  1. The script doesn't save the changes (as commented by @PolyGeo). You need to either save the map document or overwrite the layer file.

  2. UpdateLayer() is for layers in a map document, not layers in a layer file. You appear to be updating a layer from a layer file, not a layer in the map. ul should be defined something more like:

ul = arcpy.mapping.ListLayers(mxd, "Well Plot", df)[0]

(Change "Well Plot" to whatever the layer is actually named in the MXD.)

See: arcpy.mapping.UpdateLayer()

So the last few lines of your code should be something more like:

mxd = arcpy.mapping.MapDocument("\ArcGIS\Python_test\Python_workspace.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
ul = arcpy.mapping.ListLayers(mxd, "Well Plot", df)[0]
sl = arcpy.mapping.Layer("\ArcGIS\Python_test\Well_SR.lyr")
arcpy.mapping.UpdateLayer(df, ul, sl)

( If you want to actually overwrite a .lyr file, and not save the mxd, then don't add the mxd.save(), but instead you can save the map layer back to a .lyr file using: arcpy.SaveToLayerFile_management() )

  • What I have done is created and saved my mxd (Python_test.mxd), then, through code created a point shapefile (Well_plot) from the .csv file I have on my disk. This Well_plot shapefile is a layer created in the map document and not saved/exported; I have a .lyr file in the project home folder (Well_SR) which I want to use as a style template for Well_plot. Well_SR.lyr has not been added to the map and is present only in the project home folder. So in short, I ain't overwriting a layer file, just re-formatting a map layer (Well_plot). I hope this clarifies your doubts @Son of a Beach. – Toxic Blast Dec 17 '19 at 4:48
  • The line: ul = arcpy.mapping.Layer("\ArcGIS\Python_test\Well_plot.lyr") was added initially by me because after creating Well_plot I exported it as a .lyr file manually from the TOC to my home folder because I felt the attributes for UpdateLayer need to be layer files specifically. But then I ditched that path because it seemed unnecessary after going though the updates you sugested to my code. – Toxic Blast Dec 17 '19 at 4:55
  • I hope it has helped? – Son of a Beach Dec 17 '19 at 4:58
  • I defined variable 'ul' as ul = arcpy.mapping.ListLayers(mxd, "", df)[0] because it is present as a layer in the map document only. I get the following error - Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files (x86)\arcgis\desktop10.7\arcpy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.7\arcpy\arcpy\mapping.py", line 1889, in UpdateLayer assert isinstance(source_layer, Layer) AssertionError – Toxic Blast Dec 21 '19 at 12:40
  • Looks like some problem with the source layer, or something else in the UpdateLayer call. You should post a new question for this. – Son of a Beach Dec 21 '19 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.