There's a few minor hiccups in your code, I've re-written it (hopefully) better:
import arcpy
arcpy.env.workspace = "c:\DEM Files" # not requred
mxd = arcpy.mapping.MapDocument("Current") # This MXD
df = arcpy.mapping.ListDataFrames(mxd,"Georgia")[0] # the first data frame called Georgia
rasters = arcpy.mapping.ListLayers(mxd,"*",df) # all the layers
# normally the layer would be a layer file, not so much a layer in the map
symbologyLayer = "Feature_El147" # I'm assuming this exists
for ThisLayer in rasters:
print "Working on " + ThisLayer.name
if not ThisLayer.isBroken: # only try to work with layers that aren't broken
print "-not broken"
if not ThisLayer.name.upper() == symbologyLayer.upper():
print "-not the source layer"
# not the source layer
if ThisLayer.isRasterLayer:
print "-is a raster layer"
# only applies to raster layers
arcpy.CalculateStatistics_management(ThisLayer.dataSource)
print "--Statistics calculated"
arcpy.ApplySymbologyFromLayer_management(ThisLayer,symbologyLayer)
print "--Symbology Applied"
mxd.save()
del mxd
This assumes that the data frame called Georgia exists in the current mxd, if it doesn't then you'll have problems. I've added a condition for isRasterLayer in case you've got mixed feature/raster/group layers - note, you might want to include the layer.isBroken as well if you've got unreferenced layers (red exclamation marks).
I have changed the script and tested it; this version works on ArcGis 10.1:
import arcpy, sys
try:
symbologyLayer = arcpy.mapping.Layer(sys.argv[1])
except:
arcpy.AddError("Input parameters could not be resolved")
sys.exit(-1)
arcpy.AddMessage("Source Layer is %s" % symbologyLayer.symbologyType)
# Exit if the source layer is broken
if symbologyLayer.isBroken:
arcpy.AddError("Source layer is broken")
sys.exit(-2)
# Exit if the source layer is not a raster layer
if not symbologyLayer.isRasterLayer:
arcpy.AddError("Source layer is not a raster layer")
sys.exit(-3)
mxd = arcpy.mapping.MapDocument("Current") # This MXD
#df = arcpy.mapping.ListDataFrames(mxd,"Georgia")[0] # the first data frame called Georgia
df = arcpy.mapping.ListDataFrames(mxd)[0] # Just the first data frame in the MXD
rasters = arcpy.mapping.ListLayers(mxd,"*",df) # all the layers
for ThisLayer in rasters:
arcpy.AddMessage( "Working on " + ThisLayer.name)
if not ThisLayer.isBroken: # only try to work with layers that aren't broken
arcpy.AddMessage( "-not broken")
if not ThisLayer.name.upper() == symbologyLayer.name.upper():
arcpy.AddMessage( "-not the source layer")
# not the source layer
if ThisLayer.isRasterLayer:
arcpy.AddMessage( "-is a raster layer")
# only applies to raster layers
arcpy.CalculateStatistics_management(ThisLayer.dataSource)
arcpy.AddMessage( "--Statistics calculated")
arcpy.AddMessage( "--Raster symbology is %s" % ThisLayer.symbologyType)
arcpy.ApplySymbologyFromLayer_management(ThisLayer,symbologyLayer)
arcpy.AddMessage( "--Symbology Applied")
#mxd.save() # I'm not saving, uncomment this to save
del mxd
I have commented out lines that don't suit my environment...
It is possible that it's having issues running from the python window so I set up the code to run from a toolbox, copy and paste the code into notepad, save as .py file then create a new toolbox in Catalog and add the script with one parameter: Layer to match
, type Layer File
.
In ArcMap run the tool from the toolbox (in catalog view is fine) and it will apply the symbology from the layer file.. to get the original layer file right click on the correct symbology layer and select 'Save As Layer File'.
Best Answer
I haven't tried this personally, but it looks like you need to access the GraduatedColorsSymbology class on the layer. Have a look at the second example at the bottom of this page.
It also looks like you may need to have an existing lyr file with the graduated colours already set in order to use UpdateLayer.