Hello,
I am creating an automatic mapping process using Python and am running to a really interesting issue that seems to be shared among some other users with no apparent solution.
My map document contains one layout with every layer I need to toggle on and off and then export to a PDF, however the legend does not update when a new layer is turned on and the map is exported (the layer turns on and draws fine). I've ensured that every legend setting is set to automatically update (legends in Pro /should/ be automatically dynamic) and show only what's visible in the map extent. Additionally, when I isolate one map in particular in my series, it seems to print just fine with the updated legend.
When running my script I am running it as a standalone process in the IDLE environment. When I run the script within the actual Python window it works like a charm, but there is some kind of disconnect happening when running in it's IDLE standalone instance.
I'm currently running ArcGIS Pro 2.6 most recent. If anyone has any idea as to why this is happening that would be great.
#My code below (Omitting import functions as they are a given, correct, and not giving any issue.
aprx = arcpy.mp.ArcGISProject(r"C:\Users\JohnDoe\Documents\ArcGIS\Projects\PythonMaps\Python Maps.aprx")
lyt = aprx.listLayouts("Main Layout")[0]
m = aprx.listMaps("Main Map")[0]
def test():
print("Printing FEMA Map...")
#Turns on BFE and Flood Zone Layers
for elm in lyt.listElements('TEXT_ELEMENT', 'Map Title'):
elm.text = "FEMA"
for elm in lyt.listElements('TEXT_ELEMENT', 'Source Text'):
elm.text = "Source: Business Inc., FEMA 2018"
time.sleep(10) #Layer load/drawing time
for lyr in m.listLayers():
if lyr.longName == "County Name\FEMA":
lyr.visible = True
#Prints Layout
lyt.exportToPDF(PDFPath + "\\" + "FEMA", resolution = 300) #Exports to 300 DPI PDF #PDFPath Variable from User input
#Turns off BFE and Flood Zone Layers
for lyr in m.listLayers():
if lyr.longName == "County Name\FEMA":
lyr.visible = False
time.sleep(5) #Layer drawing off time
aprx.save()
print("FEMA Map Complete...")
Note: I have my maps sorted by group layers of counties within my one layout (hence reference to the lyr.longName property. Since county printing is done by user input, each process is defined by individual functions. Below in my conditional segment for determining which function to run if it helps...
PDFPath = input("Please enter output PDF directory: ")
mapset = input("What county is the exhibit property in? ")
if mapset == "County 1":
county1()
elif mapset == "County2":
county2()
elif mapset == "County 3":
county3()
elif mapset == "County 4":
county4()
elif mapset == "County 5":
county5()
elif mapset == "Test":
test()
else:
print("Checking for county availability...")
time.sleep(5) #acts like script is checking available counties to export map series (just for show)
print("County not available for process...")
Additional Note: When I run my maps in the county function the layer legend issue is happening, but when I run my maps in my test function, the issue is resolved. Both functions are written the exact same way...
Update for the Additional Note Above: The issue seems to be when multiple maps are attempting to be exported and not just one.
Did you ever resolve this issue? I'm seeing the same issue when producing maps in a similar way using ArcGIS Pro 2.6
In case anyone comes across this issue, I found a solution that worked for me. I'm currently using ArcGIS Pro 3.3.2 and running a python script in IDLE that exports a PDF of a layout after toggling a set of layers on and off.
Toggling the legend's syncLayerVisibility on and off before exporting the PDF (and after all the layer visibilities had been changed) worked to refresh the legend:
pLegend = pLayout.listElements("LEGEND_ELEMENT","Legend")[0]
pLegend.syncLayerVisibility = False
pLegend.syncLayerVisibility = True