arcpy.ApplySymbologyFromLayer_management does not define symbology within stand-alone script

1454
4
11-07-2017 08:01 AM
AnanyaBaruah
New Contributor

I have a python script that was initially running with ArcGIS Desktop 10.0. The script runs a bunch of analysis and then creates maps and exports to jpeg images. Prior to exporting ot jpeg, the script also defines the symbology of the map layers from some pre-defined saved layer files (.lyr). Recently, the ArcGIS license was upgraded to 10.4. The scripts works fine without any errors and produces all results. However, the symbology does not render in the new maps and ArcMAP sets some default color symbologies. Also, I checked the part (arcpy.ApplySymbologyFromLayer_management) which imports the symbologies within the ArcMAP python built-in window and it works correctly. I cannot figure out where the disconnect is in my stand alone script. Please help.

Note: Due to company policy I cannot share the code here, but any insight into the matter will be helpful.

Thanks

0 Kudos
4 Replies
RandyBurton
MVP Alum

Are you running the script in the Python window in ArcMap?  If so, are you refreshing the view before exporting your jpeg?

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
AnanyaBaruah
New Contributor

Sorry for the delay in replying.

The script works when I run in Python window in ArcMap. It renders the symbology correctly. The stand alone script does not render the symbology correctly.

0 Kudos
DanPatterson_Retired
MVP Emeritus

That is because the standalone script knows nothing about resetting the symbology unless you are using the mapping module and saving changes to the *.mxd

RandyBurton
MVP Alum

You mention that a "bunch of analysis" is done.  Would ArcMap be creating layers where the name changes with each run, and if so, is the script adapting to this change? Print statements would help identify this problem.

This is a sample of the code that I use with ApplySymbologyFromLayer.

import os
import arcpy # if outside ArcMap

# inside ArcMap, use "CURRENT" for document name
mxd = arcpy.mapping.MapDocument(r"C:\Path\To\map.mxd")
# layer file is in directory with mxd map document; this will be the workspace
arcpy.env.workspace = os.path.dirname(mxd.filePath)

# dictonary matches map layer with symbology layer file
symbols = {'SymbolTest':'LayerSymbology.lyr'}

for layer in arcpy.mapping.ListLayers(mxd):
  if layer.name in symbols:
    print "Applying symbology to layer '{}' using '{}'.".format(layer.name, symbols[layer.name])
    arcpy.ApplySymbologyFromLayer_management(layer, symbols[layer.name])
  else:
    print "Layer '{}' not in map.".format(layer.name)

# may need to refresh map
# arcpy.RefreshTOC()
# arcpy.RefreshActiveView()

# save exported jpg in directory with mxd file using mxd name
arcpy.mapping.ExportToJPEG(mxd,os.path.splitext(os.path.basename(mxd.filePath))[0])

del mxd‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍