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) for layer in allLayers: arcpy.ApplySymbologyFromLayer_management(layer, lyrFile) layer.visible = "True" arcpy.RefreshActiveView()
What is the purpose of the "" at the end of the allLayers line? Although it is in the code snippets here it isn't explained.