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
Click 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 at position 0.
-
Connect to the
MouseClicked
event on the MapView. -
Identify the visible layer where it is tapped or clicked on and get the feature.
-
Create the following
ArcadeExpression
:"var crimes = FeatureSetByName($map, 'Crime in the last 60 days');\n" "return Count(Intersects($feature, crimes));"
-
Create an
ArcadeEvaluator
using the Arcade expression andArcadeProfile.FORM_CALCULATION
. -
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}`
-
Call
ArcadeEvaluator::evaluate()
on the Arcade evaluator object and pass the profile variables map. -
Call
ArcadeEvaluationResult::result()
to get the result fromArcadeEvaluator::ArcadeEvaluationResult
. -
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
// [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);
}