View on GitHub Sample 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 Portal using the URL.
  2. Create a PortalItem using the Portal and ID.
  3. Create an ArcGISMap using the PortalItem.
  4. Set up a listener for clicks on the map.
  5. Identify the visible layer where it is clicked using mapView.identifyLayerAsync() and get the feature.
  6. Create the following ArcadeExpression: new 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 $feature and $map as keys.
  9. Call arcadeEvaluator.evaluateAsync() and pass it the profile variables map as parameters.
  10. Once the evaluateAsync() has finished evaluating, get the ArcadeEvaluationResult with listenableFuture.get()
  11. Get the result from the arcade evaluation result with arcadeEvaluationResult.getResult().
  12. 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 2 layers for 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

module-info.java module-info.java QueryFeaturesWithArcadeExpressionSample.java
/*
* 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.
*/
module com.esri.samples.query_features_with_arcade_expression {
// require ArcGIS Maps SDK for Java module
requires com.esri.arcgisruntime;
// handle SLF4J http://www.slf4j.org/codes.html#StaticLoggerBinder
requires org.slf4j.nop;
// require JavaFX modules that the application uses
requires javafx.graphics;
requires javafx.controls;
exports com.esri.samples.query_features_with_arcade_expression;
}