Select to view content in your preferred language

Can't get ApplySymbologyFromLayer to work on Rasters.

1179
5
03-23-2019 06:04 PM
AdamArcichowski1
Occasional Contributor

I'm having a hard time symbolizing a raster from a Layer file. 

rasterPath = r"......\DEM\slopeoutput1"

rasterLayerName = 'slopeoutput12'

SlopeSymbology1 = ".....\SlopeAnalysis\HeatSlope.lyr"

md = arcpy.mapping.MapDocument(mxdpath)
df = arcpy.mapping.ListDataFrames(md)[0]

arcpy.MakeRasterLayer_management(rasterPath, rasterLayerName)
arcpy.mapping.Layer(rasterLayerName)
arcpy.ApplySymbologyFromLayer_management(rasterLayerName, SlopeSymbology1)

md.save()

I can get this to work in the python window but then when i run it as a script it doesnt apply the Symbology. 

Tags (1)
0 Kudos
5 Replies
curtvprice
MVP Esteemed Contributor

When a layer is created inside a script it isn't necessarily added to the map unless you add it to the data frame (which you are not doing in your snippet, though I see you got a handle to the data frame, you did not use it.)

I suspect you may be making this a bit more complicated that it has to be.  If you create a raster and return it as an output parameter for a model, there is no coding required to apply symbology, in the script tool parameters you apply the symbology from the layer to the output parameter, and the output raster is added to the map using the symbology you assigned.

Setting output symbology in scripts—Help | ArcGIS Desktop 

0 Kudos
deleted-user-yC5VkbyXzrQR
Deactivated User

Thanks Curtis for the reply. 

I understand, however the output will not be a parameter, it will be be saved in a FGDB automatically.

I managed to get it to work by doing the following:

arcpy.AddMessage("Adding raster & applying symbology.")
arcpy.MakeRasterLayer_management(rasterPath, rasterLayerName)
arcpy.mapping.Layer(rasterLayerName)
arcpy.ApplySymbologyFromLayer_management(rasterLayerName, SlopeSymbology1)
arcpy.SaveToLayerFile_management(rasterLayerName, symbolized_raster)
addLayer = arcpy.mapping.Layer(rasterLayerName)
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
arcpy.AddMessage("Finished Adding raster & applying symbology.")

Seems like a lot of code, but it works ahah. 

curtvprice
MVP Esteemed Contributor
I understand, however the output will not be a parameter, it will be be saved in a FGDB automatically.

If the output path is not exposed as an output tool parameter (ie the output path is generated internally by the script), you set the output to Derived, apply the .lyr symbology to it -- and in your code populate the output path into that derived parameter with SetParameterAsText.

That said, glad you got it to work though -- whatever works is good as far as I am concerned!

0 Kudos
AdamArcichowski1
Occasional Contributor

Yes, this would make my code a lot cleaner. 

Could you show me what you mean. I am aware of how to change Parameter Property to Derived. I just dont know how it would look in the script. Below is a test code i written up.

import arcpy, datetime
date = datetime.datetime.now()
namefile = arcpy.GetParameterAsText(0)
prjbdy = arcpy.GetParameterAsText(1)
express = arcpy.GetParameterAsText(2)
prjsymb = arcpy.SetParameterAsText(3)
mxdpath = "C:\Users\Adam\Desktop\Test5.mxd"
arcpy.CreateFileGDB_management(namefile, "\\SlopeAdam" + date.strftime("%Y%m%d")+ ".gdb")
fgdb = namefile + "\\Slope" + date.strftime("%Y%m%d")+ ".gdb"
arcpy.env.workspace = fgdb
mxd = arcpy.mapping.MapDocument(mxdpath)
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.MakeFeatureLayer_management(prjbdy, "prjbdy_Layer",express)
arcpy.CopyFeatures_management("prjbdy_Layer", "testingggADAdaani")
arcpy.MakeFeatureLayer_management("testingggADAdaani", "test2")
layer = arcpy.mapping.Layer("test2")
arcpy.mapping.AddLayer(df, layer, "AUTO_ARRANGE")
arcpy.ApplySymbologyFromLayer_management('test2', "C:\Users\Adam\Desktop\SlopeAnalysis\prjbdy_Layer.lyr")
mxd.save()
del mxd‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
curtvprice
MVP Esteemed Contributor

Here's an attempt as to what I'm getting at. I am assuming you want to run this script tool from ArcMap and you want the created feature class to be added to the current map. Your approach you have already (above) is appropriate if you are trying update an MXD on disk.

import arcpy, datetime
# script parameter type Folder, Input
outfolder = arcpy.GetParameterAsText(0)
# script parameter type Feature Class, Input
prjbdy = arcpy.GetParameterAsText(1)
# in script tool properties,  type SQL Expression, Input
# and set dependent on parameter 1 table
express = arcpy.GetParameterAsText(2)
# Script parameter 3 type Feature Class, Derived
# and assign .lyr symbology to param 3
# Parameter 3 derived value is set below at end of script

# hard coded feature class name
outfc_name = "testingggADAdaani"
date = datetime.datetime.now()
fgdb_name =  "SlopeAdam{}.gdb".format(date.strftime("%Y%m%d"))
fgdb = arcpy.CreateFileGDB_management(namefile, fgdb_name)
arcpy.env.workspace = str(fgdb)  # str() converts result to path
outfc = arcpy.Select_analysis(prjbdy, outfc_name, express)
arcpy.SetParameterAsText(3, outfc)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍