Hi all, I am trying to add a TIF file as an ArcGIS Pro map layer and apply symbology from a .lyrx file. If I manually drag a TIF file onto the map and import the .lyrx file, it draws just fine. These data layers are dynamic, so I want to automate adding many of these TIF files and symbolizing them via python. I found 2 different ways to use python to add a TIF file as a new map layer, but I'm stuck on applying the .lyrx file.
With either approach, I then use arcpy.ApplySymbologyFromLayer_management to try to set the symbology. I never get a python error, but it doesn't have any effect. The new layer is just there with the default stretched black-to-white color ramp.
datapath = 'C:\\temp'
import arcpy
import os
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
# approach 1 - creates a layer with a name I assign
arcpy.MakeRasterLayer_management(os.path.join(datapath, 'wrfout_d3.2015081400.f13.0000.UV10.tif'), 'f13')
<Result 'f13'>
asfl = arcpy.ApplySymbologyFromLayer_management('f13', os.path.join(datapath, 'wind_barbs_uv.lyrx'))
asfl
<Result 'f13'>
# approach 2 - creates a layer with the same name as the TIF
lyr = m.addDataFromPath(os.path.join(datapath, 'wrfout_d3.2015081400.f13.0000.UV10.tif'))
asfl2 = arcpy.ApplySymbologyFromLayer_management('wrfout_d3.2015081400.f13.0000.UV10.tif', os.path.join(datapath, 'wind_barbs_uv.lyrx'))
asfl2
<Result 'wrfout_d3.2015081400.f13.0000.UV10.tif'>
Possibly relevant details:
I'm not sure if I'm doing the wrong thing or missing a step. In the help, it shows ApplySymbologyFromLayer_management being used to apply symbology to another layer file, so I'm guessing it's not meant to applied to a map layer, but I don't know where to go from there.
Thanks!
Solved! Go to Solution.
David, please see my response on this related thread: Adding feature class and applying symbology in ArcGIS Pro using arcpy
It seems you are running into BUG-000108497: Running Apply Layer Symbology as a script in the Python window does not update the symbology in the map.
David... a shortish read, but something is afoot
so I am not clear if there is a definitive problem and/or solution.... hate to give you the ...Tech Support ...answer, but the more on an issue, the quicker they are identified and/or addressed
That was adding the layer, then getting the reference to it from the layer list, and then applying the symbology. I wasn't doing the middle step, instead using the layer object. When I tried that approach, I got the same result. Thanks, though.
The way I know how to this is to run the Make Feature Layer tool in the python script tool, returning the layer as an output parameter -- and applying the lyrx using the output parameter's Symbology property.
Apply Symbology From Layer—Data Management toolbox | ArcGIS Desktop
If the input is a feature class or dataset path, this tool will automatically create and return a new layer with the result of the tool applied.
2. That leaves the symbology field... which you have tried without success... manually I presume it isn't needed and reads and applies the symbology correctly, and whether/how to update the symbology
ApplySymbologyFromLayer_management (in_layer, in_symbology_layer, {symbology_fields}, {update_symbology})
3. Which leave my suggestion of filing a tech support case.
IF there is nothing that you do manually when not doing this via arcpy, you seem to have covered everything, but the 'issues' with applysymbologyfromlayer noted by others might just be wider than the sum of the parts OR the documentation needs to be updated with more than the simple use cases
Keep the thread posted if you find a workaround
David, please see my response on this related thread: Adding feature class and applying symbology in ArcGIS Pro using arcpy
It seems you are running into BUG-000108497: Running Apply Layer Symbology as a script in the Python window does not update the symbology in the map.
I updated the source using the same dataset for the in and out and it worked! Thanks!
datapath = 'C:\\temp\\2018-06\\WRF_wind_barbs_screenshots'
import arcpy
import os
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps('*')[0]
lyr = m.addDataFromPath(os.path.join(datapath, 'wrfout_d3.2015081400.f13.0000.UV10.tif'))
lyr0 = m.listLayers()[0]
print(lyr0)
asfl = arcpy.ApplySymbologyFromLayer_management(lyr0, os.path.join(datapath, 'wind_barbs_uv.lyrx'))
in_dict = {'dataset': 'wrfout_d3.2015081400.f13.0000.UV10.tif', 'database': datapath}
out_dict = {'dataset': 'wrfout_d3.2015081400.f13.0000.UV10.tif', 'database': datapath}
lyr0.updateConnectionProperties(in_dict, out_dict)
I just got an email from the bug saying that the way to fix this is to set the output as derived. Not had chance to test it thought I'd post in case someone wanted to
Public Explanation: To fix this issue:
I can confirm this works:
import sys, os, arcpy
import arcpy
inputLayer = 'lakes'
symbologyLayer = r"C:\Temp\symbologyBug\Polygon_Styles.lyrx"
# Symbolize the input by FIL_COLOURfield
symbologyFields = [["VALUE_FIELD", "FIL_COLOUR", "FIL_COLOUR"]]
arcpy.ApplySymbologyFromLayer_management(inputLayer, symbologyLayer, symbologyFields)
arcpy.SetParameterAsText(0, inputLayer)
This was very helpful!