Select to view content in your preferred language

Code for copying pie chart symbology using ArcPy not working

872
3
03-04-2021 02:14 AM
AntonDhooge
New Contributor

This is a question relating to the use of ArcPy in ArcGIS Pro 2.5.1. In essence, I am trying to write a piece code to copy the symbology from one pie chart to another. Both pie charts use the same fields and their underlying tables have exactly the same columns. It is very important that the size of the pie chart is determined by the value of one specific field. I already contacted ESRI, who told me that it is not possible to set the size of a pie chart by field value when the symbolized layer consists of a join between two layers (which is the case here). Hence, I wrote a piece of code to do the following:

  • Export the join layer to a gdb feature class
  • Load this feature class into the correct map inside the aprx file (it is now no longer a 'join' layer, hence solving the issue)
  • Apply the symbology from an example symbology layer to this newly imported layer

The code is shown below. It produces no errors. The pie chart layer is correctly imported. However, the ApplySymbologyFromLayer does not seem to do anything. The symbology_example layer from which the symbology should be copied, definitely has the correct symbology.

Can anyone help me figure out why the code is running without errors, but not symbolizing my layer correctly? I have also tries exporting the layer as a layer file, then importing it again. However, this gives me an error when trying to copy the symbology, as there is a type mismatch between goal and source layer.

import arcpy

# Assign input parameters to variables

name_GDB = arcpy.GetParameterAsText(0)
arcpy.env.workspace = arcpy.GetParameterAsText(1)
aprx = arcpy.mp.ArcGISProject("CURRENT")
arcpy.management.CreateFileGDB(f"{arcpy.env.workspace}",  name_GDB)

# make a map object ("piechart_map" refers to the map in which the pie charts are located)

versch = (aprx.listMaps("piechart_map")[0])

# Bullet 1 in list : write the joined layers to a gdb feature class
    # "Pie_Charts" is a lyr containing 3 individual sublayers to be formatted as pie charts, 
    # for the sake of argument, I reduced the code so that it only looks at one of these 3 
    # (code is analogous for 2 other pie charts)
out_path = f"{arcpy.env.workspace}\\{name_GDB}.gdb"    
versch_lyr = versch.listLayers("Pie_Charts")[0].listLayers()
Pie_Chart = arcpy.conversion.FeatureClassToFeatureClass(versch_lyr[0], out_path, "Pie_Chart")


# Bullet 2 in list :add the layer from gdb to map s.t. it can be symbolized correctly

path_PieChart = f"{out_path}\\Pie_Chart" #path to feature class
versch.addDataFromPath(path_PieChart) #load feature class

# Bullet 3 in list : Transfer the symbology from the example lyr (Symbology_example) to the imported 
# layer (Pie Chart)

symbologyFields = [["VALUE_FIELD","ZC_na_V","ZC_na_V"],
                   ["VALUE_FIELD","V_nsch","V_nsch"], 
                   ["VALUE_FIELD","V_sch","V_sch"]]
arcpy.management.ApplySymbologyFromLayer(versch.listLayers("Pie_Chart")[0], versch.listLayers("Pie_Charts")[0].listLayers("Symbology_example")[0],symbologyFields)
Tags (3)
0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

BUG-000116527: ApplySymbologyFromLayer in a Python script in ArcGIS.. (esri.com)

BUG-000108497: Running the Apply Layer Symbology tool as a script i.. (esri.com)

Not in current production plan  (not sure why, but have a look-see)

Alternate solution :  Close and reopen the project

 


... sort of retired...
0 Kudos
AntonDhooge
New Contributor

Dan, 

Thanks for you reply. I have tried closing and reopening the project on multiple occasions, which did not fix my issue.

Kr,
Anton

0 Kudos
DanPatterson
MVP Esteemed Contributor

Duplicate of BUG-000108497. Similar to another apply symbology tool bug. Will put this as a test case for when the issue is completed.

File your own case and Subscribe to the listed bugs, there may be wider issues and the number of people using pie chart symbology in python code is probably not a huge target audience, so "issues" can slide unnoticed for many update.

On that note, check the release notes for 2.7 and 2.6 which lists the bug fixes and enhancements

Release notes for ArcGIS Pro 2.7—ArcGIS Pro | Documentation

in case the issues is only indirectly related to ApplySymbologyFromLayer


... sort of retired...
0 Kudos