Snap geometry edits

View on GitHubSample viewer app

Use the Geometry Editor to edit a geometry and align it to existing geometries on a map.

screenshot

Use case

A field worker can create new features by editing and snapping the vertices of a geometry to existing features on a map. In a water distribution network, service line features can be represented with the polyline geometry type. By snapping the vertices of a proposed service line to existing features in the network, an exact footprint can be identified to show the path of the service line and what features in the network it connects to. The feature layer containing the service lines can then be accurately modified to include the proposed line.

How to use the sample

To create a geometry, press the create button to choose 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.

Snap settings can be configured by enabling and disabling snapping, feature snapping, geometry guides and snap sources.

To interactively snap a vertex to a feature or graphic, ensure that snapping is enabled for the relevant snap source and move the mouse pointer or drag a vertex to nearby an existing feature or graphic. When the pointer is close to that existing geoelement, the edit position will be adjusted to coincide with (or snap to), edges and vertices of its geometry. Click or release the touch pointer to place the vertex at the snapped location.

To edit a geometry, tap the geometry to be edited in the map to select it and then edit the geometry by tapping and dragging its vertices and snapping them to nearby features or graphics.

To undo changes made to the geometry, press the undo button.

To delete a geometry or a vertex, tap the geometry or vertex to select it and then press the delete button.

To save your edits, press the save button.

How it works

  1. Create a Map from the URL and connect it to the MapView.
  2. Set the map's LoadSettings.featureTilingMode to enabledWithFullResolutionWhenSupported.
  3. Create a GeometryEditor and connect it to the map view.
  4. Call syncSourceSettings after the map's operational layers are loaded and the geometry editor connected to the map view.
  5. Set SnapSettings.isEnabled and SnapSourceSettings.isEnabled to true for the SnapSource of interest.
  6. Toggle geometry guides using SnapSettings.IsGeometryGuidesEnabled and feature snapping using SnapSettings.IsFeatureSnappingEnabled.
  7. Start the geometry editor with a GeometryType.

Relevant API

  • FeatureLayer
  • Geometry
  • GeometryEditor
  • GeometryEditorStyle
  • GraphicsOverlay
  • MapView
  • SnapSettings
  • SnapSource
  • SnapSourceSettings

About the data

The Naperville water distribution network is based on ArcGIS Solutions for Water Utilities and provides a realistic depiction of a theoretical stormwater network.

Additional information

Snapping is used to maintain data integrity between different sources of data when editing, so it is important that each SnapSource provides full resolution geometries to be valid for snapping. This means that some of the default optimizations used to improve the efficiency of data transfer and display of polygon and polyline layers based on feature services are not appropriate for use with snapping.

To snap to polygon and polyline layers, the recommended approach is to set the FeatureLayer's feature tiling mode to FeatureTilingMode::EnabledWithFullResolutionWhenSupported and use the default ServiceFeatureTable feature request mode FeatureRequestMode::OnInteractionCache. Local data sources, such as geodatabases, always provide full resolution geometries. Point and multipoint feature layers are also always full resolution.

Snapping can be used during interactive edits that move existing vertices using the VertexTool or ReticleVertexTool. When adding new vertices, snapping also works with a hover event (such as a mouse move without a mouse button press). Using the ReticleVertexTool to add and move vertices allows users of touch screen devices to clearly see the visual cues for snapping.

Geometry guides are enabled by default when snapping is enabled. These allow for snapping to a point coinciding with, parallel to, perpendicular to or extending an existing geometry.

On supported platforms haptic feedback on SnapState::SnappedToFeature and SnapState::SnappedToGeometryGuide is enabled by default when snapping is enabled. Custom haptic feedback can be configured by setting SnapSettings::isHapticFeedbackEnabled to false and listening to GeometryEditor::snapChanged events to provide specific feedback depending on the SnapState.

Tags

edit, feature, geometry editor, graphics, layers, map, snapping

Sample Code

SnapGeometryEdits.cppSnapGeometryEdits.cppSnapSourceListModel.cppSnapGeometryEdits.hSnapSourceListModel.hGeometryEditorButton.qmlSnapGeometryEdits.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
// [WriteFile Name=SnapGeometryEdits, Category=EditData]
// [Legal]
// Copyright 2024 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

// sample headers
#include "SnapGeometryEdits.h"
#include "SnapSourceListModel.h"

// ArcGIS Maps SDK headers
#include "FeatureLayer.h"
#include "FeatureTable.h"
#include "Geometry.h"
#include "GeometryEditor.h"
#include "GeometryEditorElement.h"
#include "GeometryTypes.h"
#include "Graphic.h"
#include "GraphicListModel.h"
#include "GraphicsOverlay.h"
#include "GraphicsOverlayListModel.h"
#include "IdentifyGraphicsOverlayResult.h"
#include "Layer.h"
#include "LayerListModel.h"
#include "LoadSettings.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Portal.h"
#include "PortalItem.h"
#include "ReticleVertexTool.h"
#include "SimpleFillSymbol.h"
#include "SimpleLineSymbol.h"
#include "SimpleMarkerSymbol.h"
#include "SnapSettings.h"
#include "SnapSourceSettings.h"
#include "SymbolTypes.h"

// Qt headers
#include <QFuture>
#include <QtGlobal>

#ifdef Q_OS_ANDROID
#include "ArcGISRuntimeEnvironment.h"

#include <QCoreApplication>
#include <QJniObject>
#endif

using namespace Esri::ArcGISRuntime;

SnapGeometryEdits::SnapGeometryEdits(QObject* parent /* = nullptr */) :
  QObject(parent),
  m_snapSourceListModel(new SnapSourceListModel(this))
{
  PortalItem* portalItem = new PortalItem("b95fe18073bc4f7788f0375af2bb445e", this);
  m_map = new Map(portalItem, this);

  #ifdef Q_OS_ANDROID
  ArcGISRuntimeEnvironment::setAndroidApplicationContext(QJniObject{QNativeInterface::QAndroidApplication::context()});
  #endif

  m_geometryEditor = new GeometryEditor(this);
  m_graphicsOverlay = new GraphicsOverlay(this);

  // if mobile, use ReticleVertexTool
  #if defined(Q_OS_IOS) || defined(Q_OS_ANDROID)
    m_geometryEditor->setTool(new ReticleVertexTool(this));
  #endif // defined(Q_OS_IOS) || defined(Q_OS_ANDROID)

  connect (m_map, &Map::doneLoading, this, [this]()
  {
    for (Layer* layer : *m_map->operationalLayers())
    {
      connect (layer, &Layer::doneLoading, this, [this]()
      {
        // Enable snap settings after layers load
        if (m_layersLoaded == false)
        {
          m_layersLoaded = true;
          emit layersLoadedChanged();
        }
      });
    }
  });
}

SnapGeometryEdits::~SnapGeometryEdits() = default;

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

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

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

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

  // Enable feature tiling mode to load geometries with full resolution for snapping support
  m_map->loadSettings()->setFeatureTilingMode(FeatureTilingMode::EnabledWithFullResolutionWhenSupported);

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

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

  emit mapViewChanged();
  createInitialSymbols();
  createConnections();
}

// Create symbols used by all graphics
void SnapGeometryEdits::createInitialSymbols()
{
  m_pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Square, QColor(255, 45, 0), 10, this);
  m_multiPointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Square, QColor(255, 45, 0), 10, this);
  m_lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(255, 45, 0), 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);
}

void SnapGeometryEdits::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(), 10, false).then(this, [this](IdentifyGraphicsOverlayResult* result)
      {
        // Handle editing selected graphics, if any
        auto identifyResult = std::unique_ptr<IdentifyGraphicsOverlayResult>(result);
        // 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
        m_geometryEditor->start(m_editingGraphic->geometry());

        emit geometryEditorStartedChanged();
      });
    }
    emit canUndoChanged();
    emit isElementSelectedChanged();
  });

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

bool SnapGeometryEdits::geometryEditorStarted() const
{
  return (m_geometryEditor && m_geometryEditor->isStarted());
}

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

bool SnapGeometryEdits::isElementSelected()
{
  return (m_geometryEditor && m_geometryEditor->selectedElement() && m_geometryEditor->selectedElement()->canDelete());
}

// Toggles snapping using the enabled state from the snap settings
void SnapGeometryEdits::snappingEnabledStatus(bool snappingCheckedState)
{
  m_geometryEditor->snapSettings()->setEnabled(snappingCheckedState);
}

// Toggles geometry guides using the enabled state from the snap settings
void SnapGeometryEdits::geometryGuidesEnabledStatus(bool geometryGuidesCheckedState)
{
  m_geometryEditor->snapSettings()->setGeometryGuidesEnabled(geometryGuidesCheckedState);
}

// Toggles feature snapping using the enabled state from the snap settings
void SnapGeometryEdits::featureSnappingEnabledStatus(bool featureSnappingCheckedState)
{
  m_geometryEditor->snapSettings()->setFeatureSnappingEnabled(featureSnappingCheckedState);
}

// Starts the GeometryEditor using the selected geometry type
void SnapGeometryEdits::startEditor(GeometryEditorMode geometryEditorMode)
{
  switch (geometryEditorMode)
  {
  case GeometryEditorMode::PointMode:
    m_geometryEditor->start(Esri::ArcGISRuntime::GeometryType::Point);
    break;

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

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

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

  default:
    break;
  }
  emit geometryEditorStartedChanged();
}

Symbol* SnapGeometryEdits::determineGeometrySymbol(const Geometry& geometry)
{
  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:
    break;
  }
  return geometrySymbol;
}

// Stops the GeometryEditor and append the new graphic to the graphics overlay
void SnapGeometryEdits::stopEditing()
{
  const Geometry geometry = m_geometryEditor->stop();
  emit geometryEditorStartedChanged();

  if (m_editingGraphic)
  {
    m_editingGraphic->setGeometry(geometry);

    m_editingGraphic->setVisible(true);
    m_editingGraphic = nullptr;

    return;
  }

  Symbol* geometrySymbol = determineGeometrySymbol(geometry);
  if (geometrySymbol)
  {
    m_graphicsOverlay->graphics()->append(new Graphic(geometry, geometrySymbol, this));
  }
}

// Deletes the selected element
void SnapGeometryEdits::deleteSelection()
{
  m_geometryEditor->deleteSelectedElement();
  emit canUndoChanged();
}

// Reverts the last event on the geometry editor
void SnapGeometryEdits::editorUndo()
{
  m_geometryEditor->undo();
  emit canUndoChanged();
}

void SnapGeometryEdits::displaySnapSources()
{
  if (!m_snapSourceListModel)
    return;

  // Sync the snap settings and update the list model
  m_geometryEditor->snapSettings()->syncSourceSettings();
  m_snapSourceListModel->setSnapSourceSettings(m_geometryEditor->snapSettings()->sourceSettings());

  emit snapSourceModelChanged();
}

void SnapGeometryEdits::enableAllLayersInSection(const QString& section)
{
  m_snapSourceListModel->enableAllLayersInSection(section);
  emit snapSourceModelChanged();
}

QAbstractListModel* SnapGeometryEdits::snapSourceListModel() const
{
  return m_snapSourceListModel;
}

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