Delete features (feature service)

View inC++QMLView on GitHubSample viewer app

Delete features from an online feature service.

screenshot

Use case

Sometimes users may want to delete features from an online feature service.

How to use the sample

  • click on a feature on the Map
  • click on the delete button

How it works

  1. Create a ServiceFeatureTable object from a URL.
  2. Create a FeatureLayer from the service feature table.
  3. Select features from the feature layer via selectFeatures().
  4. Remove the selected features from the table using deleteFeatures().
  5. Update the table on the server using applyEdits().

Relevant API

  • Feature
  • FeatureLayer
  • ServiceFeatureTable

Tags

deletion, feature, online, Service, table

Sample Code

DeleteFeaturesFeatureService.qml
Use dark colors for code blocksCopy
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
// [WriteFile Name=DeleteFeaturesFeatureService, 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
import Esri.ArcGISRuntime.Toolkit

Rectangle {
    width: 800
    height: 600

    property Point calloutLocation
    property string damageType


    // Create MapView that contains a Map
    MapView {
        id: mapView
        anchors.fill: parent
        wrapAroundMode: Enums.WrapAroundModeDisabled

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        Map {
            // Set the initial basemap to Streets
            Basemap {
                initStyle: Enums.BasemapStyleArcGISStreets
            }

            // Set the initial viewpoint over The United States
            ViewpointCenter {
                Point {
                    x: -10800000
                    y: 4500000
                    spatialReference: SpatialReference {
                        wkid: 102100
                    }
                }
                targetScale: 3e7
            }

            FeatureLayer {
                id: featureLayer

                // declare as child of feature layer, as featureTable is the default property
                ServiceFeatureTable {
                    id: featureTable
                    url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0"

                    // make sure edits are successfully applied to the service
                    onApplyEditsStatusChanged: {
                        if (applyEditsStatus === Enums.TaskStatusCompleted) {
                            console.log("successfully deleted feature");
                        }
                    }

                    // signal handler for the asynchronous deleteFeature method
                    onDeleteFeatureStatusChanged: {
                        if (deleteFeatureStatus === Enums.TaskStatusCompleted) {
                            // apply the edits to the service
                            featureTable.applyEdits();
                        }
                    }
                }

                // signal handler for asynchronously fetching the selected feature
                onSelectedFeaturesStatusChanged: {
                    if (selectedFeaturesStatus === Enums.TaskStatusCompleted) {
                        while (selectedFeaturesResult.iterator.hasNext) {
                            // obtain the feature
                            const feat = selectedFeaturesResult.iterator.next();

                            // delete the feature in the feature table asynchronously
                            featureTable.deleteFeature(feat);
                        }
                    }
                }

                // signal handler for selecting features
                onSelectFeaturesStatusChanged: {
                    if (selectFeaturesStatus === Enums.TaskStatusCompleted) {
                        if (!selectFeaturesResult.iterator.hasNext)
                            return;

                        const feat = selectFeaturesResult.iterator.next();
                        damageType = feat.attributes.attributeValue("typdamage");
                        calloutLocation = feat.geometry.extent.center;

                        // show the callout
                        callout.showCallout();
                    }
                }
            }
        }

        QueryParameters {
            id: params
        }

        // hide the callout after navigation
        onViewpointChanged: {
            if (callout.calloutVisible)
                callout.dismiss();
        }

        onMouseClicked: mouse => {
            // reset the map callout and update window
            featureLayer.clearSelection();
            if (callout.calloutVisible)
                callout.dismiss();

            //! [DeleteFeaturesFeatureService identify feature]
            // call identify on the feature layer
            const tolerance = 10;
            const returnPopupsOnly = false;
            mapView.identifyLayerWithMaxResults(featureLayer, mouse.x, mouse.y, tolerance, returnPopupsOnly, 1);
            //! [DeleteFeaturesFeatureService identify feature]
        }

        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);
                }
            }
        }

        calloutData {
            location: calloutLocation
            // We use the HTML to bold, increase the pt size, and center-align the the damageType title.
            title: "<br><b><font size=\"+2\">%1</font></b>".arg(damageType)
        }

        // map callout window
        Callout {
            id: callout
            accessoryButtonType: "Custom"
            customImageUrl: "qrc:/Samples/EditData/DeleteFeaturesFeatureService/ic_menu_trash_light.png"
            calloutData: parent.calloutData
            background: Rectangle {
                border.width: 1
                border.color: "lightgrey"
            }
            leaderPosition: Callout.LeaderPosition.Automatic
            onAccessoryButtonClicked: {
                if (callout.calloutVisible)
                    callout.dismiss();
                featureLayer.selectedFeatures();
            }
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.