Select to view content in your preferred language

Legend issues with notebook script

132
3
3 weeks ago
JohnsonAbby
Emerging Contributor

In the following script, it outputs the maps correctly but the legend keeps repeating itself between the first few county legends? I've attached an example for Sulfate by town-range and you can see the legend shows Arsenic by county.

# Make Web Display Current Map Set
# make sure only WI_Counties layer on
import sys
import arcpy
import os
import arcpy.mp as Map

# set these control parameters for the map production:
xDate = "January - 2025" # current data set being displayed (above legend on layout)
xPrint = "no" # "yes"/"no" for print control
xFormat = "pdf" # can be either pdf or png; reset the map document to a borderless png version if appropriate
xRes = 300
pdfBook = "Viewer_MapBook_January_2025" #enter name of mapbook; disabled when format set to png
xDir=r"C:\Projects\Well Water Quality Viewer\make_web\Display_Maps" #dir for the output files
aprxFile=r"C:\Projects\Well Water Quality Viewer\make_web\Display_Maps\Display_Maps\Display_Maps.aprx" #mxd document
# set maps to a list of desired maps (in the order requested for mapbook)
maps = ['Alkalinity-Co','Arsenic-Co','Arsenic-Max-Co','Atrazine Family-Co','Bacteria-Co','Chloride-Co',
'Conductivity-Co','Copper-Co','Iron-Co','Lead-Co','Manganese-Co','Nitrate-Co','Nitrate Exceedence-Co',
'pH-Co','Saturation Index-Co','Sulfate-Co','Total Hardness-Co',
'Alkalinity-TR','Arsenic-TR','Arsenic-Max-TR','Atrazine Family-TR','Bacteria-TR','Chloride-TR',
'Conductivity-TR','Copper-TR','Iron-TR','Lead-TR','Manganese-TR','Nitrate-TR','Nitrate Exceedence-TR',
'pH-TR','Saturation Index-TR','Sulfate-TR','Total Hardness-TR',
'Alkalinity-S','Arsenic-S','Arsenic-Max-S','Atrazine Family-S','Bacteria-S','Chloride-S',
'Conductivity-S','Copper-S','Iron-S','Lead-S','Manganese-S','Nitrate-S','Nitrate Exceedence-S',
'pH-S','Saturation Index-S','Sulfate-S','Total Hardness-S']
# ---------------------------------------------------------------
arcpy.env.overwriteOutput = True
aprx = Map.ArcGISProject(aprxFile)
layout = aprx.listLayouts('PW_WEB_MAPPER')[0]
ms = layout.mapSeries
noteSet = layout.listElements("TEXT_ELEMENT", "Data_Set")[0]
noteSet.text = xDate
legendList = aprx.listMaps("Layers")[0]
dfList = aprx.listMaps("Layers")[0]

if xFormat != "pdf":
xPrint = "no"
if xPrint == "no":
mapPrint = "N"
else:
mapPrint = "P"
if xFormat != "pdf":
pdfBook = "no"
if pdfBook != "no":
pdfDoc = Map.PDFDocumentCreate(os.path.join(xDir, pdfBook)+".pdf")

#main loop
for xmap in maps:
curLayer = dfList.listLayers(xmap)[0]
#curLegend = legendList.listLayers(xmap)[0]
curLayer.visible = True
#curLegend.visible = True
Xport = os.path.join(xDir, xmap)
if xFormat == "pdf":
layout.exportToPDF(Xport,resolution=xRes)
if pdfBook != "no":
pdfDoc.appendPages(Xport+".pdf")
else:
layout.exportToPNG(Xport,resolution=xRes)
if mapPrint == "P":
Map.PrintMap(aprx)
print ("Exported: ", xmap)
curLayer.visible = False
curLegend.visible = False

if pdfBook != "no":
pdfDoc.saveAndClose()
del pdfDoc
del aprx
print ("Script Complete")

0 Kudos
3 Replies
JeffBarrette
Esri Regular Contributor

Hello, I can't identify the issue from the picture that is attached.  What is "repeating itself"?

Jeff - arcpy.mp team

0 Kudos
JohnsonAbby
Emerging Contributor
The legends for Alkalinity-co, arsenic-co and chloride-co keep repeating. There are 14 parameters each for county, town-range and section. So for sulfate it’s showing chloride rather than going through the list of legends. Does that help?
0 Kudos
JohnsonAbby
Emerging Contributor

Here are a couple screenshots if that helps. The legends are part of the Layers map frame. This was a layout from ArcMap that I imported into Pro and am trying to update my script. 

0 Kudos