0

I am having a strange issue that I can't seem to figure out. I am writing a simple script that will automate a map production workflow. In part of the script, I import a new feature class as a layer, change it's symbology to an existing feature layer in the map, and then change the extent of the map frame to the new feature layer. Here is a snippet...

# SPECIFY AREA OF INTEREST 

#SYMBOLOGY WORKS CORRECTLY WHEN I PROVIDE A TRUE PATH TO A FEATURE CLASS
AOI = r"G:\Users\Parcels.gdb\Arcadia"
#FAILS WHEN I PROVIDE GETPARAMETERASTEXT FOR THE AOI
AOI = arcpy.GetParameterAsText(0)

# SPECIFY AN EXISTING ARC PRO PROJECT FILE
aprx = arcpy.mp.ArcGISProject('G:/Users/Project.aprx')

# OBTAIN THE MAP AND LAYOUT FROM THE PROJECT
mp = aprx.listMaps()[0]
lyt = aprx.listLayouts()[0]

# OBTAIN ELEMENTS FROM THE LAYOUT THAT WILL BE MODIFIED
elements = lyt.listElements()
map_frame = elements[15]

# ADD PARCEL TO THE MAP
mp.addDataFromPath(AOI)

# GET LAYERS FROM THE MAP
map_layers = mp.listLayers()
AOI_lyr = map_layers[0]
TTRS_lyr = map_layers[1]

# APPLY TTRS SYMBOLOGY TO NEW AOI
arcpy.ApplySymbologyFromLayer_management(AOI_lyr, TTRS_lyr)

# CENTER THE MAP AROUND THE NEW AOI
map_frame.camera.setExtent(map_frame.getLayerExtent(AOI_lyr))

# EXPORT THE MAP LAYOUT AS A PDF
lyt.exportToPDF("G:\Users\Map.pdf",resolution=300)

When I test the script line by line in a development environment (i.e PyCharm, etc.) the output maps are correct in their symbology. However, when I run it as a script tool (with GetParameterAsText), the symbology of the new layer is not correct. Please see the picture below for an example. Could GetParamterAsText be throwing something off here? I am otherwise uncertain what the issue could be here. enter image description here

NOTE: I found similar posts that suggest this might be an existing bug in ArcGIS Pro, regarding the use of symbology in script tools. There must be some alternative solution?

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

Adding feature class and applying symbology in ArcGIS Pro using ArcPy?

Making arcpy.ApplySymbologyFromLayer_management() work on CURRENT project of ArcGIS Pro

1 Answer 1

1

I think the issue lies with your understand of the terminology. Throughout your description of the issue you refer to it as a Feature Class yet you clearly indicate you get a handle on the dataset from the map. If you are grabbing the data from within the map then you are accessing a Feature Layer.

The difference is that a Feature class is the data; think of it as a table of data. Nothing in that table says what symbology to use or even which rows are selected, that is the job of the Feature Layer (often referred to as simply layer). Now you understand the difference review your code:

You make an explicit call to load data from its path and by definition this has to be a Feature Class:

mp.addDataFromPath(AOI)

But you allow a user to supply an input using:

AOI = arcpy.GetParameterAsText(0)

Well what if that input the user selected the layer from within the map, that's a layer not a feature class.

Also another potential problem is where the layer you load using addDataFromPath() is inserted into your table of contents. Looking at the help file it says that placement is based upon a set of rules. Now look at your code:

AOI_lyr = map_layers[0]

You have hard wired the index position to zero. Well if you have any line or point data in your map a polygon layer would get inserted beneath those.

1
  • I have changed the language of my post to better reflect the issue that I am having. In my script, I import a feature class into the project but I also indicate this as a layer AOI_lyr = map_layers[0] and use this as an input to arcpy.ApplySymbologyFromLayer_management without any success Commented Aug 19, 2021 at 13:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.