Query features with Arcade expression

View inC++QMLView on GitHubSample viewer app

Query features on a map using an Arcade expression.

screenshot

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 at position 0.

  4. Connect to the MouseClicked event on the MapView.

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

  6. Create the following ArcadeExpression:

    "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 Enums.ArcadeProfileFormCalculation.

  8. Create a map of profile variables with the following key-value pairs. This will be passed to ArcadeEvaluator.evaluate() in the next step.

      `{"$feature", identifiedFeature}`
      `{"$map", map}`
    
  9. Call ArcadeEvaluator.evaluate() on the Arcade evaluator object and pass the profile variables map.

  10. Call ArcadeEvaluationResult.result() to get the result from ArcadeEvaluator.ArcadeEvaluationResult.

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.qml
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
// [WriteFile Name=QueryFeaturesWithArcadeExpression, Category=DisplayInformation]
// [Legal]
// 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.
// [Legal]

import QtQuick 2.12
import Esri.ArcGISRuntime 100.15
import Esri.ArcGISExtras 1.1
import Esri.ArcGISRuntime.Toolkit 100.15

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    property Point mapPoint: null

    MapView {
        id: mapView
        anchors.fill: parent

        Component.onCompleted: {
            // Set and keep the focus on MapView to enable keyboard navigation
            forceActiveFocus();
        }

        PortalItem {
            id: mapPortalItem
            portal: portal
            itemId: "14562fced3474190b52d315bc19127f6"
        }

        Map {
            id: map
            item: mapPortalItem

            onLoadStatusChanged: {
                if (loadStatus === Enums.LoadStatusLoaded) {

                    // Set the visibility of all the other layers to false to avoid cluttering the UI
                    mapView.map.operationalLayers.get(1).visible = false;
                    mapView.map.operationalLayers.get(2).visible = false;
                    mapView.map.operationalLayers.get(3).visible = false;
                }
            }
        }
        onMouseClicked: {
            mapPoint = screenToLocation(mouse.x, mouse.y);
            if (identifyLayerStatus !== Enums.TaskStatusInProgress) {
                if (map.operationalLayers.get(0))
                {
                    identifyLayer(map.operationalLayers.get(0), mouse.x, mouse.y, 12, false, 1);
                }
            }
        }
        onIdentifyLayerStatusChanged: {
            if (identifyLayerStatus === Enums.TaskStatusCompleted) {
                if (identifyLayerResult.geoElements.length < 1) {
                    callout.dismiss();
                    return;
                }
                else if (identifyLayerResult.geoElements.length > 0) {
                    const geoElement = identifyLayerResult.geoElements[0];

                    showEvaluatedArcadeInCallout(geoElement, mapPoint);
                }
                callout.calloutData.title = "RPD Beats"
            }
        }

        Callout {
            id: callout
            calloutData: parent.calloutData
            accessoryButtonHidden: true
            leaderPosition: leaderPositionEnum.Automatic
        }
    }

    function showEvaluatedArcadeInCallout(feature, mapPoint) {
        const expressionString = "var crimes = FeatureSetByName($map, \'Crime in the last 60 days\');\n
        return Count(Intersects($feature, crimes));";
        const arcadeExpression = ArcGISRuntimeEnvironment.createObject("ArcadeExpression", {expression: expressionString});
        const arcadeEvaluator = ArcGISRuntimeEnvironment.createObject("ArcadeEvaluator", {initExpression: arcadeExpression, initProfile: Enums.ArcadeProfileFormCalculation});

        const profileVariables = {
            "$feature": feature,
            "$map": map
        };
        arcadeEvaluator.evaluateStatusChanged.connect(()=> {
                                                          if (arcadeEvaluator.evaluateStatus !== Enums.TaskStatusCompleted)
                                                          return;

                                                          const result = arcadeEvaluator.evaluateResult.result;
                                                          callout.calloutData.detail = "Crimes in the last 60 days: " + result;
                                                          callout.calloutData.location = mapPoint;
                                                          callout.showCallout();
                                                      });
        const evaluationResult = arcadeEvaluator.evaluate(profileVariables);
    }
}

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