Query features on a map using an Arcade expression.
Use case
Arcade is a portable, lightweight, and secure expression language used to create custom content in ArcGIS applications. Like other expression languages, it can perform mathematical calculations, manipulate text, and evaluate logical statements. It also supports multi-statement expressions, variables, and flow control statements. What makes Arcade particularly unique when compared to other expression and scripting languages is its inclusion of feature and geometry data types. This sample uses an arcade expression to query the number of crimes in a neighborhood in the last 60 days.
How to use the sample
Tap on any neighborhood to see the number of crimes in the last 60 days in a callout.
How it works
-
Create a
PortalItem
using the URL and ID. -
Create a
Map
using the portal item. -
Set the visibility of all the layers to false, except for the layer named "RPD Beats - City_Beats_Border_1128-4500".
-
Set up a lambda or a listener for clicks on the map.
-
Identify the visible layer where it is tapped or tapped on and get the feature.
-
Create an
ArcadeExpression
using the following string:"var crimes = FeatureSetByName($map, 'Crime in the last 60 days');\n" + "return Count(Intersects($feature, crimes));"
-
Create an
ArcadeEvaluator
using the Arcade expression andArcadeProfile.FormCalculation
. -
Create a map of profile variables with the following key-value pairs. This will be passed to
ArcadeEvaluator.EvaluateAsync()
in the next step:{"$feature", identifiedFeature} {"$map", map}
-
Call
ArcadeEvaluator.EvaluateAsync()
on the Arcade evaluator object and pass the profile variables map. -
Get the result from the
ArcadeEvaluationResult.Result
property. -
Convert the result to a numerical value (integer) and populate the callout with the crime count.
Relevant API
- ArcadeEvaluationResult
- ArcadeEvaluator
- ArcadeExpression
- ArcadeProfile
- Portal
- PortalItem
About the data
This sample uses the Crimes in Police Beats Sample ArcGIS Online Web Map which contains 3 layers for police stations, city beats borders, and crimes in the last 60 days as recorded by the Rochester, NY police department.
Additional information
Visit Getting Started on the ArcGIS Developer website to learn more about Arcade expressions.
Tags
Arcade evaluator, Arcade expression, identify layers, portal, portal item, query
Sample Code
// Copyright 2022 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
using Esri.ArcGISRuntime;
using Esri.ArcGISRuntime.Arcade;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Portal;
using Esri.ArcGISRuntime.UI;
namespace ArcGIS.Samples.QueryFeaturesWithArcadeExpression
{
[ArcGIS.Samples.Shared.Attributes.Sample(
name: "Query features with Arcade expression",
category: "Data",
description: "Query features on a map using an Arcade expression.",
instructions: "Tap on any neighborhood to see the number of crimes in the last 60 days in a callout.",
tags: new[] { "Arcade evaluator", "Arcade expression", "identify layers", "portal", "portal item", "query" })]
public partial class QueryFeaturesWithArcadeExpression : ContentPage
{
// Hold a reference to the layer for use in event handlers.
private Layer _layer;
// Hold a reference to the feature for use in event handlers.
private ArcGISFeature _previousFeature;
// The name of the layer used in this sample.
private const string RPDBeatsLayerName = "RPD Beats - City_Beats_Border_1128-4500";
// Hold a reference to the callout text content to store it between clicks.
private string _calloutText = string.Empty;
public QueryFeaturesWithArcadeExpression()
{
InitializeComponent();
_ = Initialize();
}
private async Task Initialize()
{
try
{
// Create an ArcGIS portal item.
var portal = await ArcGISPortal.CreateAsync();
var item = await PortalItem.CreateAsync(portal, "14562fced3474190b52d315bc19127f6");
// Create a map using the portal item.
MyMapView.Map = new Map(item);
}
catch (Exception e)
{
await Application.Current.MainPage.DisplayAlert("Error", e.Message, "OK");
}
// Load the map.
await MyMapView.Map.LoadAsync();
// Set the visibility of all but the RDT Beats layer to false to prevent UI clutter.
MyMapView.Map.OperationalLayers.ToList().ForEach(l => l.IsVisible = l.Name == RPDBeatsLayerName);
// Hold the layer value so that the clicked geoelement can be identified in the event handler.
_layer = MyMapView.Map.OperationalLayers.FirstOrDefault(l => l.Name == RPDBeatsLayerName);
}
private async Task GeoViewTappedTask(Esri.ArcGISRuntime.Maui.GeoViewInputEventArgs e)
{
try
{
// Get the layer based on the position tapped on the MapView.
IdentifyLayerResult identifyResult = await MyMapView.IdentifyLayerAsync(_layer, new Point(e.Position.X, e.Position.Y), 12, false);
if (identifyResult == null || !identifyResult.GeoElements.Any())
{
MyMapView.DismissCallout();
return;
}
// Get the tapped GeoElement as an ArcGISFeature.
GeoElement element = identifyResult.GeoElements.First();
ArcGISFeature feature = element as ArcGISFeature;
// If the previously clicked feature is null or the previous feature ID does not match the current feature ID
// run the arcade expression query to get the crime count for a given feature.
if (_previousFeature == null || !(feature.Attributes["ID"].Equals(_previousFeature.Attributes["ID"])))
{
// Show the loading indicator as the arcade evaluator evaluation call can take time to complete.
MyLoadingGrid.IsVisible = true;
// Instantiate a string containing the arcade expression.
string expressionValue = "var crimes = FeatureSetByName($map, 'Crime in the last 60 days');\n" +
"return Count(Intersects($feature, crimes));";
// Create an ArcadeExpression using the string expression.
var expression = new ArcadeExpression(expressionValue);
// Create an ArcadeEvaluator with the ArcadeExpression and an ArcadeProfile enum.
var evaluator = new ArcadeEvaluator(expression, ArcadeProfile.FormCalculation);
// Instantiate a list of profile variable key value pairs.
var profileVariables = new List<KeyValuePair<string, object>>();
profileVariables.Add(new KeyValuePair<string, object>("$feature", feature));
profileVariables.Add(new KeyValuePair<string, object>("$map", MyMapView.Map));
// Get the arcade evaluation result given the previously set profile variables.
ArcadeEvaluationResult arcadeEvaluationResult = await evaluator.EvaluateAsync(profileVariables);
if (arcadeEvaluationResult == null) return;
// Construct the callout text content.
var crimeCount = Convert.ToInt32(arcadeEvaluationResult.Result);
_calloutText = $"Crimes in the last 60 days: {crimeCount}";
// Set the current feature as the previous feature for the next click detection.
_previousFeature = feature;
// Hide the loading indicator.
MyLoadingGrid.IsVisible = false;
}
// Display a callout showing the number of crimes in the last 60 days.
MyMapView.ShowCalloutAt(e.Location, new CalloutDefinition(string.Empty, _calloutText));
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
}
}
private void MyMapView_GeoViewTapped(object sender, Esri.ArcGISRuntime.Maui.GeoViewInputEventArgs e)
{
_ = GeoViewTappedTask(e);
}
}
}