Adding feature class and applying symbology in ArcGIS Pro using arcpy

9555
27
06-13-2018 09:40 PM
SeanHough
New Contributor II

Using an arcpy script run from a toolbox, I want to import a feature class from a geodatabase into an ArcPro map and then symbolise that layer based on a lyrx file stored on disk. Here is a simplified version:

import arcpy, os 
aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.listMaps('*')[0]
outputWorkspace = r"C:\Path\To\MyProject.gdb"
arcpy.env.workspace = outputWorkspace 
out_fc = "fc_data"
out_fc_p = os.path.join(outputWorkspace, out_fc)
ref_lyrx = r"C:\Path\To\symbol_ref.lyrx"
m.addDataFromPath(out_fc_p)
out_fc_lyr = m.listLayers()[0]
arcpy.ApplySymbologyFromLayer_management(out_fc_lyr, ref_lyrx)

The script completes without error but no visible change is made to the symbology. Is this a bug? I've reviewed many similar questions but have been unable to resolve this.

Tags (2)
27 Replies
TingDai
New Contributor

This method works! I am using 2.8.2. Thanks!

0 Kudos
KeithMiller4
New Contributor III

Both of these programmatic fixes work for me in the Python window within ArcGIS Pro. However, I can get neither of them to work as part of a Python Toolbox tool script.

MichaelLowry2
New Contributor II

BUG-000106281 about this issue is apparently fixed for Pro SDK at release 2.3,

https://pro.arcgis.com/en/pro-app/get-started/release-notes.htm

but I still cannot apply symbology from a layer file to a layer in my map through a script tool in Pro.

I have tried both call methods:

arcpy.management.ApplySymbologyFromLayer(target_layer, symbology_layer)

arcpy.ApplySymbologyFromLayer_management(target_layer, symbology_layer)

And I have tried .lyr and .lyrx

I know it is a problem with Pro, because if I run the exact same script tool in ArcMap (10.3) it works properly.

Any information or suggestions?

DavidWilton
Occasional Contributor

See my comment below. I still have a case open. I think it may have been closed for the net sdk but not python. Yet for some reason the complaint about python was attached as a duplicate of the pro issue and closed with it which is pretty frustrating. I don’t think two cases using different technologies should ever be considered a duplicate personally but not much we can do. I’m still pushing my case 

BUG-000119907 - The Apply Symbology From Layer tool (arcpy.ApplySymbologyFromLayer_management) does not work if used in a python script from the tool box in ArcGIS Pro 2.3

LeandraGordon
Occasional Contributor II

Looks like they have a separate case open for just Python now: BUG-000108497: Running the Apply Layer Symbology tool as a script i.. 

0 Kudos
by Anonymous User
Not applicable

Good morning. I have been working on applying symbology from layer without results. This needs to be implemented in a stand alone python script. Ultimately it will have to apply many lyrx files to matching feature classes, however I can not even get this to apply a single feature class on test data.

Has this bug been fixed?

Please let me know as soon as you can, thank you!!

Justine

JoshuaBixby
MVP Esteemed Contributor

This defect, BUG-000119907, is publicly logged:  BUG-000119907: The Apply Symbology From Layer tool does not work if..  You can check the current status and see the alternate work arounds.

0 Kudos
by Anonymous User
Not applicable

Thanks for your speedy reply. I am trying to do this in a stand-alone python script & it is failing. The data does have the appropriate fields, data source is not broken, etc. 

Do you have advice if this is a bug in stand alone scripts & work arounds for that?

Thank you,

Justine

RichardHowe
Occasional Contributor III

Interestingly I hit this issue whilst using the Apply Symboogy from layer GP tool within model builder. Setting the output as a parameter solved the issue. I'm not sure what the pythonic equivalent to that would be though

0 Kudos
MichaelLowry2
New Contributor II

The updateConnectionProperties solution did not work for me when running as a script. This workaround does:

current_project = arcpy.mp.ArcGISProject("CURRENT")
current_map = current_project.listMaps()[0]
symbology_layer = SymbologyPath + '\\FLO_scaled_graduated_symbol.lyrx'
new_lyr_file = arcpy.mp.LayerFile(symbology_layer)
new_lyr = new_lyr_file.listLayers()[0]
old_lyr = current_map.listLayers(original_network)[0]
old_lyr_name = old_lyr.name
new_lyr.updateConnectionProperties(new_lyr.connectionProperties, old_lyr.connectionProperties)
new_lyr.name = old_lyr_name
new_lyr_file.save()
current_map.insertLayer(old_lyr, new_lyr_file)
current_map.removeLayer(old_lyr)

However, I want to publish this tool and I cannot because as far as I can tell webtools cannot use "CURRENT".

By the way these are the other methods I have tried, none of which work in Pro but work fine in ArcMap:

1.

arcpy.ApplySymbologyFromLayer_management(inputLayer, symbologyLayer)

2. Using derived output (e.g. parameter 2):

arcpy.SetParameterAsText(2, inputLayer)

params = arcpy.GetParameterInfo()

params[2].symbology = symbologyLayer

3. Using derived output and pointing to the symbology file when setting script tool parameters.