Adding feature class and applying symbology in ArcGIS Pro using arcpy

3951
24
06-13-2018 09:40 PM
Highlighted
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)
24 Replies
Highlighted
MVP Esteemed Contributor

Based on the help topic ...

Apply Symbology From Layer—Data Management toolbox | ArcGIS Desktop 

the two optional parameters aren't specified

ApplySymbologyFromLayer_management (in_layer, in_symbology_layer, {symbology_fields}, {update_symbology})

which makes me wonder what the layer looks like before and after if this were done manually to ensure that the limitations of the 'immediate mode' are indeed being met (ie featureclass type etc).

Also the refreshActiveView no longer exists

Migrating from arcpy.mapping to ArcGIS Pro—ArcPy | ArcGIS Desktop 

which would be the normal go-to

Highlighted
New Contributor II

I'm trying it initially using only single symbol to match the symbology of the reference lyrx file so I didn't think the optional fields were required. Running it in immediate mode has the same effect as running it from the toolbox.

Unfortunate that refreshActiveView no longer exists. Previously in the desktop version I would use arcpy.mapping.UpdateLayer to update symbology of fields added to the TOC but that option too seems to have disappeared with the upgrade to Pro...

Reply
0 Kudos
Highlighted
New Contributor III

Hi Sean

I'm having the same problem with arcpy.ApplySymbologyFromLayer_management in ArcGIS Pro 2.1. You find any solution to the problem?

Reply
0 Kudos
Highlighted
New Contributor II

Not yet I'm afraid, but I'll keep trying to find a work around.

I tried also first converting the fc to a layer using arcpy.MakeFeatureLayer_management before adding to the map with addLayer and then using arcpy.ApplySymbologyFromLayer_management but the same thing happened: script completes successfully without error but no visible change is made. 

Reply
0 Kudos
Highlighted
New Contributor III

Same here, and I'm seeing the same problem with applying symbology to rasters too.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Sharing with Python‌ for broader awareness.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

an issue with symbology from layer was reported on Beta 2.2 but there is mixed reports, you might wish to report this to tech support

They give an associated case which might be useful

Esri Case #02133585 - Applying symbology from layer does not work correctly

Highlighted
Occasional Contributor III

Sean, based on the details it seems you are running into this defect:

BUG-000108497: Running Apply Layer Symbology as a script in the Python window does not update the symbology in the map.

  • A fix is in the Product Plan - please contact Esri Support Services if you'd like to have a case attached to the Defect log as this would allow you to more closely monitor the status of the defect.

This is a quirky one and based on my testing I'd say the most reliable way to fix this is to clear the Display Cache via Project > Options > Display.

You can fix this programmatically though:

  • Some have reported success with updateConnectionProperties
  • You can also insert a copy of the layer and remove the original using insertLayer and removeLayer
Highlighted
Occasional Contributor

Updating the connection (same input and output) worked for me, thanks!

datapath = 'C:\\temp'
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)
Reply
0 Kudos