Query features with Arcade expression

View inMAUIWPFWinUIView on GitHubSample viewer app

Query features on a map using an Arcade expression.

QueryFeaturesWithArcadeExpression

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

Click on any neighborhood to see the number of crimes in the last 60 days in a callout.

How it works

  1. Create a PortalItem using the URL and ID.

  2. Create a Map using the portal item.

  3. Set the visibility of all the layers to false, except for the layer named "RPD Beats - City_Beats_Border_1128-4500".

  4. Set up a lambda or a listener for clicks on the map.

  5. Identify the visible layer where it is tapped or clicked on and get the feature.

  6. Create an ArcadeExpression using the following string:

    "var crimes = FeatureSetByName($map, 'Crime in the last 60 days');\n" +
    "return Count(Intersects($feature, crimes));"
  7. Create an ArcadeEvaluator using the Arcade expression and ArcadeProfile.FormCalculation.

  8. 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}
  9. Call ArcadeEvaluator.EvaluateAsync() on the Arcade evaluator object and pass the profile variables map.

  10. Get the result from the ArcadeEvaluationResult.Result property.

  11. 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

QueryFeaturesWithArcadeExpression.xaml.csQueryFeaturesWithArcadeExpression.xaml.csQueryFeaturesWithArcadeExpression.xaml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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;
using Esri.ArcGISRuntime.UI.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace ArcGIS.WPF.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: "Click 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
    {
        // 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)
            {
                MessageBox.Show(e.Message, "Error");
            }

            // 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(GeoViewInputEventArgs e)
        {
            try
            {
                // Get the layer based on the position tapped on the MapView.
                IdentifyLayerResult identifyResult = await MyMapView.IdentifyLayerAsync(_layer, e.Position, 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.Visibility = Visibility.Visible;

                    // 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.Visibility = Visibility.Collapsed;
                }

                // 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)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        private void MyMapView_GeoViewTapped(object sender, GeoViewInputEventArgs e)
        {
            _ = GeoViewTappedTask(e);
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.