Apply Symbology to TOC layers

1549
4
Jump to solution
01-26-2018 04:46 PM
JordanMiller5
New Contributor

I have a .lyr file on my desktop that I would like apply symbology to a TOC sde layer in an existing map document. How can I do this using python IDLE?

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

Looks like you are on the right track.  You can use ApplySymbology outside with IDLE; what type of error do you get when using IDLE.

Here's a script that I use to apply symbology using a layer file:

import os
import arcpy # if outside ArcMap

# inside ArcMap, use "CURRENT" for document name
mxd = arcpy.mapping.MapDocument(r"C:\Path\To\SymbolTest.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
# layer name in TOC : name of layer file (can use full path/file name if not in map directory)
symbols = {'SymbolTest':'LayerSymbology.lyr'}

for layer in arcpy.mapping.ListLayers(mxd):
  if layer.name in symbols:
    print "Layer: '{}' - previous symbology type: '{}'.".format(layer.name, layer.symbologyType)
    # http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/apply-symbology-from-layer....
    # ApplySymbologyFromLayer_management (in_layer, in_symbology_layer)
    print "Applying symbology to layer '{}' using '{}'.".format(layer.name, symbols[layer.name])
    arcpy.ApplySymbologyFromLayer_management(layer, symbols[layer.name])
    print "Layer: '{}' - new symbology type: '{}'.".format(layer.name, layer.symbologyType)
  else:
    print "Layer '{}' not updated.".format(layer.name)

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

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

View solution in original post

4 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Did you see Apply Symbology from Layer there is a code sample at the end.

JordanMiller5
New Contributor

Hi Dan,

I did try using the example code but it didn't work. I'll post my code and error.

Edit: If I run the symbology to TOC part of the script inside the map python it works great. Why is it not able to run outside in IDLE?

# Name: Create Map

# Import system modules
import arcpy
from arcpy import env

# Rebuild Map Workspace
#
mxd = arcpy.mapping.MapDocument(r"C:\Users\aa2zz6\Desktop\test\Untitled.mxd")

df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
# Add layer to TOC
#
addLayer = arcpy.mapping.Layer(r"I:\UPDM.gdb\P_PipeSystem\P_Meters")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

addLayer = arcpy.mapping.Layer(r"I:\UPDM.gdb\P_PipeSystem\P_MeterSetting")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

addLayer = arcpy.mapping.Layer(r"I:\UPDM.gdb\P_PipeSystem\P_Regulator")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

addLayer = arcpy.mapping.Layer(r"I:\UPDM.gdb\P_PipeSystem\P_TownBorderStation")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

mxd.save()

# Apply Symbology to TOC layer

#Set layer to apply symbology to
lyr = "P_Meters"

# Set layer that output symbology will be based on
ymbolLyr = r"C:\Users\aa2zz6\Desktop\test\Meters.lyr"

# Apply the symbology from the symbology layer to the input layer
arcpy.ApplySymbologyFromLayer_management(lyr, symbolLyr)

Error

Traceback (most recent call last): File "C:\Users\aa2zz6\Downloads\CREATE NEW MXD.py", line 38, in arcpy.ApplySymbologyFromLayer_management(lyr, symbolLyr) NameError: name 'symbolLyr' is not defined

0 Kudos
Madanbhurati
New Contributor III

Hi Jordan,

In following line of script "symbolLyr"  is defined as "ymbolLyr", as stated in error trace back.

# Set layer that output symbology will be based on

ymbolLyr = r"C:\Users\aa2zz6\Desktop\test\Meters.lyr"

RandyBurton
MVP Regular Contributor

Looks like you are on the right track.  You can use ApplySymbology outside with IDLE; what type of error do you get when using IDLE.

Here's a script that I use to apply symbology using a layer file:

import os
import arcpy # if outside ArcMap

# inside ArcMap, use "CURRENT" for document name
mxd = arcpy.mapping.MapDocument(r"C:\Path\To\SymbolTest.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
# layer name in TOC : name of layer file (can use full path/file name if not in map directory)
symbols = {'SymbolTest':'LayerSymbology.lyr'}

for layer in arcpy.mapping.ListLayers(mxd):
  if layer.name in symbols:
    print "Layer: '{}' - previous symbology type: '{}'.".format(layer.name, layer.symbologyType)
    # http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/apply-symbology-from-layer....
    # ApplySymbologyFromLayer_management (in_layer, in_symbology_layer)
    print "Applying symbology to layer '{}' using '{}'.".format(layer.name, symbols[layer.name])
    arcpy.ApplySymbologyFromLayer_management(layer, symbols[layer.name])
    print "Layer: '{}' - new symbology type: '{}'.".format(layer.name, layer.symbologyType)
  else:
    print "Layer '{}' not updated.".format(layer.name)

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

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