TransWikia.com

Why would arcpy ApplySymbologyFromLayer tool not work in ArcGIS Pro Python window?

Geographic Information Systems Asked by T Dog on April 9, 2021

I can’t seem to make ApplySymbologyFromLayer work in ArcGIS Pro console, or in a scripting tool.

It works in an independant python script run in IDLE, but not when executed inside ArcGIS Pro GUI, neither from a scripting tool or the console.

For a basic “single symbol” symbology, this snippet should do the trick :

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps(your_map_name)[0]

map.addDataFromPath(path/to/your/layer)

in_layer = map.listLayers(your_layer_name)[0]
symbology_layer = path/to/your/layer.lyrx
arcpy.management.ApplySymbologyFromLayer(in_layer, symbology_layer, None)

It doesn’t matter weither I try to apply the symbology from a lyrx file or from a data layer present inside map. Changing arcpy.management.ApplySymbologyFromLayer to arcpy.ApplySymbologyFromLayer_management doesn’t change the output either. Neither does replacing the “None” as a last argument of the function by #, or nothing.

When executed, these code blocks say they worked, but the symbology is unchanged on the map. However, it appears to be changed in the symbology tab.

Here, I have tried to apply a grey, 7 pt symbology to this layer currently in pink. It is still pink in the map, and in the TOC, but if you go in the symbology panel for this layer, you see the symbology it is supposed to have.
enter image description here
It looks like a bug to me for the symbology displayed on the map does not match the one you can see on the symbology panel.

Is there a way to make this command work when executed from within ArcGIS Pro console or from a scripting tool ?

I tried this at first using ArcGIS Pro 2.0. I recently updated to ArcGIS Pro 2.1, but the problem is still there. Also tried it on different computers and for different datas, and data types (points, lines, polygons)

3 Answers

In order to get this to work in your scripts inside ArcGIS Pro, I discovered it is best or even vital to first add the layers to the ArcGIS Pro TOC / Map, instead of just referencing them via a created Feature Layer. See the code below for an example.

Using this approach, I have been able to successfully update the layer symbology using the ApplySymbologyFromLayer_management tool with scripts running as a tool from a toolbox within ArcGIS Pro.

in_layer = YOUR CODE TO CREATE in_layer
in_symbology_layer = YOUR CODE TO CREATE in_symbology_layer

aprx = arcpy.mp.ArcGISProject("CURRENT")

aprxMap = aprx.activeMap # NOTE(!): 'aprx.activeMap' is only supported from Pro 2.1 onwards!!! activeMap returns None if script is called outside a running application according to ArcGIS Pro Help.

aprxMap.addLayer(in_layer, "AUTO_ARRANGE")
in_layer = aprxMap.listLayers(in_layer.name)[0]

aprxMap.addLayer(in_symbology_layer, "AUTO_ARRANGE")
in_symbology_layer = aprxMap.listLayers(in_symbology_layer.name)[0]

in_layer = arcpy.ApplySymbologyFromLayer_management(in_layer,in_symbology_layer,update_symbology="MAINTAIN")[0]

Answered by Marco_B on April 9, 2021

This is still logged with ESRI as a bug: https://support.esri.com/en/bugs/nimbus/QlVHLTAwMDEwODQ5Nw== Bug BUG-000108497 -Running the Apply Layer Symbology tool as a script in the Python window does not update the symbology in the map.

Still not fixed. Alternate solution is 'close and reopen the project' (??)

Answered by LeasMaps on April 9, 2021

Using ArcGIS Pro 2.6.1

I found that passing a layer object through the ApplySymbologyFromLayer() function, as the first arg, does not work. For whatever reason, you should pass a string through it. So basically change your line from:

in_layer = arcpy.ApplySymbologyFromLayer_management(in_layer,in_symbology_layer,update_symbology="MAINTAIN")[0]

to:

in_layer = arcpy.ApplySymbologyFromLayer_management(str(in_layer),in_symbology_layer,update_symbology="MAINTAIN")[0]

Answered by edgrmdna on April 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP