Find features in a feature table which match an SQL query.
Use case
Query expressions can be used in ArcGIS to select a subset of features from a feature table. This is most useful in large or complicated data sets. A possible use case might be on a feature table marking the location of street furniture through a city. A user may wish to query by a TYPE column to return "benches". In this sample, we query a U.S. state by STATE_NAME from a feature table containing all U.S. states.
How to use the sample
Input the name of a U.S. state into the text field. When you click the button, a query is performed and the matching features are highlighted or an error is returned.
How it works
Create a ServiceFeatureTable using the URL of a feature service.
Create a QueryParameters with a where clause specified using the WhereClause property.
Perform the query using QueryFeaturesAsync(query) on the service feature table.
When complete, the query will return a FeatureQueryResult which can be iterated over to get the matching features.
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
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2018 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.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
namespaceArcGIS.WinUI.Samples.FeatureLayerQuery{
[ArcGIS.Samples.Shared.Attributes.Sample(
name: "Feature layer query",
category: "Data",
description: "Find features in a feature table which match an SQL query.",
instructions: "Input the name of a U.S. state into the text field. When you click the button, a query is performed and the matching features are highlighted or an error is returned.",
tags: new[] { "query", "search" })]
publicpartialclassFeatureLayerQuery {
// Create reference to service of US Statesprivatestring _statesUrl = "https://services.arcgis.com/jIL9msH9OI208GCb/arcgis/rest/services/USA_Daytime_Population_2016/FeatureServer/0";
// Create globally available feature table for easy referencingprivate ServiceFeatureTable _featureTable;
// Create globally available feature layer for easy referencingprivate FeatureLayer _featureLayer;
publicFeatureLayerQuery() {
InitializeComponent();
// Create the UI, setup the control references and execute initialization Initialize();
}
privatevoidInitialize() {
// Create new Map with basemap Map myMap = new Map(BasemapStyle.ArcGISTopographic);
// Create and set initial map location MapPoint initialLocation = new MapPoint(
-11000000, 5000000, SpatialReferences.WebMercator);
myMap.InitialViewpoint = new Viewpoint(initialLocation, 100000000);
// Create feature table using a url _featureTable = new ServiceFeatureTable(new Uri(_statesUrl));
// Create feature layer using this feature table _featureLayer = new FeatureLayer(_featureTable)
{
// Set the Opacity of the Feature Layer Opacity = 0.6,
// Work around service setting MaxScale = 10 };
// Create a new renderer for the States Feature Layer. SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Black, 1);
SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Transparent, lineSymbol);
// Set States feature layer renderer _featureLayer.Renderer = new SimpleRenderer(fillSymbol);
// Set the selection color MyMapView.SelectionProperties.Color = Color.Cyan;
// Add feature layer to the map myMap.OperationalLayers.Add(_featureLayer);
// Assign the map to the MapView MyMapView.Map = myMap;
}
privateasync Task QueryStateFeature(string stateName) {
try {
// Create a query parameters that will be used to Query the feature table QueryParameters queryParams = new QueryParameters();
// Trim whitespace on the state name to prevent broken queriesstring formattedStateName = stateName.Trim().ToUpper();
// Construct and assign the where clause that will be used to query the feature table queryParams.WhereClause = "upper(STATE_NAME) LIKE '%" + formattedStateName + "%'";
// Query the feature table FeatureQueryResult queryResult = await _featureTable.QueryFeaturesAsync(queryParams);
// Cast the QueryResult to a List so the results can be interrogated List<Feature> features = queryResult.ToList();
if (features.Any())
{
// Create an envelope. EnvelopeBuilder envBuilder = new EnvelopeBuilder(SpatialReferences.WebMercator);
foreach (Feature feature in features)
{
// Add the extent of each matching feature to the envelope. envBuilder.UnionOf(feature.Geometry.Extent);
// Select each feature. _featureLayer.SelectFeature(feature);
}
// Zoom to the extent of the selected feature(s).await MyMapView.SetViewpointGeometryAsync(envBuilder.ToGeometry(), 50);
}
else {
var message = new MessageDialog2("State Not Found!", "Add a valid state name.");
await message.ShowAsync();
}
}
catch (Exception ex)
{
var message = new MessageDialog2("Sample error: " + ex, "An error occurred");
await message.ShowAsync();
}
}
privateasyncvoidQueryEntry_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args) {
try {
// Remove any previous feature selections that may have been made _featureLayer.ClearSelection();
// Begin query processawait QueryStateFeature(args.QueryText);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
}
}