Creating and scheduling scripts with the ArcGIS API for Python
If you’re looking to create a script and schedule it in ArcGIS Online, the ArcGIS API for Python and Notebooks are the way to go. Say a client needs to survey a plant species. Specifically the Pink Lady Slipper (Cypripedium acaule Ait.); an orchid native to eastern North America. The plant has a bloom season between May and June, so the best time to survey for the species is in that time period as they are easier to spot and less easy to mistake for a similar plant species.
During the surveying season, the team would like to aggregate the data into a report that is run once a week. The report is sent in an email that contains all the data collected the previous week. This will allow the team lead to perform a QC check on the week’s data; review the covered areas, review the data for any incomplete entries, and plan for the following week’s data collection efforts.
Infomaptic Reports
When a quick script no longer cuts it and you need to generate professional-looking reports for your ArcGIS Online and ArcGIS Enterprise feature data
Infomaptic Reports
So why choose the ArcGIS API for Python and Notebooks for this task? ArcGIS Notebooks have an integrated scheduling feature called Tasks. It provides an easy way to set up a reoccurring event that will run the script in the same environment that you write it in.
Creating a task is straightforward. You name the task with the option of providing an event trigger that will initiate the script, such as a new feature being added to a dataset. For the purpose of a weekly email, this is not needed so it can be left blank.
The next step is setting the time parameters: what date the task should start executing, how often the task should repeat (by the minute, hour, day, week, or month), what time the task should execute, and for how long should the scheduled task repeat. This team would want the emails to be sent every Friday around 8am after the fieldwork has been conducted for the week in order to aid in reviewing the data collected previously and to prepare for the next week’s data collection efforts.
To create a Notebook you will need an ArcGIS Online account and proper user permissions. The notebooks are stored in contents along with any layers and maps used to store and visualize the collected data. To get started import any needed Python libraries and connect to the feature layers and tables where the data is being collected.
# Run this cell to connect to your GIS and get started:
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
from arcgis.features import FeatureLayerCollection
gis = GIS("home")
With this assignment, the data needs to be added to the main string with HTML styling and string formatting through a For Loop to allow for multiple data entries to be displayed in the email body. This string will then be fed into a Send Email function that sends the email report formatted in the body of the mail.
masterstring = "<html><body><b>{0} through {1} </b><br>".format(mondayDate, currentDate)
for flg in flagIDs:
surveyorQuery = surveyLayer.query(where="FlagID = '{0}'".format(flg), out_fields='CollectionDate, Description, Habitat, FlagID, Surveyor')
collectionDate = surveyorQuery.features[0].get_value("CollectionDate")
dateformat = readable_date(collectionDate)
description = surveyorQuery.features[0].get_value("Description")
habitat = surveyorQuery.features[0].get_value("Habitat")
flagID = surveyorQuery.features[0].get_value("FlagID")
surveyor = surveyorQuery.features[0].get_value("Surveyor")
masterstring = masterstring + "<b><u>{0} </u></b> <br> <i>  Date:{1} --> Surveyor:{2} --> Habitat:{3}<br></i>  Description: {4} <br>".format(flagID, dateformat, surveyor, habitat, description)
masterstring = masterstring + "</body></html>"
And with that, you have a script created using the ArcGIS API for Python in ArcGIS Notebooks, scheduled to run as a weekly task. The below screenshot is the report generated from running the scripts on our sample orchid dataset.
We have shared the entire script with you so you can create your own automated scheduled ArcGIS Notebook tasks using ArcGIS API for Python. You can find it on our ArcGIS Online.
If you found this content useful, please leave a comment below to let us know.