I've adapted some code from Apply same symbology to many raster layers in ArcMap? and the ESRI noticeboards to batch apply a .lyr file to several hundred .TIF files:

import arcpy
#Specifies mxd
mxd = arcpy.mapping.MapDocument(r"PATHTOMXD\ModelVisuals.mxd")

#Specifies layer file with the symbology you want to apply to the other raster layers
sourceLayer = arcpy.mapping.Layer(r"PATHTOLAYERFILE\FloodDepths.lyr")

#Loops through layers in the mxd
for lyr in arcpy.mapping.ListLayers(mxd,""):
    arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer)

The code runs fine with no errors and it loops through the rasters, locking them one by one, but it doesn't apply the symbology layer or turn them on.

I am very rusty with python for ArcGIS. What am I doing wrong or is there a better way of doing it?



This code (from this question) works perfectly:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
groupName = "Group"
lyrFile = "XXXXXXX"
allLayers = arcpy.mapping.ListLayers(mxd, groupName, df)[0]
for layer in allLayers:
     arcpy.ApplySymbologyFromLayer_management(layer, lyrFile)
     layer.visible = "True"

What is the purpose of the "[0]" at the end of the allLayers line? Although it is in the code snippets here it isn't explained.

  • is this running in arcgis or standalone? if standalone you should define your workspace arcpy.env,workspace = – ziggy Jan 9 '17 at 15:07
  • Desktop 10.3 so standalone. Adding "arcpy.env.workspace = "XXX:\My_Documents\GIS DataBase"" doesn't make any odds. – Ed Rollason Jan 9 '17 at 15:16
  • Are you running this code snippet from a Python script tool, the Python window of ArcMap, IDLE (or another IDE), ...? – PolyGeo Jan 9 '17 at 20:31

turning the layer visibility on is not something this tool does. It should only change the rendering properties. You will need to add mxd.save() to have the changes persist

for lyr in arcpy.mapping.ListLayers(mxd,""):
    arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer)
    lyr.visible = True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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