1

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")
2
  • Welcome to GIS SE, python is indentation strict, not sure if you posted code is what you have on your end as far as indentation goes. Are you getting any error messages?
    – artwork21
    Commented Jun 10, 2016 at 19:02
  • Thanks for responding artwork21. The indentation may be off because of how I posted the script. No, I'm not getting any error messages.
    – Eves
    Commented Jun 10, 2016 at 19:18

2 Answers 2

1

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.

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")

    # Find the new layer in the MXD
    newlayer = arcpy.mapping.ListLayers(mxd, addlayer.name)[0]

    # Apply the symbology to the layer in the MXD
    arcpy.ApplySymbologyFromLayer_management(newlayer, addSym)
0
-2

I've also recently discovered if you apply the symbology before adding the layer to the MXD it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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