Tutorial: Write Arcade expressions in the QuickCapture designer

935
0
12-01-2023 07:31 AM
BrandyPerkins
Esri Contributor
2 0 935

In the November 2023 release, ArcGIS QuickCapture has a new integrated Arcade editor in the web designer. You can build expressions that calculate attributes for data collected with version 1.18 of the QuickCapture mobile app. Using Arcade expressions in QuickCapture projects is applicable to online and offline data collection workflows.

The following steps provide guidance for writing your own expressions in the QuickCapture designer. This example populates a field based on a point-in-polygon query that gets the name of a region in which a point feature was captured.

You can write expressions to return a value for the capture field using Arcade variables ($feature, $layer, and $map) and functions. To refer to layers in a web map using the $map variable, the web map must be saved as the project map. You'll start by creating a map for your project using a layer from ArcGIS Living Atlas of the World.

  1. Create a web map in Map Viewer by adding the World Countries Generalized feature layer from Living Atlas.

    Living Atlas layer selected for the mapLiving Atlas layer selected for the map
  2. Create a Quick Capture project using the Run, Walk, Have Fun template and change the project map to use your new web map with the Living Atlas layer of world boundaries.

    Map changed in the projectMap changed in the project
  3. Click the Take a photo button, click the Data tab, and click the target layer’s info icon to open the item page so you can add the field.

    Info button for the target feature layerInfo button for the target feature layer
  4. In the Details section of the item page, click FunRun to open the source layer’s item page.

    Item details with link to open source layerItem details with link to open source layer
  5. Click the Data tab, switch to Fields view, and click Add. Create a String field named Country.

    Add Field window for new Country fieldAdd Field window for new Country field
  6. Return to the QuickCapture designer, save the project, and refresh the browser to see the new field on the Data tab. Scroll to the Country field and select Arcade expression from its options. Click Select expression and click Create new.

    Arcade expression chosen as input for the Country fieldArcade expression chosen as input for the Country field
  7. In the Arcade window, type a label for the expression, which will appear in the Arcade expression list of options so you can use it in other fields or buttons for the project. Click the Profile variables (x) tab and click the arrow next to $map to access its layers.

    Expression label and Profile variables tab with arrow button selected for $mapExpression label and Profile variables tab with arrow button selected for $map

    TIP: You can use the interactive panel to insert snippets and select layer and field references from the project’s map and layers. The Arcade window provides in-panel help for syntax and examples, Intellisense for suggestions as you type, and options to validate and format your expressions. 
      
  8. Click the arrow next to World Countries Generalized (the layer you added to your map) and scroll through the field names—you’ll use the COUNTRY field. Above the Field names section, click the FeatureSetByName function to insert the snippet.

    FeatureSetByName selected for the map to insert the snippetFeatureSetByName selected for the map to insert the snippet
  9. Click the Run button to test it out. This returns all the features in the map layer (a feature set).

    Run button and test result for FeatureSetByNameRun button and test result for FeatureSetByName
  10. Edit the expression to add a third parameter for [“COUNTRY”], so it only returns the COUNTRY field values for the features in the layer. Click Run to test the expression.

    COUNTRY field name added to expression and new test resultsCOUNTRY field name added to expression and new test results
  11. To perform an intersect between the location of the point feature captured in the mobile app ($feature) and polygon features in the map’s countries layer, edit the expression as follows:

 

// Create a feature set using the 'World Countries Generalized' layer in the map 
var countries = FeatureSetByName($map, "World Countries Generalized", ["COUNTRY"]);
// Intersect the current location with the countries and
// get the first country
var country = First(Intersects($feature, countries));
// If the current location does intersect a feature,
// return the name of the country. Otherwise, return null
if (!IsEmpty(country)) {
  return country["COUNTRY"];
} else {
  return null;
} ​

 


Note: When your expression references a polygon layer by name, you must include that layer in the web map that’s configured for your QuickCapture project. Ensure that the layer name and field name in your expression matches what’s used in the web map layer. To use this expression for point-in-polygon calculations with your own data, swap the “World Countries Generalized” layer name with the layer name from the map in your project and swap “COUNTRY” with the field name from the layer that you want to return.

12. Click Create. Save the project and download the project to the mobile app. (You can’t test this expression in the designer until the target feature layer has at least one record because it uses the first feature to run the test.)

13. In the mobile app, select the Take a photo button to capture a feature and photo. Once the record is processed and the expression runs, select Send to send the feature. (QuickCapture runs the expression before sending the feature.)

14. To verify the collected data, return to your project in the designer and click the info icon for the Photos target feature layer to open its item page.

Take a photo button selected in the project and info button selected for its target feature layerTake a photo button selected in the project and info button selected for its target feature layer

15. Click Open in Map Viewer to verify that the new feature’s pop-up shows that the Country field is populated.  (You may need to configure the pop-up to turn on the field.)

Sample web map pop-up for a captured feature with the Country field populatedSample web map pop-up for a captured feature with the Country field populated

This example uses geographic regions, however, it would work for any polygon layer, such as land parcels, vegetation types, population count, and so on. For more information about using Arcade in QuickCapture, plus additional example expressions to try, refer to the blog post, Streamline field data collection in QuickCapture using Arcade. Here’s a list of the common use cases included:

  • Calculate a value by referencing other fields
  • Get values from other layers in the map (point in polygon queries)
  • Get attributes from a nearby feature