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 ArcadeProfile.FORM_CALCULATION.

  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.

  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.cppQueryFeaturesWithArcadeExpression.cppQueryFeaturesWithArcadeExpression.hQueryFeaturesWithArcadeExpression.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
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
// [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]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "QueryFeaturesWithArcadeExpression.h"

#include "ArcadeEvaluator.h"
#include "ArcadeEvaluationResult.h"
#include "ArcadeExpression.h"
#include "ArcGISFeatureTable.h"
#include "CalloutData.h"
#include "Map.h"
#include "MapQuickView.h"
#include "Point.h"
#include "PortalItem.h"

#include <QVariantMap>

using namespace Esri::ArcGISRuntime;

QueryFeaturesWithArcadeExpression::QueryFeaturesWithArcadeExpression(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISTopographic, this))
{
  Portal* portal = new Portal(QUrl("https://www.arcgis.com/"), this);
  PortalItem* portalItem = new PortalItem(portal, "14562fced3474190b52d315bc19127f6", this);

  // Create a map object using the portalItem
  m_map = new Map(portalItem, this);

  connect(m_map, &Map::doneLoading, this, [this]()
  {
    if (m_map->loadStatus() == LoadStatus::Loaded)
    {
      // Set the visibility of all but the RDT Beats layer to false to avoid cluttering the UI
      m_map->operationalLayers()->at(1)->setVisible(false);
      m_map->operationalLayers()->at(2)->setVisible(false);
      m_map->operationalLayers()->at(3)->setVisible(false);
    }
  });
}

QueryFeaturesWithArcadeExpression::~QueryFeaturesWithArcadeExpression() = default;

void QueryFeaturesWithArcadeExpression::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<QueryFeaturesWithArcadeExpression>("Esri.Samples", 1, 0, "QueryFeaturesWithArcadeExpressionSample");
}

MapQuickView* QueryFeaturesWithArcadeExpression::mapView() const
{
  return m_mapView;
}

// Set the view (created in QML)
void QueryFeaturesWithArcadeExpression::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;
  m_mapView->setMap(m_map);

  m_mapView->calloutData()->setVisible(false);
  m_mapView->calloutData()->setTitle("RPD Beats");

  connect(m_mapView, &MapQuickView::mouseClicked, this, [this](QMouseEvent& mouseEvent){
    if (m_mapView->calloutData()->isVisible())
      m_mapView->calloutData()->setVisible(false);
    m_mapView->calloutData()->setDetail("");
    // Set callout position
    const Point mapPoint(m_mapView->screenToLocation(mouseEvent.x(), mouseEvent.y()));
    m_mapView->calloutData()->setLocation(mapPoint);
    m_mapView->identifyLayers(mouseEvent.x(), mouseEvent.y(), 12, false);

  });

  connect(m_mapView, &MapQuickView::identifyLayersCompleted, this, [this](QUuid, const QList<IdentifyLayerResult*>& results)
  {
    if (results.empty())
      return;

    QList<GeoElement*> element_list = results.first()->geoElements();
    if (element_list.empty())
      return;

    GeoElement* element = element_list.at(0);
    ArcGISFeature* identifiedFeature = dynamic_cast<ArcGISFeature*>(element);
    m_mapView->calloutData()->setVisible(true);

    showEvaluatedArcadeInCallout(identifiedFeature);
  });

  emit mapViewChanged();
}

void QueryFeaturesWithArcadeExpression::showEvaluatedArcadeInCallout(Feature* feature)
{
  QVariantMap profileVariables;
  profileVariables["$feature"] = QVariant::fromValue(feature);
  profileVariables["$map"] = QVariant::fromValue(m_map);

  const QString expressionValue =
      "var crimes = FeatureSetByName($map, 'Crime in the last 60 days');\n"
      "return Count(Intersects($feature, crimes));";

  ArcadeExpression expression {expressionValue};
  ArcadeEvaluator* evaluator = new ArcadeEvaluator(&expression, ArcadeProfile::FormCalculation, this);

  connect(evaluator, &ArcadeEvaluator::evaluateCompleted, this, [this](QUuid, ArcadeEvaluationResult* arcadeEvaluationResult)
  {
    if (!arcadeEvaluationResult)
      return;

    QVariant evalResult = arcadeEvaluationResult->result();
    const int crimeCount = evalResult.toInt();
    m_mapView->calloutData()->setDetail("Crimes in the last 60 days: " + QString::number(crimeCount));
  });
  evaluator->evaluate(profileVariables);
}

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