Import symbology from layer - arcpy

2089
4
03-21-2017 07:57 AM
martinaraffellini
New Contributor II

Hello!

I am trying to update the symbology of a raste layer using an existing layer file with 2 different functions:

 - update layer

- apply symbology from layer

The same function works with shapefiles, though.

Nothing happens, still the code runs till the end with no error message, which makes it pretty difficult to find the error.

import arcpy
arcpy.env.overwriteOutput = True
workDir = 'C:/0.STUDY PROJECT/Maps/'

mxd = arcpy.mapping.MapDocument(template)
dfs = arcpy.mapping.ListDataFrames(mxd)
df = dfs[0]


# clip is obtained interpolating a point shapefile with natural neighbours method and clipped with a polygon shapefile
clip = "C:\\0.STUDY PROJECT\\MapsKulmbach\\processed_data\\clip"

depth_data = arcpy.mapping.Layer(clip)
arcpy.mapping.AddLayer(df, depth_data)

srcLay = workDir + "depthSymbology.lyr"
srcLayObj = arcpy.mapping.Layer(srcLay)

# try function 1
arcpy.mapping.UpdateLayer(df, depth_data, srcLayObj, True)

# try function 2
arcpy.ApplySymbologyFromLayer_management (depth_data, srcLayObj)

mxd.save()
arcpy.RefreshActiveView()

Do you know what a reason could be?

Thank you

Tags (1)
0 Kudos
4 Replies
JimCousins
MVP Regular Contributor

It is difficult to pin this down without seeing the entire script. However, is "data" returning a single raster object, or a list of rasters in a directory location? Try hardcoding a single raster layer, and running that through. I have a similar script that I use without troubles:

# Purpose: This funtion applies a lyr file to set symbology of a layer in the map.
# This function takes 2 arguments:
# 1) the layer to set symbolgy for
# 2) the symbolgy source
# Works very well from within a model


import arcpy
import sys
import string
import arcpy.mapping
from arcpy import env
arcpy.env.overwriteOutput = True

# Local Variables from input
Layer2Symbolize = sys.argv[1]
lyrSource = sys.argv[2]

# Process - Apply Symbology From Layer
arcpy.ApplySymbologyFromLayer_management(Layer2Symbolize, lyrSource)

Best Regards,

Jim

0 Kudos
martinaraffellini
New Contributor II

Thank you for your reply.

The whole code is too long but i can add a few lines in the script above.

Does your code work with shapefiles, rasters, TIN and layerfiles?

I can manage only with shp

0 Kudos
JimCousins
MVP Regular Contributor

I have not tested it with tins, but for shapefiles and rasters I use it often. I usually configure in model builder, but have populated a for loop in python to apply the symbology to a set of files as well. To do this, I get a list of layers in a map, and loop through them 1 at a time to apply the symbology. You could also define a function and pass the srcLay object and the data object.

My concern is still with the object “data”. Is it a single raster or is it a list? If it is a list, you need to work with them 1 at a time inside a loop.

Regards,

Jim

martinaraffellini
New Contributor II

no, it is a single raster. I have to put it in a loop later, but now i am trying one step

0 Kudos