ArcGIS Blog

Mapping

ArcGIS Online

Catch More Waves: Use Arcade Expressions to Calculate Fields in Forms

By Emily Garding

You work hard, you play hard. You spend time and energy managing large complex datasets, then head to one of your favorite spots to unwind. Now you can get even more free time by prepopulating fields in forms in Map Viewer. In this article, we’ll show you how to use an Arcade expression to automatically pull attributes from one layer into another. We’ll walk you through authoring the form, creating an expression, provide you with a code snippet, and explore a map so you can take it for a spin. Let’s dive in.

 

FeatureSet

Arcade is a portable, lightweight, expression language used across the ArcGIS platform that can be used to do things like perform calculations or manipulate text. It can be used in pop-ups, labels, for field calculation and more.

FeatureSetis a function in Arcade that creates a set of features from a layer (or table) that can be used in calculations. Previously, you could use FeatureSet functions to pull values from other layers into a pop-up. With the Fall 2022 update of ArcGIS Online, we’ve expanded its use to smart forms, enabling you to pull values from another layer into a form while editing. This process automates data collection for that field, making editing more efficient while improving accuracy.

Example

Let’s say you’re a GIS Analyst in Honolulu working on an addressing project. You need to create a point for each new address and provide attribute information for the new point, including a zoning code for the location. No problem, using Arcade we’ll fetch that information from another layer that contains zoning codes and paste it right into the form when creating a new point.

Layers added to Map Viewer appear in the Panel on the Left.
Step 1: Author a map with an editable layer and reference layers

Step 1: Author a map

You’ll need a map with at least one editable layer, and a reference layer that contains the information you want. In this example, we have the following layers:

  • Editable layer: New Address Point
  • Reference layer: Zoning Codes

Let’s take a look inside the attribute table for Zoning Codes. The field ‘zone_class’ contains the information we want. We need to take note of both the layer name and the field name, as we’ll use these later in our Arcade expression.

A look inside the Zoning Codes attribute table
Explore the data to get both the name of the layer and the name of the field that contains the information you want

Step 2: Author a form

  1. With the editable layer selected, click Forms Forms button on the Settings (light) toolbar.
  2. Add fields to the form by dragging fields like Address and Zoning District onto the form.

 

Add fields to a form using Form builder.
Step 3: Author a form

Step 3: Author the expression

Next, we’ll open the Arcade editor window following these steps:

  1. Inside the Form builder window, Select the field you want to calculate (Zoning District).
  2. In the Properties Pane on the right, scroll down to Calculated expressions.
  3. Click the gear icon to open Calculated expressions.
  4. Click New expression.
  5. The Arcade editor window appears. This is where you can write and test expressions.

 

Inside the Arcade editor
Step 3: Author the expression in the Arcade editor

Let’s break it down. In this expression, we do the following:

  1. Use a variable to define a FeatureSet using the layer name and the field name
  • var <VariableName1> = FeatureSetByName($map, “<LayerName>”, [“<FieldName>”])

2. Use the function ‘Intersect’ to locate the data from the feature that intersects a new address point ($feature)

  • var <VariableName2> = First(Intersects($feature, <VariableName1>))

3. If the specified field contains a value, we return the value

  • if (!IsEmpty(variable2)) {return variable2[‘field name’]} else {return null}

Below is a snippet you can copy, paste, and modify:

Make this expression your own following these steps:

  1. Give your expression a title. This is especially important so you can identify expressions when you have more than one.
  2. Copy, paste, and customize the code snippet.
  3. Replace the “layer” and [‘field’] names to match your layer.
  4. Click Run to ensure the expression is working.
  5. Click Done to close the Arcade editor.
  6. Click OK to close the form.

 

Best practice: When a feature set is made up of point or line features, we recommend adding a small buffer to those features to ensure that the snapping tolerance is large enough for the intersect to be detected properly. You can do this using the Buffer() function in addition to Intersect.

Example:

  • var <VariableName2> = Intersects(<VariableName1>, Buffer($feature, <BufferSize>))

Step 4: Try it out

Open this map to give it a spin following these steps:

  1. Click Edit Edit button on the Settings (light) toolbar. The Editor pane appears.
  2. Under Create features, select New Address Point.
  3. Click to place a point on the map.

 

Watch the form autopopulate in Map Viewer

 

The form opens. The field ‘Zoning District’ is automatically populated with the correct zoning class.

Summary

And there you have it! You’ve just seen how you can use Arcade expressions to fetch values from other layers. We walked through configuring a form and authoring an expression using the Feature set function. This ability enables you to expedite data editing while ensuring data integrity so you can spend more time at the beach and less time managing data.

Now, go catch that wave!

 

Share this article