Manage features
Manage a feature layer's features in four distinct ways.
Use case
An end-user performing a survey may want to manage features on the map in various ways during the course of their work.
How to use the sample
Pick a function, then tap a location on the map to perform the function at that location. Available edit functions include, "Create feature", "Delete feature", "Update attribute", and "Update geometry".
How it works
A Feature
instance is added to a ServiceFeatureTable
which then pushes that new feature to the server.
- Create a
ServiceGeodatabase
from a URL.- When the table loads, you can get the domain to determine which options to present in your UI.
- Get a
ServiceFeatureTable
from theServiceGeodatabase
. - Create a
FeatureLayer
derived from theServiceFeatureTable
instance. - Create a
Picker
with four options, each representing its corresponding edit function. Selecting a new option should change the behavior ofGeoViewInputArgsEvent
. - Create a
Feature
with attributes and a location using theServiceFeatureTable
. - Apply edits intended by the selected option to the
ServiceGeodatabase
upon tapping the map. - After the change, update the table on the server using
ApplyEditsAsync
.
Relevant API
- Feature
- FeatureEditResult
- FeatureLayer
- ServiceFeatureTable
- ServiceGeodatabase
Additional information
When editing feature tables that are subject to database behavior (operations on one table affecting another table), it's now recommended to call these methods (apply edits & undo edits) on the ServiceGeodatabase
object rather than on the ServiceFeatureTable
object. Using the ServiceGeodatabase
object to call these methods will prevent possible data inconsistencies and ensure transactional integrity so that all changes can be commited or rolled back.
Tags
amend, attribute, deletion, details, edit, editing, feature, feature layer, feature table, information, moving, online service, service, updating, value
Sample Code
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright 2023 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.UI.Controls;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ArcGIS.WinUI.Samples.ManageFeatures
{
[ArcGIS.Samples.Shared.Attributes.Sample(
name: "Manage features",
category: "Data",
description: "Manage a feature layer's features in four distinct ways.",
instructions: "Pick a function, then tap a location on the map to perform the function at that location. Available edit functions include, \"Create feature\", \"Delete feature\", \"Update attribute\", and \"Update geometry\".",
tags: new[] { "amend", "attribute", "deletion", "details", "edit", "editing", "feature", "feature layer", "feature table", "information", "moving", "online service", "service", "updating", "value" })]
[ArcGIS.Samples.Shared.Attributes.OfflineData()]
public partial class ManageFeatures
{
// URL to the feature service.
private const string FeatureServiceUrl = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0";
// Hold a reference to the feature layer.
private FeatureLayer _damageLayer;
// Hold a reference to the feature table.
private ServiceFeatureTable _damageFeatureTable;
// Name of the field that will be updated.
private const string AttributeFieldName = "typdamage";
// Hold a reference to the selected feature.
private ArcGISFeature _selectedFeature;
// Create an items source list for the ComboBox.
private List<string> _methodList = new List<string> { "Create feature", "Delete feature", "Update attribute", "Update geometry" };
public ManageFeatures()
{
InitializeComponent();
_ = Initialize();
}
public async Task Initialize()
{
try
{
// Create the map with streets basemap.
MyMapView.Map = new Map(BasemapStyle.ArcGISStreets);
// Create a service geodatabase from the feature service.
ServiceGeodatabase serviceGeodatabase = new ServiceGeodatabase(new Uri(FeatureServiceUrl));
await serviceGeodatabase.LoadAsync();
// Gets the feature table from the service geodatabase, referring to the Damage Assessment feature service.
// Creating the feature table from the feature service will cause the service geodatabase to be null.
_damageFeatureTable = serviceGeodatabase.GetTable(0);
//// UPDATE ATTRIBUTES - When the table loads, use it to discover the domain of the typdamage field.
_damageFeatureTable.Loaded += DamageTable_Loaded;
// Create a feature layer to visualize the features in the table.
_damageLayer = new FeatureLayer(_damageFeatureTable);
// Add the layer to the map.
MyMapView.Map.OperationalLayers.Add(_damageLayer);
// Zoom to the United States.
_ = MyMapView.SetViewpointCenterAsync(new MapPoint(-10800000, 4500000, SpatialReferences.WebMercator), 3e7);
// Bind the list of method names to the ComboBox.
MethodPicker.ItemsSource = _methodList;
}
catch (Exception ex)
{
await new MessageDialog2(ex.ToString(), "Error").ShowAsync();
}
}
private void DamageTable_Loaded(object sender, EventArgs e)
{
// This code needs to work with the UI, so it needs to run on the UI thread.
DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
// Get the relevant field from the table.
ServiceFeatureTable table = (ServiceFeatureTable)sender;
Field typeDamageField = table.Fields.First(field => field.Name == AttributeFieldName);
// Get the domain for the field.
CodedValueDomain attributeDomain = (CodedValueDomain)typeDamageField.Domain;
// Update the ComboBox with the attribute values.
DamageTypeDropDown.ItemsSource = attributeDomain.CodedValues.Select(codedValue => codedValue.Name);
});
}
private async void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Clear all potentially hooked GeoViewTapped events.
MyMapView.GeoViewTapped -= MapView_Tapped_CreateFeature;
MyMapView.GeoViewTapped -= MapView_Tapped_DeleteFeature;
MyMapView.GeoViewTapped -= MapView_Tapped_UpdateAttribute;
MyMapView.GeoViewTapped -= MapView_Tapped_UpdateGeometry;
// Reset UI elements.
_damageLayer.ClearSelection();
_selectedFeature = null;
DamageTypeDropDown.Visibility = Visibility.Collapsed;
MyMapView.DismissCallout();
// Hold a variable for the ComboBox's selected value.
ComboBox comboBox = (ComboBox)sender;
if (comboBox.SelectedIndex == 0)
{
// Update the label.
InstructionLabel.Text = "Tap on the map to create a new feature.";
// Update the currently hooked GeoViewTapped event.
MyMapView.GeoViewTapped += MapView_Tapped_CreateFeature;
}
else if (comboBox.SelectedIndex == 1)
{
// Update the label.
InstructionLabel.Text = "Tap an existing feature to delete it.";
// Update the currently hooked GeoViewTapped event.
MyMapView.GeoViewTapped += MapView_Tapped_DeleteFeature;
}
else if (comboBox.SelectedIndex == 2)
{
// Update the label.
InstructionLabel.Text = "Tap an existing feature to edit its attribute.";
// Update the currently hooked GeoViewTapped event.
MyMapView.GeoViewTapped += MapView_Tapped_UpdateAttribute;
}
else if (comboBox.SelectedIndex == 3)
{
// Update the label.
InstructionLabel.Text = "Tap an existing feature to select it, tap the map to move it to a new position.";
// Update the currently hooked GeoViewTapped event.
MyMapView.GeoViewTapped += MapView_Tapped_UpdateGeometry;
}
}
private async void MapView_Tapped_CreateFeature(object sender, GeoViewInputEventArgs e)
{
try
{
// Create the feature.
ArcGISFeature feature = (ArcGISFeature)_damageFeatureTable.CreateFeature();
// Get the normalized geometry for the tapped location and use it as the feature's geometry.
MapPoint tappedPoint = (MapPoint)GeometryEngine.NormalizeCentralMeridian(e.Location);
feature.Geometry = tappedPoint;
// Set feature attributes.
feature.SetAttributeValue("typdamage", "Minor");
feature.SetAttributeValue("primcause", "Earthquake");
// Add the feature to the table.
await _damageFeatureTable.AddFeatureAsync(feature);
// Apply the edits to the service on the service geodatbase.
await _damageFeatureTable.ServiceGeodatabase.ApplyEditsAsync();
// Update the feature to get the updated objectid - a temporary ID is used before the feature is added.
feature.Refresh();
// Confirm feature addition.
await new MessageDialog2($"Created feature {feature.Attributes["objectid"]}", "Success").ShowAsync();
}
catch (Exception ex)
{
await new MessageDialog2(ex.ToString(), "Error creating feature").ShowAsync(); ;
}
}
private async void MapView_Tapped_DeleteFeature(object sender, GeoViewInputEventArgs e)
{
// Clear any existing selection.
_damageLayer.ClearSelection();
// Dismiss any existing callouts.
MyMapView.DismissCallout();
try
{
// Perform an identify to determine if a user tapped on a feature.
IdentifyLayerResult identifyResult = await MyMapView.IdentifyLayerAsync(_damageLayer, e.Position, 2, false);
// Do nothing if there are no results.
if (!identifyResult.GeoElements.Any())
{
return;
}
// Otherwise, get the ID of the first result.
long featureId = (long)identifyResult.GeoElements.First().Attributes["objectid"];
// Get the feature by constructing a query and running it.
QueryParameters qp = new QueryParameters();
qp.ObjectIds.Add(featureId);
FeatureQueryResult queryResult = await _damageLayer.FeatureTable.QueryFeaturesAsync(qp);
Feature tappedFeature = queryResult.First();
// Select the feature.
_damageLayer.SelectFeature(tappedFeature);
// Show the callout.
ShowDeletionCallout(tappedFeature);
}
catch (Exception ex)
{
await new MessageDialog2(ex.ToString(), "Error selecting feature").ShowAsync();
}
}
private void ShowDeletionCallout(Feature tappedFeature)
{
// Create a button for deleting the feature.
Button deleteButton = new Button();
deleteButton.Content = "Delete incident";
deleteButton.Padding = new Thickness(5);
deleteButton.Tag = tappedFeature;
// Handle button clicks.
deleteButton.Click += DeleteButton_Click;
// Show the callout.
MyMapView.ShowCalloutAt((MapPoint)tappedFeature.Geometry, deleteButton);
}
private async void DeleteButton_Click(object sender, RoutedEventArgs e)
{
// Dismiss the callout.
MyMapView.DismissCallout();
try
{
// Get the feature to delete from the layer.
Button deleteButton = (Button)sender;
Feature featureToDelete = (Feature)deleteButton.Tag;
// Delete the feature.
await _damageLayer.FeatureTable.DeleteFeatureAsync(featureToDelete);
// Sync the change with the service on the service geodatabase.
ServiceFeatureTable serviceTable = (ServiceFeatureTable)_damageLayer.FeatureTable;
await serviceTable.ServiceGeodatabase.ApplyEditsAsync();
// Show a message confirming the deletion.
await new MessageDialog2($"Deleted feature with ID {featureToDelete.Attributes["objectid"]}", "Success").ShowAsync();
}
catch (Exception ex)
{
await new MessageDialog2(ex.ToString(), "Couldn't delete feature.").ShowAsync();
}
}
private async void MapView_Tapped_UpdateAttribute(object sender, GeoViewInputEventArgs e)
{
// Clear any existing selection.
_damageLayer.ClearSelection();
// Dismiss any existing callouts.
MyMapView.DismissCallout();
// Reset the dropdown.
DamageTypeDropDown.Visibility = Visibility.Collapsed;
DamageTypeDropDown.SelectedIndex = -1;
try
{
// Perform an identify to determine if a user tapped on a feature.
IdentifyLayerResult identifyResult = await MyMapView.IdentifyLayerAsync(_damageLayer, e.Position, 2, false);
// Reset the instruction label and return if there are no results.
if (!identifyResult.GeoElements.Any())
{
InstructionLabel.Text = "Tap an existing feature to edit its attribute.";
return;
}
// Get the tapped feature.
_selectedFeature = (ArcGISFeature)identifyResult.GeoElements.First();
// Select the feature.
_damageLayer.SelectFeature(_selectedFeature);
// Update instruction label.
InstructionLabel.Text = "Select damage type:";
// Update the UI for the selection.
UpdateUIForSelectedFeature();
}
catch (Exception ex)
{
await new MessageDialog2(ex.ToString(), "Error selecting feature.").ShowAsync();
}
}
private void UpdateUIForSelectedFeature()
{
// Get the current value.
string currentAttributeValue = _selectedFeature.Attributes[AttributeFieldName].ToString();
// Update the ComboBox selection without triggering the event.
DamageTypeDropDown.SelectionChanged -= DamageType_Changed;
DamageTypeDropDown.SelectedValue = currentAttributeValue;
DamageTypeDropDown.SelectionChanged += DamageType_Changed;
// Enable the ComboBox.
DamageTypeDropDown.Visibility = Visibility.Visible;
}
private async void DamageType_Changed(object sender, SelectionChangedEventArgs e)
{
// Skip if nothing is selected.
if (DamageTypeDropDown.SelectedIndex == -1)
{
return;
}
try
{
// Get the new value.
string selectedAttributeValue = DamageTypeDropDown.SelectedValue.ToString();
// Load the feature.
await _selectedFeature.LoadAsync();
// Update the attribute value.
_selectedFeature.SetAttributeValue(AttributeFieldName, selectedAttributeValue);
// Update the table.
await _selectedFeature.FeatureTable.UpdateFeatureAsync(_selectedFeature);
// Update the service on the service geodatabase.
ServiceFeatureTable table = (ServiceFeatureTable)_selectedFeature.FeatureTable;
await table.ServiceGeodatabase.ApplyEditsAsync();
await new MessageDialog2($"Edited feature {_selectedFeature.Attributes["objectid"]}", "Success").ShowAsync();
}
catch (Exception ex)
{
await new MessageDialog2(ex.ToString(), "Failed to edit feature.").ShowAsync();
}
}
private void MapView_Tapped_UpdateGeometry(object sender, GeoViewInputEventArgs e)
{
// Select the feature if none selected, move the feature otherwise.
if (_selectedFeature == null)
{
// Select the feature.
TrySelectFeature(e);
}
else
{
// Move the feature.
MoveSelectedFeature(e);
}
}
private async void MoveSelectedFeature(GeoViewInputEventArgs tapEventDetails)
{
try
{
// Get the MapPoint from the EventArgs for the tap.
MapPoint destinationPoint = tapEventDetails.Location;
// Normalize the point - needed when the tapped location is over the international date line.
destinationPoint = (MapPoint)GeometryEngine.NormalizeCentralMeridian(destinationPoint);
// Load the feature.
await _selectedFeature.LoadAsync();
// Update the geometry of the selected feature.
_selectedFeature.Geometry = destinationPoint;
// Apply the edit to the feature table.
await _selectedFeature.FeatureTable.UpdateFeatureAsync(_selectedFeature);
// Push the update to the service with the service geodatabase.
ServiceFeatureTable serviceTable = (ServiceFeatureTable)_selectedFeature.FeatureTable;
await serviceTable.ServiceGeodatabase.ApplyEditsAsync();
await new MessageDialog2($"Moved feature {_selectedFeature.Attributes["objectid"]}", "Success").ShowAsync();
}
catch (Exception ex)
{
await new MessageDialog2(ex.ToString(), "Error when moving feature.").ShowAsync();
}
finally
{
// Reset the selection.
_damageLayer.ClearSelection();
_selectedFeature = null;
}
}
private async void TrySelectFeature(GeoViewInputEventArgs tapEventDetails)
{
try
{
// Perform an identify to determine if a user tapped on a feature.
IdentifyLayerResult identifyResult = await MyMapView.IdentifyLayerAsync(_damageLayer, tapEventDetails.Position, 10, false);
// Do nothing if there are no results.
if (!identifyResult.GeoElements.Any())
{
return;
}
// Get the tapped feature.
_selectedFeature = (ArcGISFeature)identifyResult.GeoElements.First();
// Select the feature.
_damageLayer.SelectFeature(_selectedFeature);
}
catch (Exception ex)
{
await new MessageDialog2(ex.ToString(), "There was a problem.").ShowAsync();
}
}
}
}