Skip navigation
All Places > ArcGIS Workforce > Blog > 2020 > March
2020

Welcome to another blog post that will teach you how to automate tasks for Workforce using ArcGIS API for Python. The last blog in this series was about using Python to set up and configure a Workforce project. We covered how to import workers, create assignments based off a feature layer, and assign work based on location. You can check it out here.

 

This blog will teach you three different ways to monitor assignments using ArcGIS API for Python. It can be overwhelming to keep up with all of the assignments within your project, especially when you have multiple workers. The following scripts are designed to help you monitor work efficiently.

 

You’ll learn how to create a dashboard for your project using ArcGIS Dashboards, how to monitor the status of assignments using Slack, and how to verify a worker’s location at the time they modified an assignment.

 

Overview

  • Scenario
  • Install ArcGIS API for Python
  • Download Workforce scripts
  • Create a dashboard
  • Monitor assignments with Slack 
  • Check assignment completion location
  • Additional resources

 

 

Scenario

A tree inspection company is using Workforce to assign inspections to workers, and there are 24 open assignments throughout the city of Atlanta. You, as the inspection lead, want an easy way to monitor the progress of these assignments.

 

Note: This scenario uses the project created in the first blog; however, you can use any Workforce project that you own to follow along with these exercises.

 

Install ArcGIS API for Python

 

To automate and script Workforce projects, you must install the prerelease of ArcGIS API for Python 1.8.3. 

 

ArcGIS API for Python 1.8.3 is planned to release in Q4 of 2020. The following command allows you to download the prerelease of this version that contains the Workforce module necessary to automate and script projects that are enabled for offline use. This version also supports automating and scripting Classic projects. 

 

First, review the requirements for installing ArcGIS API for Python.

 

Then, Install ArcGIS API for Python by running the following command in either a Python script or Python console:

 

conda install -c esri/label/prerelease -c esri arcgis==1.8.3

 

Download Workforce scripts

 

Next, you need to clone or download the GitHub repository that contains the Python scripts for Workforce. Once it is downloaded, navigate to the “scripts” folder in either the terminal or the command prompt.

 

cd C:\Users\user\Desktop\workforce-scripts\scripts

 

Install the required libraries using Python’s default package installer, pip:

 

pip install –r requirements.txt

 

Once these are installed, all of the available Python scripts for Workforce are ready to use.

 

Create a dashboard

 

ArcGIS Dashboards is a web application that allows you to monitor your operations in real-time. While the Workforce web app allows dispatchers to edit and view assignments, a dashboard provides a read-only alternative for supervisors and users. A dashboard displays key statistics, allowing you to understand the progress of your project at a glance.

 

You can create a dashboard for your Workforce project with just one command.

 

Note: If you’d like to build a dashboard for your Workforce project without using the Python API, check out this blog.

 

Have the following information ready to use as parameters for the script:

  • Username
  • Password
  • Organization URL (<org>.maps.arcgis.com)
  • Project ID 

 

Note: If you are working with a Classic project (a v1 project), the Project ID is the item ID of the project item. If you are working with projects enabled for offline use (a v2 project), the Project ID is the item ID of the Workforce feature service. Regardless of what version you are working with, the Project ID can be found in the URL of your project. 

For a complete list of parameters, see the readme for this script.

 

Run create_ops_dasbhoard.py in either the terminal or command prompt. Be sure to swap the default parameters with your own.

 

python create_ops_dashboard.py -u <username> -p <password> -org https://arcgis.com -project-id <project_id> --light-mode

 

Once you run this script, you’ll see a new dashboard has been shared with the group containing your Workforce project. Here’s what the dashboard for the Tree Inspections project looks like:

 

Dashboard for the Tree Inspections Workforce project.

 

The script creates a dashboard that shows worker information, assignment information, and a copy of the Dispatcher map. The dashboard will update to reflect changes in assignment completion and worker status in real time. This is great to have displayed in the office to quickly note the progress of your operation.

 

Monitor assignments with Slack 

 

Slack is an office communication tool that allows employees to stay connected with one another during the workday. By adding a webhook to a channel on Slack, you can receive information and updates from outside web sources, including Workforce.

 

The assignment monitor script connects your Workforce project with a webhook so that you will receive a message in Slack every time an assignment is completed. Anyone who joins the Slack channel that contains the webhook will receive these notifications.

 

Tip: This script can also be configured to send notifications to your email account. See the assignment_monitor readme for more information. 

 

To add a webhook to a Slack channel, you’ll first need to create a new app in Slack. Visit the Your Apps page in the Slack API and click Create New App.

 

Create new app button

 

Give your app an appropriate title. This one is called “Tree Inspections”. You’ll also need to select the Slack Workspace you want your app to belong to.

 

Create a Slack App window.

 

Once your app is created, you can create a webhook. Go to the Basic Information page for your app and click Incoming Webhooks.

 

Basic Information page for Building Apps for Slack.

 

Make sure that Activate Incoming Webhooks is toggled on. Then click Add new Webhook to Workspace.

 

Note: If you aren’t the manager for your workspace, you will need to request permission to add a webhook.

 

Web page for activating incoming webhooks.

 

A window appears requesting permission for the app to access your workspace. Select the channel you want your app to post to and click Allow.

 

Tip: Since this webhook will send a message every time an assignment is completed, you may want to create a channel specifically for it.

 

Window requesting permission for the app to access your workspace.

 

You have successfully created a new web hook. Copy the Webhook URL, shown below, and paste it somewhere you can easily access. You will need it for running the Python script.

 

Incoming Webhooks page where the Webhook URL is located.

 

If you go to the Slack channel you connected the webhook to, there will be a message saying a new app was integrated.

 

Assignment monitor channel in Slack.

Now that you have created a Slack webhook and connected it to your workspace, you are ready to run the Python script.

 

First, in the Assignment Monitor folder, edit the config file. Edit the file to include your project ID, slack webhook URL, organization, and log-in credentials.

 

Config file opened in Notepad.

Once you’re done editing the file, save it. You are now ready to run the Python script. Open the terminal or command prompt and make sure your file path points to the scripts folder:

 

cd C:\Users\user\Desktop\workforce-scripts\scripts

 

Then run the assignment monitor script:

 

python assignment_monitor.py

 

The script will check for newly completed assignments, and a message will be sent to your assignment monitor channel on Slack when one is found. To test this, log in as one of your workers on the Workforce mobile app and complete an assignment.

 

A message will appear in Slack that looks like this:

 

Assignment completion message in Slack.

 

This message shows who completed what assignment and where it is located. It also shows that the worker left a note saying the assignment needs attention right away.

 

The script will continue checking for completed assignments every 5 seconds. To stop the script, press Ctrl+c while in the terminal or command prompt.

 

Tip: This script can be modified to run once as opposed to looping infinitely. It can be called every so often (i.e. once per minute) using a task scheduler such as Windows Task Scheduler or Cron.

 

For more information, see the assignment_monitor readme.

 

Check completion location 

 

The check_edit_location script uses data from Tracker for ArcGIS to see if a worker edited a feature without visiting its location. Assignments in Workforce, features in Collector, and surveys in Survey123 can all be checked using this script.

 

If you want to follow along with this exercise, find a Workforce project that has workers who have enabled location tracking.

 

Note: Location Tracking must be enabled for your organization to use this script. You must either be an administrator or a track viewer who can view the tracks of each worker whose work you'd like to verify.

 

For example, Sharon, a tree inspector, completed three assignments this morning (shown below).

 

Assignments that Sharon completed shown on a map.

 

The check_edit_location  script is used to verify that Sharon visited the location of each completed assignment.

 

First, clone or download the Github repository that contains the Python scripts for Tracker.

 

Next, open the terminal or command prompt and navigate to the scripts folder:

 

cd C:\Users\user\Desktop\tracker-scripts\scripts

 

Create the virtual environment with the correct dependencies:

 

conda env create --file environment.yml

 

Activate the environment:

 

conda activate tracker-scripts

 

Here is the check_edit_location script:

 

python check_edit_location.py -u username -p password -org https://arcgis.com -workers <worker1>, <worker2> -field-name completedDate -time-tolerance 10 -distance-tolerance 100 -layer-url <layer-url> -tracks-layer-url <tracks-layer-url>

 

Before you run it, you will want to make sure you edit the parameters as follows:

 

  • Provide your username, password, and AGOL organization to connect to your content.
  • List the username for each worker whose assignment and location you want to check.
  • Set the field name to the date field within the feature layer you want to verify. To check for assignment completion, use completedDate as input for this parameter.
  • Set a time tolerance that provides a range around the time when the assignment was completed. The script defaults to 10 minutes.
  • Set a distance tolerance that provides a buffer around the location where the assignment was completed. The script defaults to 100 meters.
  • Set the minimum accuracy required when querying worker locations. The script defaults to 50 meters.
  • Provide the complete feature service URL for the assignments layer. For example:
    • https://services.arcgis.com/a910db6b36ff4066a9d4131fccc3da9b/arcgis/rest/services/assignments_ad9af2fc00314fa49ce79ec7d7317acc/FeatureServer/0
  • Enter the complete tracks layer URL if there is a specific track view you want to utilize. The script defaults to the tracks layer in your location tracking service. For example:
    • https://locationservicesdev.arcgis.com/US6xjA1Nd8bW1aoAarcgis/rest/services/5bfd7a01b6d4b698df17af205b8dbef_Track_View/FeatureServer/0

 

Once the parameters are set, run the script.

 

For this example, the script was run with the default parameters to see if Sharon was within 100 meters of the tree inspection assignments at the time they were completed. The script returned the following message:

 

The user sharon_user who last edited the feature with OBJECTID 22 was potentially not within the distance tolerance when updating the field completedDate

 

The script only returns assignments that are invalid. This means that Sharon did visit the location of two of the assignments she completed. However, there is one tree inspection that she may not have visited.

 

Next, Sharon’s tracks were added to the assignments layer to see if the script’s output made sense.

 

Sharon’s tracks and completed assignments displayed on a map.

 

Sharon’s tracks, shown in pink above, show that she visited assignments 21 and 20, but did not visit assignment 22. The script successfully reported that, though assignment 22 was completed, Sharon potentially did not visit the site of the tree inspection.

 

For more information about this script, see the check_edit_location readme.

 

Additional resources

This blog post taught you how to use ArcGIS API for Python to monitor your Workforce projects. See the following blog posts to learn about other ways to automate tasks for Workforce:

Feel free to test out all of the Workforce scripts in the Github repoTo learn more about the Workforce module within the Python API, see Managing Workforce for ArcGIS Projects.

Welcome to the first in a series of blog posts that will teach you how to automate key workflows for Workforce for ArcGIS.

 

The Workforce module within ArcGIS API for Python makes managing Workforce projects a simple and efficient task. We are constantly creating new scripts that simplify app-based workflows into just a few lines of code.

Whether you are setting up your first Workforce project, cleaning out a project with hundreds of old assignments, or trying to find new ways to improve your organization’s efficiency – the Python API will be a powerful addition to your geospatial arsenal.

 

This first blog is a step-by-step tutorial that will teach you how to configure a Workforce project and create assignments.

 

Overview

  • Getting started
  • Install ArcGIS API for Python 
  • Scenario
  • Download Workforce scripts
  • Import workers from a CSV file
  • Add assignment types
  • Create assignments based on an existing feature layer
  • Assign work based on location
  • Additional resources 

 

Getting started

 

If you would like to complete each task alongside this exercise, first do the following: 

 

 

Note: If you don't have an ArcGIS account, you can sign up for a free trial

 

Scenario

 

In this scenario, the city of Atlanta has identified 24 trees that are at risk of falling. These trees are in heavily developed areas, so it’s crucial that they be professionally inspected. The city has hired a tree risk assessment company to perform these inspections.  

 

You will use ArcGIS API for Python to configure a Workforce project for the company. You will import the company’s workers, create assignments based on a feature layer containing the trees, and assign inspections to workers based on the zone that each worker oversees.

 

Install ArcGIS API for Python

 

To automate and script Workforce projects, you must install the prerelease of ArcGIS API for Python 1.8.3. 

 

ArcGIS API for Python 1.8.3 is planned to release in Q4 of 2020. The following command allows you to download the prerelease of this version that contains the Workforce module necessary to automate and script projects that are enabled for offline use. This version also supports automating and scripting Classic projects. 

 

Install ArcGIS API for Python by running the following command in either a Python script or Python console:

 

conda install -c esri/label/prerelease -c esri arcgis==1.8.3

 

Download Workforce scripts

 

Next, you need to clone or download the GitHub repository that contains the Python scripts for Workforce. Once it is downloaded, navigate to the “scripts” folder in either the terminal or the command prompt. I am using a Windows operating system for this example. 

 

cd C:\Users\user\Desktop\workforce-scripts\scripts

 

Install the required libraries using Python's default package installer, pip: 

 

pip install -r requirements.txt

 

Also install the shapely library. This will allow us to spatially assign work later on. 

 

conda install shapely

 

Once these are installed, all of the available Python scripts for Workforce are ready to use.

 

Import workers from a CSV

 

The import_workers Python script allows you to import workers directly from a CSV file. This script is especially useful for importing a large number of workers into a project. Rather than input your workers one by one in the Workforce app, you can run this single line of code instead.

 

We will use the import_workers script to import a CSV file of tree inspectors into this scenario's Workforce project. This is the CSV file that you downloaded earlier:

 

CSV file of tree inspectors

 

There are 5 tree inspectors that will be imported into the project. Their name, working status, title, contact number, and user ID will all be passed through the script.

 

Open this file and edit the name and userID columns with the information of at least four workers within your organization. Save your changes.

 

Note: This script has many options allowing the user to specify the names of each column within the CSV. Check out the import_workers readme for more information.

 

In the terminal or command prompt, run the import workers script:

 

python import_workers.py -u <username> -p <password> -org https://<org>.maps.arcgis.com -name-field name -status-field status -user-id-field userId -log-file log.txt -csv-file ../sample_data/tree_inspectors.csv -project-id <project id> -title-field title -contact-number-field contactNumber

 

Make sure you update the following fields with your own information:

  • Username and password
  • Organization
  • CSV file
  • Project ID (from the blank project you created) 

 

Note: If you are working with a Classic project (a v1 project), the Project ID is the item ID of the project item. If you are working with projects enabled for offline use (a v2 project), the Project ID is the item ID of the Workforce feature service. Regardless of what version you are working with, the Project ID can be found in the URL of your project. 

 

Once this script has been run, check that your tree inspectors have been successfully added to your project (shown below). 

List of added tree inspectors in the Workforce web interface

Add assignment types 

 

Note: We will be using Jupyter notebooks to complete the remaining tasks within this workflow. Notebooks are useful for visualizing data and running code step by step. Either open a blank Jupyter Notebook or follow along with the notebook you downloaded earlier.

 

First, import the ArcGIS API for Python library. 

Code for importing the ArcGIS API for Python

Next, connect to your GIS and fetch the project using its project ID. 

Code for connecting to your GIS and fetching the project

Once this set-up is complete, you can start performing tasks specific to your project. You’ll first want to add assignment types to the project that are relevant to tree inspections.

Code for adding assignment types to tree inspections

After you run this code, you can check that the assignment types have been successfully added to the project (shown below).

Added assignment types in Workforce web interface

 

Create assignments based on an existing feature layer

 

The city of Atlanta has provided a feature layer containing the trees that are at risk of falling. Using the Python API, you can create an inspection assignment for each feature within this layer.

 

First, import the datetime library. Datetime allows us to pass the current time into the assigned date field.

Code for importing the datetime library

Then you can fetch the layer containing the trees, query it, and display it on a map.

 

Workforce stores assignments in the WGS84 Web Mercator projected coordinate system, so let’s ensure that the returned geometries are using this spatial reference.

 

Here is the item ID for the trees feature layer: 88495d3b613a41f4a70b9d61ef979b34

 

Code for querying the trees layer and displaying it on a map

The Atlanta Trees layer is displayed. 

 

Now you will create 24 “Inspect tree” assignments: one for each tree. You will loop through the trees feature layer to batch add assignments using the “Inspect tree” type we created in the previous section.  

Code for creating an assignment for each tree feature within the layer

To ensure that a new assignment was created for each tree, display the assignments layer on a map.

Code for displaying assignments layer on a map.

Your project now has 24 tree inspection assignments.

 

Assign work based on location

 

You have your tree inspectors and your assignments. Now it’s time to assign work. You will assign the inspections to four workers. Each inspector oversees a specific work zone in Atlanta, so they will only inspect trees that fall within their respective zone. You will use the work zone feature layer to assign each of the 24 tree inspections to a set of four zones.

 

We’ll import some modules from within the arcgis library to make typing these classes easier.

 

Code for importing the Geometry and WebMap modules.

Next, let’s get the Inspection Zones feature layer and display it on a map.

 

Here is the item ID for the Inspection Zones feature layer: ac044c7a2c94401eaeb1b215200feb57

 

Code for displaying the zones layer on a map

Add your assignments layer to the map so you can visualize how the assignments are distributed within the zones.  

Code for adding assignments layer to map of work zones

Next, you’ll create a spatially enabled data frame from the zones layer and grab all of the unassigned assignments.

Code for creating a spatially enabled data frame from the zones layer and grabbing all of the unassigned assignments.

Assign tree inspections to workers within each of their respective zones.

  • Trees in Zone 1 should be assigned to Josh
  • Trees in Zone 2 should be assigned to Jane
  • Trees in Zone 3 should be assigned to Nirav
  • Trees in Zone 4 should be assigned to Sharon

The following code will assign each worker to the trees that fall within the work zone they are responsible for.

Code for assigning each worker to the trees that fall within the work zone they are responsible for.

Run this code and then create a map with the updated assignments layer to make sure everything was assigned properly. If you click on any of the features, the status should read “Assigned”.

Code for creating a map with the updated assignments layer.

Now, if you open your Workforce project, you can view all of your updated assignments. If you sort by assignee, you’ll see that inspectors were only assigned to work for their specific zone. You’ll see that only inspections in Zone 2 were assigned to Jane.

Assigned tree inspections in the Workforce web interface.

You have successfully set up a Workforce project and assigned work using ArcGIS API for Python!

 

Additional resources

This blog post taught you how to use ArcGIS API for Python to configure your Workforce projects and assign work. See the following blog posts to learn about other ways to automate tasks for Workforce:

Feel free to test out all of the Workforce scripts in the Github repoTo learn more about the Workforce module within the Python API, see Managing Workforce for ArcGIS Projects.