Update a feature's location in an online feature service.
Use case
Sometimes users may want to edit features in an online feature service by moving them.
How to use the sample
Tap a feature to select it. Tap again to set the updated location for that feature.
Selecting a feature on the map highlights the feature. Selecting another location on the map moves the feature to the new location. This is done by first obtaining the feature after it is selected. Once the feature is obtained, the feature's geometry is updated by setting the geometry to the new map point. To update the feature in the feature table, call updateFeature and pass in the edited feature. Finally, to update the feature in the service, call applyEdits.
How it works
Create a ServiceFeatureTable object from a URL.
Create a FeatureLayer object from the ServiceFeatureTable.
Select a feature from the FeatureLayer using featureLayer.selectFeatures.
Load the selected feature.
Change the selected feature's location using feature.geometry.
After the change, update the table on the server using applyEdits.
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
// [WriteFile Name=UpdateGeometryFeatureService, Category=EditData]// [Legal]// Copyright 2016 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]import QtQuick
import QtQuick.Controls
import Esri.ArcGISRuntime
Rectangle {
width: 800height: 600property bool featureSelected: falseproperty Point newLocationproperty var selectedFeature: null// Create MapView that contains a MapMapView {
id: mapViewanchors.fill: parentwrapAroundMode: Enums.WrapAroundModeDisabled
Component.onCompleted: {
// Set the focus on MapView to initially enable keyboard navigation forceActiveFocus();
}
Map {
// Set the initial basemap to StreetsBasemap {
initStyle: Enums.BasemapStyleArcGISStreets
}
ViewpointCenter {
Point {
x: -10800000y: 4500000spatialReference: SpatialReference {
wkid: 102100 }
}
targetScale: 3e7 }
FeatureLayer {
id: featureLayer// declare as child of feature layer, as featureTable is the default propertyServiceFeatureTable {
id: featureTableurl: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0"// make sure edits are successfully applied to the serviceonApplyEditsStatusChanged: {
if (applyEditsStatus === Enums.TaskStatusCompleted) {
console.log("successfully updated feature");
}
}
// signal handler for the asynchronous updateFeature methodonUpdateFeatureStatusChanged: {
if (updateFeatureStatus === Enums.TaskStatusCompleted) {
// apply the edits to the service featureTable.applyEdits();
}
}
}
functiondoUpdateAttribute(){
if (selectedFeature.loadStatus === Enums.LoadStatusLoaded) {
selectedFeature.onLoadStatusChanged.disconnect(doUpdateAttribute);
// set the geometry selectedFeature.geometry = newLocation;
// update the feature in the feature table asynchronously featureTable.updateFeature(selectedFeature);
featureSelected = false;
selectedFeature = null;
featureLayer.clearSelection();
}
}
// signal handler for asynchronously fetching the selected featureonSelectedFeaturesStatusChanged: {
if (selectedFeaturesStatus === Enums.TaskStatusCompleted) {
while (selectedFeaturesResult.iterator.hasNext) {
// obtain the feature selectedFeature = selectedFeaturesResult.iterator.next();
selectedFeature.onLoadStatusChanged.connect(doUpdateAttribute);
selectedFeature.load();
}
}
}
// signal handler for selecting featuresonSelectFeaturesStatusChanged: {
if (selectFeaturesStatus === Enums.TaskStatusCompleted) {
if (!selectFeaturesResult.iterator.hasNext)
featureSelected = false;
else featureSelected = true;
}
}
}
}
QueryParameters {
id: params }
onMouseClicked: mouse => {
// if a feature is selected, move it to a new locationif (featureSelected) {
// obtain the new point to move the feature to newLocation = mouse.mapPoint;
// asynchronously fetch the selected feature featureLayer.selectedFeatures();
} else {
// call identify on the mapview mapView.identifyLayerWithMaxResults(featureLayer, mouse.x, mouse.y, 10, false, 1);
}
}
onIdentifyLayerStatusChanged: {
if (identifyLayerStatus === Enums.TaskStatusCompleted) {
if (identifyLayerResult.geoElements.length > 0) {
// get the objectid of the identifed object params.objectIdsAsInts = [identifyLayerResult.geoElements[0].attributes.attributeValue("objectid")];
// query for the feature using the objectid featureLayer.selectFeaturesWithQuery(params, Enums.SelectionModeNew);
}
}
}
}
}