Create and edit geometries

View on GitHub

Use the Geometry Editor to create new point, multipoint, polyline, or polygon geometries or to edit existing geometries by interacting with a map view.

Image of create and edit geometries 1 Image of create and edit geometries 2

Use case

A field worker can mark features of interest on a map using an appropriate geometry. Features such as sample or observation locations, fences or pipelines, and building footprints can be digitized using point, multipoint, polyline, and polygon geometry types. Polyline and polygon geometries can be created and edited using a vertex-based creation and editing tool (i.e. vertex locations specified explicitly via tapping), or using a freehand tool.

How to use the sample

Tap the pencil button to choose a geometry editor tool. Begin interactively sketching on the map view. Tap the pencil button again for editing options.

How it works

  1. Create a GeometryEditor and assign it to a map view with the geometryEditor view modifier.
  2. Set the tool of the geometry editor to the preferred tool.
  3. Use the start(withType:) method on the GeometryEditor to start interactively sketching on the map view.
  4. Use various methods and properties of the GeometryEditor to undo, redo, delete a selected element, clear the sketch, and cancel the sketch.
  5. Edit a tool's InteractionConfiguration to set the GeometryEditorScaleMode to allow either uniform or stretch scale mode.
  6. Save a sketch as a Graphic to a GraphicsOverlay displayed on the map view.

Relevant API

  • Geometry
  • GeometryBuilder
  • GeometryEditor
  • Graphic
  • GraphicsOverlay
  • MapView

Tags

draw, edit, freehand, geometry editor, sketch, vertex

Sample Code

CreateAndEditGeometriesView.swift
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
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
// 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
//
//   https://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 ArcGIS
import SwiftUI

/// A view that shows how to interact with the geometry editor.
struct CreateAndEditGeometriesView: View {
    /// The map to display in the view.
    @State private var map = Map(basemapStyle: .arcGISTopographic)

    /// The model that is required by the menu.
    @StateObject var model = GeometryEditorMenuModel(
        geometryEditor: GeometryEditor(),
        graphicsOverlay: GraphicsOverlay(renderingMode: .dynamic)
    )

    var body: some View {
        VStack {
            MapView(map: map, graphicsOverlays: [model.graphicsOverlay])
                .geometryEditor(model.geometryEditor)
        }
        .toolbar {
            ToolbarItem(placement: .primaryAction) {
                GeometryEditorMenu(model: model)
            }
        }
    }
}

/// A view that provides a menu for geometry editor functionality.
struct GeometryEditorMenu: View {
    /// The model for the menu.
    @ObservedObject var model: GeometryEditorMenuModel

    var body: some View {
        Menu {
            if !model.isStarted {
                // If the geometry editor is not started, show the main menu.
                mainMenuContent
            } else {
                // If the geometry editor is started, show the edit menu.
                editMenuContent
            }
        } label: {
            Label("Geometry Editor", systemImage: "pencil.tip.crop.circle")
        }
    }
}

extension GeometryEditorMenu {
    /// The content of the main menu.
    private var mainMenuContent: some View {
        VStack {
            Button {
                model.startEditing(with: VertexTool(), geometryType: Point.self)
            } label: {
                Label("New Point", systemImage: "smallcircle.filled.circle")
            }

            Button {
                model.startEditing(with: VertexTool(), geometryType: Polyline.self)
            } label: {
                Label("New Line", systemImage: "line.diagonal")
            }

            Button {
                model.startEditing(with: VertexTool(), geometryType: Polygon.self)
            } label: {
                Label("New Area", systemImage: "skew")
            }

            Button {
                model.startEditing(with: VertexTool(), geometryType: Multipoint.self)
            } label: {
                Label("New Multipoint", systemImage: "hand.point.up.braille")
            }

            Button {
                model.startEditing(with: FreehandTool(), geometryType: Polyline.self)
            } label: {
                Label("New Freehand Line", systemImage: "scribble")
            }

            Button {
                model.startEditing(with: FreehandTool(), geometryType: Polygon.self)
            } label: {
                Label("New Freehand Area", systemImage: "lasso")
            }

            Menu("Shapes") {
                Button {
                    model.startEditing(with: ShapeTool(kind: .arrow), geometryType: Polyline.self)
                } label: {
                    Label("New Line Arrow", systemImage: "arrowshape.right")
                }

                Button {
                    model.startEditing(with: ShapeTool(kind: .arrow), geometryType: Polygon.self)
                } label: {
                    Label("New Polygon Arrow", systemImage: "arrowshape.right.fill")
                }

                Button {
                    model.startEditing(with: ShapeTool(kind: .rectangle), geometryType: Polyline.self)
                } label: {
                    Label("New Line Rectangle", systemImage: "rectangle")
                }

                Button {
                    model.startEditing(with: ShapeTool(kind: .rectangle), geometryType: Polygon.self)
                } label: {
                    Label("New Polygon Rectangle", systemImage: "rectangle.fill")
                }

                Button {
                    model.startEditing(with: ShapeTool(kind: .ellipse), geometryType: Polyline.self)
                } label: {
                    Label("New Line Ellipse", systemImage: "circle")
                }

                Button {
                    model.startEditing(with: ShapeTool(kind: .ellipse), geometryType: Polygon.self)
                } label: {
                    Label("New Polygon Ellipse", systemImage: "circle.fill")
                }

                Button {
                    model.startEditing(with: ShapeTool(kind: .triangle), geometryType: Polyline.self)
                } label: {
                    Label("New Line Triangle", systemImage: "triangle")
                }

                Button {
                    model.startEditing(with: ShapeTool(kind: .triangle), geometryType: Polygon.self)
                } label: {
                    Label("New Polygon Triangle", systemImage: "triangle.fill")
                }
            }

            Divider()

            Button(role: .destructive) {
                model.clearSavedSketches()
            } label: {
                Label("Clear Saved Sketches", systemImage: "trash")
            }
            .disabled(!model.canClearSavedSketches)
        }
    }

    /// The content of the editing menu.
    private var editMenuContent: some View {
        VStack {
            Button {
                model.geometryEditor.undo()
            } label: {
                Label("Undo", systemImage: "arrow.uturn.backward")
            }
            .disabled(!model.canUndo)

            Button {
                model.geometryEditor.redo()
            } label: {
                Label("Redo", systemImage: "arrow.uturn.forward")
            }
            .disabled(!model.canRedo)

            Button {
                model.geometryEditor.deleteSelectedElement()
            } label: {
                Label("Delete Selected Element", systemImage: "xmark.square.fill")
            }
            .disabled(deleteButtonIsDisabled)

            Toggle("Uniform Scale", isOn: $model.shouldUniformScale)

            Button(role: .destructive) {
                model.geometryEditor.clearGeometry()
            } label: {
                Label("Clear Current Sketch", systemImage: "trash")
            }
            .disabled(!model.canClearCurrentSketch)

            Divider()

            Button {
                model.save()
            } label: {
                Label("Save Sketch", systemImage: "square.and.arrow.down")
            }
            .disabled(!model.canSave)

            Button {
                model.stop()
            } label: {
                Label("Cancel Sketch", systemImage: "xmark")
            }
        }
    }
}

extension GeometryEditorMenu {
    /// A Boolean value indicating whether the selection can be deleted.
    ///
    /// In some instances deleting the selection may be invalid. One example would be the mid vertex
    /// of a line.
    var deleteButtonIsDisabled: Bool {
        guard let selection = model.selection else { return true }
        return !selection.canBeDeleted
    }
}

/// An object that acts as a view model for the geometry editor menu.
@MainActor
class GeometryEditorMenuModel: ObservableObject {
    /// The geometry editor.
    let geometryEditor: GeometryEditor

    /// The graphics overlay used to save geometries to.
    let graphicsOverlay: GraphicsOverlay

    /// A Boolean value indicating if the geometry editor can perform an undo.
    @Published private(set) var canUndo = false

    /// A Boolean value indicating if the geometry editor can perform a redo.
    @Published private(set) var canRedo = false

    /// The currently selected element.
    @Published private(set) var selection: GeometryEditorElement?

    /// A Boolean value indicating if the geometry can be saved to a graphics overlay.
    @Published private(set) var canSave = false

    /// A Boolean value indicating if the geometry can be cleared from the geometry editor.
    @Published private(set) var canClearCurrentSketch = false

    /// A Boolean value indicating if the saved sketches can be cleared.
    @Published private(set) var canClearSavedSketches = false

    /// The current geometry of the geometry editor.
    @Published private(set) var geometry: Geometry? {
        didSet {
            canUndo = geometryEditor.canUndo
            canRedo = geometryEditor.canRedo
            canClearCurrentSketch = geometry.map { !$0.isEmpty } ?? false
            canSave = geometry?.sketchIsValid ?? false
        }
    }

    /// A Boolean value indicating if the geometry editor has started.
    @Published var isStarted = false

    /// A Boolean value indicating if the scale mode is uniform.
    @Published var shouldUniformScale = false {
        didSet {
            configureGeometryEditorTool(geometryEditor.tool, scaleMode: scaleMode)
        }
    }

    /// The scale mode to be set on the geometry editor.
    private var scaleMode: GeometryEditorScaleMode {
        shouldUniformScale ? .uniform : .stretch
    }

    /// Creates the geometry menu with a geometry editor.
    /// - Parameter geometryEditor: The geometry editor that the menu should interact with.
    /// - Parameter graphicsOverlay: The graphics overlay that is used to save geometries to.
    init(geometryEditor: GeometryEditor, graphicsOverlay: GraphicsOverlay) {
        self.geometryEditor = geometryEditor
        self.graphicsOverlay = graphicsOverlay

        Task { [weak self, geometryEditor] in
            for await geometry in geometryEditor.$geometry {
                self?.geometry = geometry
            }
        }
        Task { [weak self, geometryEditor] in
            for await selection in geometryEditor.$selectedElement {
                self?.selection = selection
            }
        }
    }

    /// Saves the current geometry to the graphics overlay and stops editing.
    /// - Precondition: `canSave`
    func save() {
        precondition(canSave)
        let geometry = geometryEditor.geometry!
        let graphic = Graphic(geometry: geometry, symbol: symbol(for: geometry))
        graphicsOverlay.addGraphic(graphic)
        stop()
        canClearSavedSketches = true
    }

    /// Clears all the saved sketches on the graphics overlay.
    func clearSavedSketches() {
        graphicsOverlay.removeAllGraphics()
        canClearSavedSketches = false
    }

    /// Stops editing with the geometry editor.
    func stop() {
        geometryEditor.stop()
        isStarted = false
    }

    /// Returns the symbology for graphics saved to the graphics overlay.
    /// - Parameter geometry: The geometry of the graphic to be saved.
    /// - Returns: Either a marker or fill symbol depending on the type of provided geometry.
    private func symbol(for geometry: Geometry) -> Symbol {
        switch geometry {
        case is Point, is Multipoint:
            return SimpleMarkerSymbol(style: .circle, color: .blue, size: 20)
        case is Polyline:
            return SimpleLineSymbol(color: .blue, width: 2)
        case is ArcGIS.Polygon:
            return SimpleFillSymbol(
                color: .gray.withAlphaComponent(0.5),
                outline: SimpleLineSymbol(color: .blue, width: 2)
            )
        default:
            fatalError("Unexpected geometry type")
        }
    }

    /// Configures the scale mode for the geometry editor tool.
    /// - Parameters:
    ///   - tool: The geometry editor tool.
    ///   - scaleMode: Preserve the original aspect ratio or scale freely.
    func configureGeometryEditorTool(_ tool: GeometryEditorTool, scaleMode: GeometryEditorScaleMode) {
        switch tool {
        case let tool as FreehandTool:
            tool.configuration.scaleMode = scaleMode
        case let tool as ShapeTool:
            tool.configuration.scaleMode = scaleMode
        case let tool as VertexTool:
            tool.configuration.scaleMode = scaleMode
        default:
            fatalError("Unexpected tool type")
        }
    }

    /// Starts editing with the specified tool and geometry type.
    /// - Parameters:
    ///   - tool: The tool to draw with.
    ///   - geometryType: The type of geometry to draw.
    func startEditing(with tool: GeometryEditorTool, geometryType: Geometry.Type) {
        configureGeometryEditorTool(tool, scaleMode: scaleMode)
        geometryEditor.tool = tool
        geometryEditor.start(withType: geometryType)
        isStarted = true
    }
}

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