-
Notifications
You must be signed in to change notification settings - Fork 521
/
QueryFeaturesWithArcadeExpression.xaml.cs
140 lines (114 loc) · 6.5 KB
/
QueryFeaturesWithArcadeExpression.xaml.cs
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
// 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;
namespace ArcGIS.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: "Tap 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 : ContentPage
{
// 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)
{
await Application.Current.MainPage.DisplayAlert("Error", e.Message, "OK");
}
// 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(Esri.ArcGISRuntime.Maui.GeoViewInputEventArgs e)
{
try
{
// Get the layer based on the position tapped on the MapView.
IdentifyLayerResult identifyResult = await MyMapView.IdentifyLayerAsync(_layer, new Point(e.Position.X, e.Position.Y), 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.IsVisible = true;
// 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.IsVisible = false;
}
// 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)
{
await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
}
}
private void MyMapView_GeoViewTapped(object sender, Esri.ArcGISRuntime.Maui.GeoViewInputEventArgs e)
{
_ = GeoViewTappedTask(e);
}
}
}