Update geometries (feature service)

View on GitHub
Sample viewer app

Update a feature's location in an online feature service.

Map with features Selected feature Feature in edit mode Updated map

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 the icon in the callout to begin updating the geometry. Tap the feature to select it, then tap on the map to update the geometry. Tap the "Done" button to complete the changes. An alert will be shown confirming success or failure.

How it works

  1. Create an AGSServiceFeatureTable object from a URL.
  2. Create an AGSFeatureLayer object from the AGSServiceFeatureTable.
  3. Find the layer of the selected feature using AGSGeoView.identifyLayer(_:screenPoint:tolerance:returnPopupsOnly:maximumResults:completion:), which returns an AGSIdentifyLayerResult used to get the selected feature.
  4. Load the selected feature.
  5. Change the selected feature's AGSFeature.geometry to change its location.
  6. After the change, update the table on the server using AGSServiceFeatureTable.applyEdits(completion:).

Relevant API

  • AGSFeature
  • AGSFeatureLayer
  • AGSServiceFeatureTable

Tags

editing, feature layer, feature table, moving, service, updating

Sample Code

EditGeometryViewController.swift
                                                                                                                                                          
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
// 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.

import UIKit
import ArcGIS

class EditGeometryViewController: UIViewController, AGSGeoViewTouchDelegate, AGSCalloutDelegate {
    @IBOutlet private weak var mapView: AGSMapView!
    @IBOutlet private weak var toolbar: UIToolbar!
    @IBOutlet private var toolbarBottomConstraint: NSLayoutConstraint!

    private var map: AGSMap!
    private var featureTable: AGSServiceFeatureTable!
    private var featureLayer: AGSFeatureLayer!
    private var lastQuery: AGSCancelable!

    private var selectedFeature: AGSArcGISFeature!
    private let featureServiceURL = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0"

    override func viewDidLoad() {
        super.viewDidLoad()

        // add the source code button item to the right of navigation bar
        (self.navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = ["EditGeometryViewController"]

        self.map = AGSMap(basemapStyle: .arcGISOceans)

        self.featureTable = AGSServiceFeatureTable(url: URL(string: featureServiceURL)!)
        let featureLayer = AGSFeatureLayer(featureTable: self.featureTable)

        self.map.operationalLayers.add(featureLayer)

        self.mapView.map = self.map
        self.mapView.setViewpoint(AGSViewpoint(center: AGSPoint(x: -9030446.96, y: 943791.32, spatialReference: .webMercator()), scale: 2e6))
        self.mapView.touchDelegate = self

        // store the feature layer for later use
        self.featureLayer = featureLayer
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        // default state for toolbar is off
        self.setToolbarVisibility(visible: false)
    }

    func setToolbarVisibility(visible: Bool) {
        toolbarBottomConstraint.constant = visible ? 0 : -toolbar.frame.height - view.safeAreaInsets.bottom

        UIView.animate(withDuration: 0.3) { [weak self] in
            self?.view.layoutIfNeeded()
        }
    }

    func applyEdits() {
        self.featureTable.applyEdits { [weak self] (_, error) in
            if let error = error {
                self?.presentAlert(error: error)
            } else {
                self?.presentAlert(message: "Saved successfully!")
            }
            // un hide the feature
            self?.featureLayer.setFeature(self!.selectedFeature, visible: true)
        }
    }

    // MARK: - AGSGeoViewTouchDelegate

    func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        if let lastQuery = self.lastQuery {
            lastQuery.cancel()
        }

        // hide the callout
        self.mapView.callout.dismiss()

        self.lastQuery = self.mapView.identifyLayer(self.featureLayer, screenPoint: screenPoint, tolerance: 12, returnPopupsOnly: false, maximumResults: 1) { [weak self] (identifyLayerResult: AGSIdentifyLayerResult) in
            if let error = identifyLayerResult.error {
                print(error)
            } else if let features = identifyLayerResult.geoElements as? [AGSArcGISFeature],
                let feature = features.first {
                // show callout for the first feature
                let title = feature.attributes["typdamage"] as! String
                self?.mapView.callout.title = title
                self?.mapView.callout.delegate = self
                self?.mapView.callout.show(for: feature, tapLocation: mapPoint, animated: true)
                // update selected feature
                self?.selectedFeature = feature
            }
        }
    }

    // MARK: - AGSCalloutDelegate

    func didTapAccessoryButton(for callout: AGSCallout) {
        // hide the callout
        self.mapView.callout.dismiss()

        // add the default geometry
        let point = self.selectedFeature.geometry as! AGSPoint

        // instantiate sketch editor with selected feature's geometry
        self.mapView.sketchEditor = AGSSketchEditor()

        // enable the sketch editor to start tracking user gesture
        self.mapView.sketchEditor?.start(with: point)

        // show the toolbar
        self.setToolbarVisibility(visible: true)

        // hide the feature for time being
        self.featureLayer.setFeature(self.selectedFeature, visible: false)
    }

    // MARK: - Actions

    @IBAction func doneAction() {
        if let newGeometry = self.mapView.sketchEditor?.geometry {
            self.selectedFeature.geometry = newGeometry
            self.featureTable.update(self.selectedFeature) { [weak self] (error) in
                if let error = error {
                    self?.presentAlert(error: error)

                    // un hide the feature
                    self?.featureLayer.setFeature(self!.selectedFeature, visible: true)
                } else {
                    // apply edits
                    self?.applyEdits()
                }
            }
        }

        // hide toolbar
        self.setToolbarVisibility(visible: false)

        // disable sketch editor
        self.mapView.sketchEditor?.stop()

        // clear sketch editor
        self.mapView.sketchEditor?.clearGeometry()
    }
}

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