AnsweredAssumed Answered

How do I apply arcpy.mapping.UpdateLayer() to a MapDocument in a background process?

Question asked by sschwindt on Feb 27, 2018

Hi,

 

I am trying to automate the export of several rasters applied to a certain Layout.mxd using the following steps (simplified):

mxd = arcpy.mapping.MapDocument(r'D:\MyFolder\MyLayout.mxd')
df = arcpy.mapping.ListDataFrames(mxd)[0]
source_lyr = arcpy.mapping.ListLayers(mxd, "sym_lyr", df)[0]
target_lyr = arcpy.mapping.ListLayers(mxd, "tar_lyr", df)[0]
arcpy.mapping.UpdateLayer(df, target_lyr, source_lyr)

The target_lyr- Layers are imported rasters and the import works perfectly - the same for the PDF-export directly after this code sample.

The problem is that "UpdateLayer" basically has no effect at all. When I try to run the above code in ArcMap Desktop, it won't work neither unless I replace

mxd = arcpy.mapping.MapDocument(r'D:\MyFolder\MyLayout.mxd')

with

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

However, I can't apply "CURRENT" in my Python script because it is not running in Foreground of the opened "MyLayout.mxd". It is also impossible for me to implement my Python Script as an AddIn of ArcMap because the export-map only is a subroutine of a complex program that needs to output maps.

In addition, I tried the following:

  • Adding "symbology_only = True"
  • Applying arcpy.RefreshActiveView () and arcpy.RefreshTOC on multiple places
  • Several other ways of writing the path in arcpy.mapping.MapDocument(path)
  • Other methods of adding layers, but this seems odd because mxd.saveACopy(copy.mxd) shows that copy.mxd perfectly looks like it should look, only without the desired symbology; but using arcpy.mapping.MapDocument("CURRENT") directly in copy.mxd with the above code sequence will apply the desired symbology!
  • Using arcpy.ApplySymbologyFromLayer_management(target_lyr, source_lyr)

Does anyone have an idea how else I can apply the symbology of source_lyr to target_lyr or how I can define step-by-step the symbology using a defined classified colorjet (the colors are very important in this case)?

 

Otherwise, does anyone know what exactly makes that "CURRENT" works but the "ABSOLUTE_PATH" does not, resp., how I can fix the issue?

 

(I'm using the ArcGISx6410.5 python interpreter)

 

I am grateful for any idea or proposition.

 

Sebastian

Outcomes