I'm trying to add symbology to a group of feature classes. Listed below is one example. The field names are the same, but the symbology isn't working in arcpy. It does however work when I used the apply symbology tool in ArcMap.
## Import system modules
import arcpy
import os
from arcpy import env
import datetime
# define variable to be used at end of code to change setting back to default
origSetting = env.overwriteOutput
# change environment setting to overwrite output
env.overwriteOutput = True
################################################################################
def makeMap(city):
newName = arcpy.ValidateTableName(city.strip("'"))
#get the map template
mxd = arcpy.mapping.MapDocument(r"C:\Template20160603.mxd")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
#Locate the layers to add to the map
folder = r"C:\test" + "\\" + newName
#loop through the layers in the city folder and add them to the mxd
walk = arcpy.da.Walk(folder, datatype=['Layer','FeatureClass'])
print "Adding layers to map for", city,"\n"
for dirpath, dirnames, filenames in walk:
for filename in filenames:
print filename
layerfile = os.path.join(dirpath, filename)
#add indicator layers to map and ensure that the error layer is above the corresponding indicator layer
i=filename.find('CT')
if i!=-1:
CTLayer = arcpy.mapping.Layer(layerfile)
arcpy.mapping.AddLayer(dataFrame, CTLayer, "BOTTOM")
#zoom to current county extent
dataFrame.extent = CTLayer.getSelectedExtent(False)
dataFrame.scale = dataFrame.scale * 1.01
indicator=("x","y")
if any(s in filename for s in indicator):
addlayerrse = arcpy.mapping.Layer(layerfile)
addlayerrse.name = str(addlayerrse) + "_rse"
arcpy.mapping.AddLayer(dataFrame, addlayerrse,"BOTTOM")
strings = ("a", "b", "c")
if any(s in filename for s in strings):
addlayer = arcpy.mapping.Layer(layerfile)
arcpy.mapping.AddLayer(dataFrame,addlayer,"BOTTOM")
if indicator=="a":
test=r"C:\Symbology\SymA.lyr"
addSym = arcpy.mapping.Layer(test)
arcpy.mapping.AddLayer(dataFrame,addSym,"BOTTOM")
arcpy.ApplySymbologyFromLayer_management(addlayer, addSym)
date=datetime.date.today()
#create a mxd with city name and date program was run
mxd.saveACopy(r"C:\mxd" + "\\"+ newName + "CT_" + str(date) + ".mxd")
del mxd
print "\n","Program Complete"
makeMap("cityx")
Best Answer
You appear to be trying to apply symbology to your layer file, not to the layer in your MXD.
You may need to find the layer in your MXD and then apply symbology to that.