Apply .lyrx file symbology via Python

5342
9
Jump to solution
06-25-2018 12:05 PM
DavidAskov1
Occasional Contributor

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 am using the "Vector Field" symbology type
  • The TIF has 2 bands for the U and the V direction components (these are set in .lyrx file)
  • I also tried plugging the default band names ("Band_1" & "Band_2") into the "symbology_fields" parameter of the ApplySymbologyFromLayer_management function. It also had no effect. 
  • Updated from 2.1.2 to 2.1.3 with the same result.

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!

0 Kudos
1 Solution

Accepted Solutions
EarlMedina
Esri Regular Contributor

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.

View solution in original post

9 Replies
DanPatterson_Retired
MVP Emeritus

David... a shortish read, but something is afoot

https://community.esri.com/message/778895-adding-feature-class-and-applying-symbology-in-arcgis-pro-... 

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

DavidAskov1
Occasional Contributor

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. 

0 Kudos
curtvprice
MVP Esteemed Contributor

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.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Apply Symbology From Layer—Data Management toolbox | ArcGIS Desktop 

  1. So you ruled out adding the data from a path 

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

EarlMedina
Esri Regular Contributor

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.

DavidAskov1
Occasional Contributor

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)
DavidWilton
Occasional Contributor

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:

  1. After adding the arcpy.ApplySymbologyFromLayer_management(layer, lyrfile) Python command, add the arcpy.SetParameterAsText(2, layer) parameter.
  2. Save the script.
  3. ArcGIS Pro > Catalog Pane > Toolbox > Right click Script > Properties.
  4. Properties Window > Parameter Tab > Add a new parameter of type Layer, and set it to derived output Documenation: https://pro.arcgis.com/en/pro-app/arcpy/geoprocessing_and_python/setting-script-tool-parameters.htm#...
0 Kudos
DavidWilton
Occasional Contributor

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)
2Quiker
Occasional Contributor II

This was very helpful!

0 Kudos