Create and edit geometries

View inC++QMLView on GitHubSample viewer app

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

screenshot

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

To create a new geometry, press the button appropriate for the geometry type you want to create (i.e. points, multipoints, polyline, or polygon) and interactively tap and drag on the map view to create the geometry.

To edit an existing geometry, tap the geometry to be edited in the map to select it and then edit the geometry by tapping and dragging elements of the geometry.

When the whole geometry is selected, you can use the control handles to scale and rotate the geometry.

If creating or editing polyline or polygon geometries, choose the desired creation/editing tool from the combo box.

Use the control panel to undo or redo changes made to the geometry, delete a selected element, save the geometry, stop the editing session and discard any edits, and remove all geometries from the map.

How it works

  1. Create a GeometryEditor and set it to the MapView using MapView::setGeometryEditor(GeometryEditor).

  2. Start the GeometryEditor using GeometryEditor::start(GeometryType) to create a new geometry or GeometryEditor::start(Geometry) to edit an existing geometry.

    • If using the Geometry Editor to edit an existing geometry, the geometry must be retrieved from the graphics overlay being used to visualize the geometry prior to calling the start method. To do this:
      • Use MapView::identifyGraphicsOverlayAsync(...) to identify graphics at the location of a tap.
      • Get the IdentifyGraphicsOverlayResult via the MapQuickView::IdentifyGraphicsOverLayCompleted slot
      • Find the desired graphic in the IdentifyGraphicsOverlayResult::graphics() list.
      • Access the geometry associated with the Graphic using Graphic::geometry() - this will be used in the GeometryEditor::start(Geometry) method.
  3. Create VertexTool, FreehandTool, or ShapeTool objects which define how the user interacts with the view to create or edit geometries, using GeometryEditor::setTool(GeometryEditorTool).

  4. Edit a tool's InteractionConfiguration to set the GeometryEditorScaleMode to allow either uniform or stretch scale mode.

  5. Check to see if undo and redo are possible during an editing session using GeometryEditor::canUndo() and GeometryEditor::canRedo(). If it's possible, use GeometryEditor::undo() and GeometryEditor::redo().

  6. Check whether the currently selected GeometryEditorElement can be deleted (GeometryEditor::selectedElement::canDelete()). If the element can be deleted, delete using GeometryEditor::deleteSelectedElement().

  7. Call GeometryEditor::stop() to finish the editing session. The GeometryEditor does not automatically handle the visualization of a geometry output from an editing session. This must be done manually by propagating the geometry returned by GeometryEditor::stop() into a Graphic and a GraphicsOverlay.

    • To create a new Graphic in the GraphicsOverlay:
      • Create a new Graphic with the geometry returned by the GeometryEditor::stop() method and a symbol.
      • Append the Graphic to the GraphicsOverlay's GraphicListModel (i.e. GraphicsOverlay::graphics::append(Graphic)).
    • To update the geometry underlying an existing Graphic in the GraphicsOverlay:
      • Replace the existing Graphic's Geometry property with the geometry returned by the GeometryEditor::stop() method using Graphic::setGeometry(Geometry).

Relevant API

  • Geometry
  • GeometryEditor
  • Graphic
  • GraphicsOverlay
  • MapView

Additional information

The sample opens with the ArcGIS Imagery basemap centered on the island of Inis Meáin (Aran Islands) in Ireland. Inis Meáin comprises a landscape of interlinked stone walls, roads, buildings, archaeological sites, and geological features, producing complex geometrical relationships.

Tags

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

Sample Code

CreateAndEditGeometries.cppCreateAndEditGeometries.cppCreateAndEditGeometries.hGeometryEditorButton.qmlCreateAndEditGeometries.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
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
// [WriteFile Name=CreateAndEditGeometries, Category=Geometry]
// [Legal]
// 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.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "CreateAndEditGeometries.h"

#include "FreehandTool.h"
#include "GeometryEditor.h"
#include "GeometryEditorElement.h"
#include "GeometryEditorTypes.h"
#include "GeometryTypes.h"
#include "Graphic.h"
#include "GraphicListModel.h"
#include "GraphicsOverlay.h"
#include "GraphicsOverlayListModel.h"
#include "IdentifyGraphicsOverlayResult.h"
#include "InteractionConfiguration.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Multipoint.h"
#include "MultipointBuilder.h"
#include "Point.h"
#include "PointCollection.h"
#include "Polygon.h"
#include "PolygonBuilder.h"
#include "Polyline.h"
#include "PolylineBuilder.h"
#include "ShapeTool.h"
#include "SimpleFillSymbol.h"
#include "SimpleLineSymbol.h"
#include "SimpleMarkerSymbol.h"
#include "SpatialReference.h"
#include "SymbolTypes.h"
#include "VertexTool.h"

#include <QFuture>
#include <QUuid>

using namespace Esri::ArcGISRuntime;

CreateAndEditGeometries::CreateAndEditGeometries(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISImagery, this))
{
  m_geometryEditor = new GeometryEditor(this);
  m_graphicsOverlay = new GraphicsOverlay(this);
  m_tempGraphicsParent = new QObject(this);

  // Create the tools to toggle between
  m_vertexTool = new VertexTool(this);
  m_freehandTool = new FreehandTool(this);
  m_arrowTool = ShapeTool::create(ShapeToolType::Arrow, this);
  m_ellipseTool = ShapeTool::create(ShapeToolType::Ellipse, this);
  m_rectangleTool = ShapeTool::create(ShapeToolType::Rectangle, this);
  m_triangleTool = ShapeTool::create(ShapeToolType::Triangle, this);
}

CreateAndEditGeometries::~CreateAndEditGeometries() = default;

void CreateAndEditGeometries::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<CreateAndEditGeometries>("Esri.Samples", 1, 0, "CreateAndEditGeometriesSample");
}

// Set the view (created in QML)
void CreateAndEditGeometries::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;
  m_mapView->setMap(m_map);

  m_mapView->setViewpointCenterAsync(Point(-9.5920, 53.08230, SpatialReference::wgs84()), 5000);

  m_mapView->graphicsOverlays()->append(m_graphicsOverlay);

  // Set the geometry editor on the map view
  m_mapView->setGeometryEditor(m_geometryEditor);

  emit mapViewChanged();

  createInitialSymbols();
  createInitialGraphics();
  createConnections();
}

// Button methods

void CreateAndEditGeometries::startGeometryEditorWithGeometryType(GeometryEditorMode geometryEditorMode)
{
  switch (geometryEditorMode)
  {
    case GeometryEditorMode::PointMode:
      m_geometryEditor->start(GeometryType::Point);
      break;

    case GeometryEditorMode::MultipointMode:
      m_geometryEditor->start(GeometryType::Multipoint);
      break;

    case GeometryEditorMode::PolylineMode:
      m_geometryEditor->start(GeometryType::Polyline);
      break;

    case GeometryEditorMode::PolygonMode:
      m_geometryEditor->start(GeometryType::Polygon);
      break;

    default:
      break;
  }

  emit geometryEditorStartedChanged();
}

void CreateAndEditGeometries::stopEditing(bool saveGeometry)
{
  // Calling stop on the geometry editor returns the currently drawn geometry
  const Geometry geometry = m_geometryEditor->stop();
  emit geometryEditorStartedChanged();

  // If the user wants to discard edits
  if (!saveGeometry)
  {
    // If the user was editing an existing geometry, show the original graphic and clear the selection
    if (m_editingGraphic)
    {
      m_editingGraphic->setVisible(true);
      m_editingGraphic = nullptr;
    }
    return;
  }

  // If the user wants to stop editing and save the geometry
  // If the user was editing an existing graphic, update its geometry
  if (m_editingGraphic)
  {
    m_editingGraphic->setGeometry(geometry);

    // Show the graphic and clear the selection
    m_editingGraphic->setVisible(true);
    m_editingGraphic = nullptr;

    return;
  }

  // If the user was not editing an existing graphic, create a new graphic with the geometry and a symbol
  // Determine what symbology to use for the graphic
  Symbol* geometrySymbol = nullptr;
  switch (geometry.geometryType())
  {
    case GeometryType::Point:
      geometrySymbol = m_pointSymbol;
      break;

    case GeometryType::Multipoint:
      geometrySymbol = m_multiPointSymbol;
      break;

    case GeometryType::Polyline:
      geometrySymbol = m_lineSymbol;
      break;

    case GeometryType::Polygon:
      geometrySymbol = m_polygonSymbol;
      break;

    default:
      return;
  }

  // Append the new graphic to the graphics overlay
  m_graphicsOverlay->graphics()->append(new Graphic(geometry, geometrySymbol, m_tempGraphicsParent));
}

void CreateAndEditGeometries::setTool(GeometryEditorToolType toolType)
{
  switch (toolType)
  {
    case GeometryEditorToolType::Vertex:
      m_geometryEditor->setTool(m_vertexTool);
      break;

    case GeometryEditorToolType::Freehand:
      m_geometryEditor->setTool(m_freehandTool);
      break;

    case GeometryEditorToolType::Arrow:
      m_geometryEditor->setTool(m_arrowTool);
      break;

    case GeometryEditorToolType::Ellipse:
      m_geometryEditor->setTool(m_ellipseTool);
      break;

    case GeometryEditorToolType::Rectangle:
      m_geometryEditor->setTool(m_rectangleTool);
      break;

    case GeometryEditorToolType::Triangle:
      m_geometryEditor->setTool(m_triangleTool);
      break;

    default:
      break;
  }
}

void CreateAndEditGeometries::clearGraphics()
{
  m_graphicsOverlay->graphics()->clear();

  // Clearing the graphics overlay does not delete the graphics it contains
  // We can delete the graphics' shared parent to easily delete all of them and prevent memory leaks
  delete m_tempGraphicsParent;
  m_tempGraphicsParent = new QObject(this);
}

void CreateAndEditGeometries::undo()
{
  if (m_geometryEditor->canUndo())
    m_geometryEditor->undo();
  emit canUndoOrRedoChanged();
}

void CreateAndEditGeometries::redo()
{
  if (m_geometryEditor->canRedo())
    m_geometryEditor->redo();
  emit canUndoOrRedoChanged();
}

void CreateAndEditGeometries::deleteSelectedElement()
{
  m_geometryEditor->deleteSelectedElement();
  emit canUndoOrRedoChanged();
}

void CreateAndEditGeometries::setScaleMode(ScaleMode scaleMode)
{
  const GeometryEditorScaleMode toolScaleMode = (scaleMode == ScaleMode::UniformScaleMode) ? GeometryEditorScaleMode::Uniform : GeometryEditorScaleMode::Stretch;

  // Set the scale mode on all available tools
  m_vertexTool->configuration()->setScaleMode(toolScaleMode);
  m_freehandTool->configuration()->setScaleMode(toolScaleMode);
  m_arrowTool->configuration()->setScaleMode(toolScaleMode);
  m_ellipseTool->configuration()->setScaleMode(toolScaleMode);
  m_rectangleTool->configuration()->setScaleMode(toolScaleMode);
  m_triangleTool->configuration()->setScaleMode(toolScaleMode);
}

MapQuickView* CreateAndEditGeometries::mapView() const
{
  return m_mapView;
}

// Create slots for asynchronous signals
void CreateAndEditGeometries::createConnections()
{
  // Allow user to edit existing graphics by clicking on them
  connect(m_mapView, &MapQuickView::mouseClicked, this, [this](const QMouseEvent& mouseEvent)
  {
    if (!m_geometryEditor->isStarted())
    {
      m_mapView->identifyGraphicsOverlayAsync(m_graphicsOverlay, mouseEvent.position(), 5 ,false).then(this,
      [this](IdentifyGraphicsOverlayResult* rawResult){
        // Handle editing selected graphics, if any

        auto identifyResult = std::unique_ptr<IdentifyGraphicsOverlayResult>(rawResult);

        // Return if no graphics were identified
        if (identifyResult->graphics().isEmpty())
          return;

        m_editingGraphic = identifyResult->graphics().first();

        // Hide the graphic currently being edited
        m_editingGraphic->setVisible(false);

        // Start the geometry editor with the graphic's geometry. This does not directly affect the graphic.
        m_geometryEditor->start(m_editingGraphic->geometry());

        emit geometryEditorStartedChanged();
      });
    }
    emit canUndoOrRedoChanged();
    emit elementIsSelectedChanged();
  });

  // Enable or disable buttons when mouse is released (ie after a drag operation)
  connect(m_mapView, &MapQuickView::mouseReleased, this, [this](const QMouseEvent&)
  {
    emit canUndoOrRedoChanged();
    emit elementIsSelectedChanged();
  });
}

// State checks to control when buttons are enabled
bool CreateAndEditGeometries::geometryEditorStarted() const
{
  return (m_geometryEditor && m_geometryEditor->isStarted());
}

bool CreateAndEditGeometries::canUndo()
{
  return (m_geometryEditor && m_geometryEditor->canUndo());
}

bool CreateAndEditGeometries::canRedo()
{
  return (m_geometryEditor && m_geometryEditor->canRedo());
}

bool CreateAndEditGeometries::elementIsSelected()
{
  return (m_geometryEditor && m_geometryEditor->selectedElement() && m_geometryEditor->selectedElement()->canDelete());
}

// Create symbols used by all graphics
void CreateAndEditGeometries::createInitialSymbols()
{
  m_pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Square, QColor(255, 45, 0), 10, this);
  m_multiPointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, QColor(255, 255, 0), 5, this);
  m_lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(0, 0, 255), 2, this);
  m_polygonSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, QColor(255, 0, 0, 75),
                                         new SimpleLineSymbol(SimpleLineSymbolStyle::Dash, QColor(0, 0, 0), 1.0, this), this);
}

// Create graphics that are present upon sample instantiation
void CreateAndEditGeometries::createInitialGraphics()
{
  // Create a temporary parent to give all the builders so we can easily delete them later
  QObject* tempParent = new QObject(this);

  // Create point geometry
  const Point house(-9.59309629, 53.0830063, SpatialReference::wgs84());

  // Create multipoint geometry
  MultipointBuilder* multipointBuilder = new MultipointBuilder(SpatialReference::wgs84(), tempParent);
  PointCollection* pointCollection = new PointCollection(SpatialReference::wgs84(), tempParent);
  pointCollection->addPoints(
        {
          Point(-9.59386587, 53.08289651), Point(-9.59330546, 53.08256400),
          Point(-9.59326997, 53.08304595), Point(-9.59250034, 53.08286101),
          Point(-9.59286835, 53.08311506), Point(-9.59370896, 53.08234917),
          Point(-9.59341755, 53.08286662), Point(-9.59246485, 53.08294507),
          Point(-9.59241815, 53.08284607), Point(-9.59307943, 53.08234731)
        });
  multipointBuilder->setPoints(pointCollection);
  const Multipoint outbuildings(multipointBuilder->toMultipoint());

  // Create polyline geometries
  PolylineBuilder* polylineBuilder = new PolylineBuilder(SpatialReference::wgs84(), tempParent);
  polylineBuilder->addPoints(
        {
          Point(-9.59486423, 53.08169453), Point(-9.5947812, 53.081754310),
          Point(-9.59475464, 53.08189379), Point(-9.59494393, 53.08213622),
          Point(-9.59464173, 53.08240521), Point(-9.59413694, 53.08260115),
          Point(-9.59357903, 53.08292660), Point(-9.59335984, 53.08311589),
          Point(-9.59318051, 53.08316903), Point(-9.59301779, 53.08322216),
          Point(-9.59264252, 53.08370038), Point(-9.59250636, 53.08383986)
        });

  const Polyline road1(polylineBuilder->toPolyline());
  delete polylineBuilder;

  polylineBuilder = new PolylineBuilder(SpatialReference::wgs84(), tempParent);
  polylineBuilder->addPoints(
        {
          Point(-9.59400079, 53.08136244), Point(-9.59395761, 53.08149528),
          Point(-9.59368862, 53.08170450), Point(-9.59358235, 53.08219267),
          Point(-9.59331667, 53.08290335), Point(-9.59314398, 53.08314246),
          Point(-9.59306760, 53.08330519), Point(-9.59303439, 53.08351109),
          Point(-9.59301447, 53.08363728), Point(-9.59293809, 53.08387307)
        });

  const Polyline road2(polylineBuilder->toPolyline());

  // Create polygon geometry
  PolygonBuilder* polygonBuilder = new PolygonBuilder(SpatialReference::wgs84(), tempParent);
  polygonBuilder->addPoints(
        {
          Point(-9.59350122, 53.08320723), Point(-9.59345177, 53.08333534),
          Point(-9.59309789, 53.08327198), Point(-9.59300344, 53.08317992),
          Point(-9.59221827, 53.08304034), Point(-9.59220706, 53.08287782),
          Point(-9.59229486, 53.08280871), Point(-9.59236398, 53.08268915),
          Point(-9.59255263, 53.08256769), Point(-9.59265165, 53.08237906),
          Point(-9.59287552, 53.08241478), Point(-9.59292812, 53.08230120),
          Point(-9.59322940, 53.08235022), Point(-9.59342188, 53.08260009),
          Point(-9.59354382, 53.08238728), Point(-9.59365852, 53.08203535),
          Point(-9.59408443, 53.08210446), Point(-9.59448232, 53.08224456),
          Point(-9.59436090, 53.08243697), Point(-9.59458319, 53.08245939),
          Point(-9.59439639, 53.08264619), Point(-9.59433288, 53.08279750),
          Point(-9.59404707, 53.08323649), Point(-9.59350122, 53.08320723)
        });

  const Polygon boundary(polygonBuilder->toPolygon());

  // Delete the builders' parent to clean up all objects used in creating the graphics
  delete tempParent;

  // Create graphics with the geometries
  m_graphicsOverlay->graphics()->append(
        {
          new Graphic(boundary, m_polygonSymbol, m_tempGraphicsParent),
          new Graphic(road2, m_lineSymbol, m_tempGraphicsParent),
          new Graphic(road1, m_lineSymbol, m_tempGraphicsParent),
          new Graphic(outbuildings, m_multiPointSymbol, m_tempGraphicsParent),
          new Graphic(house, m_pointSymbol, m_tempGraphicsParent)
        });
}

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