ArcGIS Blog

Mapping

What’s new with Arcade: Taking a stroll through FeatureSets (Part 1)

By Paul Barker

The introduction of Arcade allowed you to work with a feature and manipulate its information to create rich new attributes on the fly. While that allowed you to do useful and interesting things, it immediately sparked your imagination. What could you do with Arcade if you had the ability to work with multiple features in that layer or even features from a completely different layer?  Well, the Arcade team has been busy sifting through your requests and I’d like to introduce to some exciting new functions: FeatureSets.

What’s a FeatureSet?

FeatureSets are a new set of Arcade data functions you can use with feature layers. FeatureSets let you work with multiple features in a layer and can be used when authoring a pop-up or when calculating fields.  When chained together with other geometry and data functions like Intersects(), Filter(), and Orderby(), they become pretty powerful by allowing you to strategically build focused pop-ups or style your map based on a calculated field.

FeatureSets can feel a bit unfamiliar if you’re just getting started with Arcade but you don’t need to write long, complicated Arcade expressions just to take advantage of this new functionality.  On that note, let’s break down FeatureSets and how they can be used to make better pop-ups.

Using FeatureSets

FeatureSets, as the name suggests, are a set of features.  How many features will depend on your data and the functions used in the expression.  In the simplest case, if you define a FeatureSet from a feature layer, it is all the features in the feature layer.  The second aspect of FeatureSets is how you reference or access them.  When defining a FeatureSet from a feature layer, you need to tell the Arcade expression where to look.  That is done with global variables:

  • $map – allows you define a FeatureSet from a Feature Layer (from a feature service) that is in your map
  • $datastore – allows you to define a FeatureSet from any sub-layer of the Feature Layer that you’re currently working with in the Arcade editor
  • $layer – allows you to define a FeatureSet from the current layer being worked with in the Arcade editor

Before we dive into some examples, I suggest you take a quick look at the following web map to familiarize yourself with the two layers—Neighborhoods and Street Tree locations—from Toronto’s Open Data Catalogue. If you want to follow along, save a copy of the map to your account.

Crack open the Arcade editor for the Neighborhoods layer by configuring the pop-up and adding a new expression, give the expression the name “Number of Trees”, paste in the following code, and then click Test.

// get the count for all trees in the Neighborhood
var trees = FeatureSetByName($map,"Urban Forestry")
var countTrees = Count(Intersects(trees,$feature))
return countTrees

The expression above tells you how many trees are in the neighborhood when you open the pop-up.  First it defines a FeatureSet called trees using the name of the layer in the web map ($map).  On the next line, we define a variable that stores the number of trees that intersect the polygon ($feature). Arcade allows you to chain functions together for convenience. The last line returns the count value to be used in the pop-up.  Properly referencing the layer for a FeatureSet can be tricky to type out by hand.  The good news is you don’t have to; you can browse to the layer from the Globals tab and just insert the snippet.

 

You’ll notice in the video above that there are two ways to define a FeatureSet: by Name or by Id.  Both are identical in functionality, and how you refer to the layer is up to you.  If you expect your layer’s name to change, then I would recommend using the Id instead.

Save the pop-up and click on a Neighborhood on the map, and you’ll see your expression in the pop-up.

With the basics out of the way, let’s take a look at a few other functions that compliment FeatureSets, such as Filter().  The Filter function allows you to filter the FeatureSet based on a SQL’92 where clause. Maybe within the context of your map you are interested in Maple trees.  Starting with the expression above, you can add a new expression (or modify the existing one) to keep only Maple trees.

var mapleTrees = Filter(FeatureSetByName($map, "Urban Forestry"),"COMMON_NAM LIKE '%MAPLE%'")
var countMapleTrees = Count(Intersects(mapleTrees, $feature))
return countMapleTrees

You can also modify the expression to understand the density of trees in the neighborhood:

var trees = FeatureSetByName($map,"Urban Forestry")
var countTrees = Count(Intersects(trees,$feature))
var treeDensity = countTrees / AreaGeodetic($feature, 'square-kilometers')
return Round(treeDensity,2)

Bundle that all up in a nice pop-up and you now have dynamic information coming from another layer.

 

Things to consider

FeatureSets are hot off the press and not supported everywhere yet.  If you want to take advantage of FeaturesSets in your maps and apps, you will need to use web apps that are written with the ArcGIS API for Javascript 3.27.  Support for FeatureSets will be arriving in the 4.x Javascript API, ArcGIS Pro (2.3), ArcGIS Runtime, and our Native apps in future releases.

As you get more familiar with FeatureSets, you’ll quickly learn how to write more rich and complicated expressions (iteration, multiple layers, and more), as with any dynamic calculation you have to balance performance and user experience.  If your data is too large and your expression complex or you have too many expressions, you may find the Arcade expression evaluates too slowly to make for an effective user experience.  This can happen because FeatureSets are a connection to a set of features in memory or on a server and all the information must be retrieved first before evaluating the expression(s). If you find yourself in that situation after creating your expression, I highly recommend taking the time to calculate the expression to an existing field in your layer using Calculate Field (more on in a future blog article).

Learn more

Keep an eye on our blog page for a deeper dive into FeatureSets in the coming weeks.  In the meantime, if you want to read more about Arcade, be sure to check out the Developers Site along with our Community Github site with great Arcade examples. Or checkout Geonet for a sneak peak of  some things we’ll cover in Part 2.

Share this article