2

I have an ArcPy script to apply the symbology of one raster layer to all of the raster layers in the table of contents. I have used this script before with feature layers instead of raster layers, and it worked perfectly. Now when I run it, it runs continuously and it does not change the symbology of any of the raster layers in the table of contents. It does not give me any error messages; the window appears, saying that it successfully changed the symbology. Also, it works when I manually apply the symbology of one raster layer to the other layers in the table of contents. The script seems to iterate through the list without actually changing anything. ArcPy Raster Symbology Script

4
  • for layer in df: should be for layer in rasters:... df is a list of data frames. You might also need to put in a condition that the layer is not "Feature_El147". Nov 18, 2014 at 0:28
  • I tried changing it and I received the error message: "AttributeError: LayerObject: Get attribute layers does not exist."
    – user40332
    Nov 18, 2014 at 0:30
  • Your syntax is incorrect. You have the right line rasters = arcpy.mapping.ListLayers(mxd,"",df)* but then overwite it with a single layer with rasters = arcpy.mapping.ListLayers(mxd)[0] so it's not a layers but a layer. Nov 18, 2014 at 0:33
  • 3
    Just a tip when writing Python code questions. Instead of including a picture of your code, which cannot be copy/pasted from to test, it is better for all of us if you simply copy/paste your code in and use the {} button to format it.
    – PolyGeo
    Nov 18, 2014 at 1:03

4 Answers 4

3

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'.

12
  • Thank you for rewriting my code, but I tried it and, unfortunately, it still isn't working. I didn't receive any error messages, and the window appears saying that it successfully applied the symbology even though it didn't.
    – user40332
    Nov 18, 2014 at 1:32
  • Try saving Feature_el147 to a layer file and supply the full path to the variable. Ensure that the raster layers can be changed to that type - What is the symbology of the layer? is it stretched, unique or classified? The sample didn't have a star so you could try that rasters = arcpy.mapping.ListLayers(mxd,"",df) Nov 18, 2014 at 1:41
  • I tried, but it still isn't working. The symbology is "classified."
    – user40332
    Nov 18, 2014 at 3:14
  • In order to be classified the statistics must be calculated, I will edit that into the script but it will take a little time to run. Nov 18, 2014 at 3:26
  • I tried to run it, and I received the error message: "AttributeError: 'Layer' object has no attribute 'isValid'."
    – user40332
    Nov 19, 2014 at 14:26
1

The code below works for me, though I think it's a bit sloppy. I had to include make raster layer and save to layer for it to work.

    arcpy.MakeRasterLayer_management(in_raster, out_raster)
    arcpy.SaveToLayerFile_management(out_raster, display_raster, "ABSOLUTE")

    # Adjust symbology of Raster layer
    arcpy.ApplySymbologyFromLayer_management(display_raster, raster_symbology)
    arcpy.SaveToLayerFile_management(display_raster, symbolized_raster, "ABSOLUTE")

    # Add layer to map 
    mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
    addLayer = arcpy.mapping.Layer(symbolized_raster)
    arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

The code works properly and adds the map to the display, but it does have a problem with the data source. It sources to a temporary location instead of the workspace environment.

Folder: C:\Users\User\AppData\Local\Temp 

Raster: x234bc54_d8c9_c34_by1c458
1

I've just tested the code below and it works fine. You need to be careful in your code that you don't try and apply symbology to the template raster itself otherwise you'll receive an error so I've added an if statement.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
rasters = arcpy.mapping.ListLayers(mxd, "", df)
for layer in df:
  templatelyr = 'Feature_E1147'
  if layer.name != templateLyr:
    lyr = arcpy.mapping.Layer(templatelyr)
    arcpy.ApplySymbologyFromLayer_management(layer, lyr)
0

What is "Feature_El147"? The help for that function says that the second argument has to be a: Feature Layer; Raster Layer;TIN Layer; Network Analysis Layer;Geostatistical Layer.

Which in this context refers to a layer currently in the map document that has the symbology you want. Is that how you've got it set up?

3
  • "Feature_EL147" is the raster layer with the symbology that I want to apply to all of the raster layers in the table of contents. I have "Feature_El147" in the data frame "Georgia" along with 40 other raster layers.
    – user40332
    Nov 18, 2014 at 1:34
  • 1
    What kind of symbology? If it is 'classified', it is possible that statistics tables are missing in target layers
    – FelixIP
    Nov 18, 2014 at 1:45
  • It is "classified."
    – user40332
    Nov 18, 2014 at 3:15

Your Answer

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