The goal is to publish a geoprocessing tool that:
Pretty simple, right? Not so much. No matter what we do, the hosted feature service layer(s) aren't drawing. Sometimes they do draw, sometimes they don't; most often not.
Can anyone spot an issue?
import arcpy, os, time
from datetime import datetime
start_date = arcpy.GetParameterAsText(0) #1761486465000
end_date = arcpy.GetParameterAsText(1) #1762004877000
report_type = arcpy.GetParameterAsText(2) #"Cultural Resources"
project_area = arcpy.GetParameterAsText(3) #"Project 1"
output_map_filename = arcpy.GetParameterAsText(4) #"aTest"
output_map_file = arcpy.GetParameterAsText(5)
lyr_wildcard = "CR*" if report_type == "Cultural Resources" else "NR*"
start_date_str = datetime.fromtimestamp(start_date / 1000).strftime("%m/%d/%Y") if str(start_date).isdigit() else start_date
end_date_str = datetime.fromtimestamp(end_date / 1000).strftime("%m/%d/%Y") if str(end_date).isdigit() else end_date
arcpy.env.overwriteOutput = True
def find_dt_field(lyr_datasrc):
flds_to_ignore = ["created_date","last_edited_date"]
for fld in arcpy.ListFields(lyr_datasrc,"*_date","DateOnly"):
if not fld.name in flds_to_ignore:
return fld.name
break
aprx = arcpy.mp.ArcGISProject(r"\\nafiles-our.domain.com\Weekly Report Map GP Tool dev.aprx")
m = aprx.listMaps("weekrep")[0]
lyrs = [l for l in m.listLayers(lyr_wildcard) if not l.isGroupLayer]
for lyr in lyrs:
if lyr.supports("DEFINITIONQUERY"):
dt_fld = find_dt_field(lyr.dataSource)
sql_query = dt_fld + " >= date '" + start_date_str + "' AND " + dt_fld + " <= date '" + end_date_str + "'"
lyr.definitionQuery = sql_query
lyr.visible = True
lyt = aprx.listLayouts("Weekly")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT", "LAYERS Map Frame")[0]
bookmark = m.listBookmarks(project_area)[0]
mf.zoomToBookmark(bookmark)
time.sleep(15) #let all the complex labels draw
output_file = os.path.join(arcpy.env.scratchFolder,output_map_filename)
pdf_export_format = arcpy.mp.CreateExportFormat("PDF",output_file)
pdf_export_format.georefInfo = False
pdf_export_format.resolution = 300
pdf_export_format.imageCompressionQuality = 100
arcpy.SetParameterAsText(5, lyt.export(pdf_export_format))
Can't test at the moment, but you might be able to open the project in Pro, then run the script. Then you could look at the definition queries applied to the layers and see if they are valid?
Might give an idea as to the issue. Sometimes it's just a small syntax issue and can sometimes take a bit to figure out the right coding to string a where clause together in the proper format.
R_
Edit to add:
It could also depend on what your data source is. If I set up a similar definition query in Pro on data from SQL Server through .sde connection file:
dt_fld >= '2025-01-02 00:00:00' And dt_fld <= '2025-12-02 00:00:00'But in the same project, if I connect to a FGDB feature class, the same query becomes:
dt_fld >= timestamp '2025-01-02 00:00:00' And dt_fld <= timestamp '2025-12-30 00:00:00'Neither of which have the 'date' in them. I remember seeing that before, so 'some' data source(s) must require it that way. Another reason to check the def queries in Pro itself after the script.
I've run into similar issues before with Layouts not refreshing properly using bookmarks/mapseries elements. Something that has worked for me in the past is to just force a CIM reload:
...
lyt = aprx.listLayouts("Weekly")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT", "LAYERS Map Frame")[0]
bookmark = m.listBookmarks(project_area)[0]
mf.zoomToBookmark(bookmark)
# Force a reload of the layout by overwriting the CIM
lyt.setDefinition(lyt.getDefinition('V3'))
... # You can remove the time.sleep here nowSee here for a discussion with the arcpy.mp team on this
I have called aprx.save() and looked at the applied def queries and they're all Ok.
I also tried the suggested layout 'refresh' and still have the problem. Sometimes, the HFL shows up in the legend, because it's visible, but none of the labels or features draw.
We have a similar GP tool we published, but that Pro project uses solely enterprise gdb data from our postgres instance..zero problems.
This problematic project has Fgdb's referenced, HFLs and enterprise gdb sources..all are referenced.when published.
But, we're barley at the publishing stage. It too doesn't draw the HFL points. Neither does the script run from Toolbox in Pro.
Only sometimes it works, all the same input parameters.
Bizzare
It's worth noting..all the problematic HFLs have very complex mapPlex labeling properties set. Stacking, lead lines, weights, ect.