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

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
- Create a
Mapfrom theURLand connect it to theMapView. - Set the map’s
LoadSettings.featureTilingModetoenabledWithFullResolutionWhenSupported. - Create a
GeometryEditorand connect it to the map view. - Call
syncSourceSettingsafter the map’s operational layers are loaded and the geometry editor connected to the map view. - Set
SnapSettings.isEnabledandSnapSourceSettings.isEnabledto true for theSnapSourceof interest. - Toggle geometry guides using
SnapSettings.IsGeometryGuidesEnabledand feature snapping usingSnapSettings.IsFeatureSnappingEnabled. - 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
// [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]
import QtQuickimport QtQuick.Controlsimport QtQuick.Layouts
// This component defines each of the buttons in the Geometry Editor control UI
RoundButton { id: geometryEditorButton
property string buttonName: "" property string iconPath: ""
Layout.fillWidth: true
// Set the focus policy so that the buttons do not take focus from the MapView focusPolicy: Qt.NoFocus
radius: 5
Rectangle { anchors.fill: parent radius: parent.radius opacity: parent.enabled || parent.checked ? 1 : 0.3 color: geometryEditorButton.down ? "#d0d0d0" : "#e0e0e0" }
Image { id: imgComponent anchors { horizontalCenter: parent.horizontalCenter verticalCenter: parent.verticalCenter verticalCenterOffset: -textComponent.height/2 } source: iconPath width: 20 fillMode: Image.PreserveAspectFit }
Text { id: textComponent anchors { top: imgComponent.bottom horizontalCenter: parent.horizontalCenter } text: buttonName font.pixelSize: 8 }}// [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 graphicsvoid 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 settingsvoid SnapGeometryEdits::snappingEnabledStatus(bool snappingCheckedState){ m_geometryEditor->snapSettings()->setEnabled(snappingCheckedState);}
// Toggles geometry guides using the enabled state from the snap settingsvoid SnapGeometryEdits::geometryGuidesEnabledStatus(bool geometryGuidesCheckedState){ m_geometryEditor->snapSettings()->setGeometryGuidesEnabled(geometryGuidesCheckedState);}
// Toggles feature snapping using the enabled state from the snap settingsvoid SnapGeometryEdits::featureSnappingEnabledStatus(bool featureSnappingCheckedState){ m_geometryEditor->snapSettings()->setFeatureSnappingEnabled(featureSnappingCheckedState);}
// Starts the GeometryEditor using the selected geometry typevoid 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 overlayvoid 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 elementvoid SnapGeometryEdits::deleteSelection(){ m_geometryEditor->deleteSelectedElement(); emit canUndoChanged();}
// Reverts the last event on the geometry editorvoid 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;}// [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]
#ifndef SNAPGEOMETRYEDITS_H#define SNAPGEOMETRYEDITS_H
// Qt headers#include <QObject>
namespace Esri::ArcGISRuntime { class Geometry; class GeometryEditor; class GeometryEditor; class Graphic; class GraphicsOverlay; class Map; class MapQuickView; class Portal; class PortalItem; class SimpleFillSymbol; class SimpleLineSymbol; class SimpleMarkerSymbol; class SnapSourceSettings; class Symbol;}
class SnapSourceListModel;class QAbstractListModel;
Q_MOC_INCLUDE("MapQuickView.h");Q_MOC_INCLUDE("QAbstractListModel");
class SnapGeometryEdits : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged) Q_PROPERTY(bool geometryEditorStarted READ geometryEditorStarted NOTIFY geometryEditorStartedChanged) Q_PROPERTY(bool canUndo READ canUndo NOTIFY canUndoChanged) Q_PROPERTY(bool isElementSelected READ isElementSelected NOTIFY isElementSelectedChanged) Q_PROPERTY(bool layersLoaded MEMBER m_layersLoaded NOTIFY layersLoadedChanged) Q_PROPERTY(QAbstractListModel* snapSourceModel READ snapSourceListModel NOTIFY snapSourceModelChanged)
public: explicit SnapGeometryEdits(QObject* parent = nullptr); ~SnapGeometryEdits() override;
enum class GeometryEditorMode { PointMode, MultipointMode, PolylineMode, PolygonMode };
Q_ENUM(GeometryEditorMode)
static void init(); Q_INVOKABLE void startEditor(GeometryEditorMode geometryEditorMode); Q_INVOKABLE void stopEditing(); Q_INVOKABLE void deleteSelection(); Q_INVOKABLE void editorUndo(); Q_INVOKABLE void snappingEnabledStatus(bool checkedValue); Q_INVOKABLE void geometryGuidesEnabledStatus(bool checkedValue); Q_INVOKABLE void featureSnappingEnabledStatus(bool checkedValue); Q_INVOKABLE void displaySnapSources(); Q_INVOKABLE void enableAllLayersInSection(const QString& section);
signals: void canUndoChanged(); void isElementSelectedChanged(); void geometryEditorStartedChanged(); void layersLoadedChanged(); void mapViewChanged(); void pointLayersChanged(); void pointSourceCheckedStateChanged(); void polylineLayersChanged(); void polylineSourceCheckedStateChanged(); void snapSourceModelChanged();
private: Esri::ArcGISRuntime::MapQuickView* mapView() const; void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView); bool geometryEditorStarted() const; bool canUndo(); void createInitialSymbols(); void createConnections(); bool isElementSelected(); QAbstractListModel* snapSourceListModel() const; Esri::ArcGISRuntime::Symbol* determineGeometrySymbol(const Esri::ArcGISRuntime::Geometry& geometry);
Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::GraphicsOverlay* m_graphicsOverlay = nullptr; Esri::ArcGISRuntime::Graphic* m_editingGraphic = nullptr; Esri::ArcGISRuntime::GeometryEditor* m_geometryEditor = nullptr; Esri::ArcGISRuntime::SimpleMarkerSymbol* m_pointSymbol = nullptr; Esri::ArcGISRuntime::SimpleMarkerSymbol* m_multiPointSymbol = nullptr; Esri::ArcGISRuntime::SimpleLineSymbol* m_lineSymbol = nullptr; Esri::ArcGISRuntime::SimpleFillSymbol* m_polygonSymbol = nullptr; bool m_layersLoaded = false; SnapSourceListModel* m_snapSourceListModel = nullptr;};
#endif // SNAPGEOMETRYEDITS_H// [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]
import QtQuickimport QtQuick.Controlsimport QtQuick.Layoutsimport Esri.Samples
Item { // add a mapView component MapView { id: view anchors.fill: parent
Component.onCompleted: { // Set and keep the focus on MapView to enable keyboard navigation forceActiveFocus(); } }
// Declare the C++ instance which creates the map etc. and supply the view SnapGeometryEditsSample { id: snapGeometryEditsSampleModel mapView: view
onGeometryEditorStartedChanged: { if (!geometryEditorStarted) { pointButton.checked = false; multiPointButton.checked = false; lineButton.checked = false; polygonButton.checked = false; } } }
Rectangle { anchors { right: parent.right margins: 10 }
Control { id: control anchors.right: parent.right padding: 5 width: 140
background: Rectangle { color: "black" opacity: .5 }
contentItem: ColumnLayout { id: columns anchors { verticalCenter: parent.verticalCenter horizontalCenter: parent.horizontalCenter }
GridLayout { id: geometryColumn Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter columns: 2
Text { id: geometryHeader Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter Layout.columnSpan: 2 text: "Create" color: "white" font.pixelSize: 16 font.bold: true }
GeometryEditorButton { id: pointButton buttonName: qsTr("Point") iconPath: "qrc:/Samples/EditData/SnapGeometryEdits/iconAssets/point-32.png" checkable: true enabled: !snapGeometryEditsSampleModel.geometryEditorStarted onClicked: snapGeometryEditsSampleModel.startEditor(SnapGeometryEditsSample.PointMode); }
GeometryEditorButton { id: multiPointButton buttonName: qsTr("Multipoint") iconPath: "qrc:/Samples/EditData/SnapGeometryEdits/iconAssets/multipoint-32.png" checkable: true enabled: !snapGeometryEditsSampleModel.geometryEditorStarted onClicked: snapGeometryEditsSampleModel.startEditor(SnapGeometryEditsSample.MultipointMode); }
GeometryEditorButton { id: lineButton buttonName: qsTr("Line") iconPath: "qrc:/Samples/EditData/SnapGeometryEdits/iconAssets/line-32.png" checkable: true enabled: !snapGeometryEditsSampleModel.geometryEditorStarted onClicked: snapGeometryEditsSampleModel.startEditor(SnapGeometryEditsSample.PolylineMode); }
GeometryEditorButton { id: polygonButton buttonName: qsTr("Polygon") iconPath: "qrc:/Samples/EditData/SnapGeometryEdits/iconAssets/polygon-32.png" checkable: true enabled: !snapGeometryEditsSampleModel.geometryEditorStarted onClicked: snapGeometryEditsSampleModel.startEditor(SnapGeometryEditsSample.PolygonMode); } }
GridLayout { id: editingColumn Layout.fillWidth: true Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter columns: 2
Text { id: editingHeader Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter Layout.columnSpan: 2 text: qsTr("Edit") color: "white" font.pixelSize: 16 font.bold: true }
GeometryEditorButton { id: undoButton buttonName: qsTr("Undo") iconPath: "qrc:/Samples/EditData/SnapGeometryEdits/iconAssets/undo-32.png" enabled: snapGeometryEditsSampleModel.geometryEditorStarted && snapGeometryEditsSampleModel.canUndo onClicked: snapGeometryEditsSampleModel.editorUndo(); }
GeometryEditorButton { id: deleteButton buttonName: qsTr("Delete") iconPath: "qrc:/Samples/EditData/SnapGeometryEdits/iconAssets/erase-32.png" enabled: snapGeometryEditsSampleModel.geometryEditorStarted || snapGeometryEditsSampleModel.isElementSelected onClicked: snapGeometryEditsSampleModel.deleteSelection(); }
GeometryEditorButton { id: saveEditsButton buttonName: qsTr("Save") iconPath: "qrc:/Samples/EditData/SnapGeometryEdits/iconAssets/save-32.png" Layout.columnSpan: 2 enabled: snapGeometryEditsSampleModel.geometryEditorStarted onClicked: snapGeometryEditsSampleModel.stopEditing(); }
GeometryEditorButton { id: snapSettingsButton buttonName: qsTr("Snap Settings") iconPath: "qrc:/Samples/EditData/SnapGeometryEdits/iconAssets/settings.png" Layout.columnSpan: 2 enabled: snapGeometryEditsSampleModel.layersLoaded onClicked: { optionPanel.visible = true; snapGeometryEditsSampleModel.displaySnapSources(); } } } } } }
Rectangle { id: optionPanel anchors { left: parent.left top: parent.top bottom: parent.bottom } width: 360 visible: false color: "white" opacity: 1
ListView { id: snapSourceView anchors { fill: parent margins: 10 }
header: ColumnLayout { id: snappingColumn Layout.minimumWidth: optionPanel.width spacing: 0 RowLayout { Layout.minimumWidth: optionPanel.width Layout.minimumHeight: 35
Text { Layout.alignment: Qt.AlignLeft Layout.fillWidth: true Layout.minimumWidth: optionPanel.width * 0.75 text: "Snapping" font.pixelSize: 15 color: "#8434C1" font.bold: true }
Text { Layout.alignment: Qt.AlignRight Layout.fillWidth: true Layout.minimumWidth: optionPanel.width * 0.25 text: "Done" font.pixelSize: 15 color: "#8434C1" font.bold: true MouseArea { anchors.fill: parent onClicked: optionPanel.visible = false; } } }
ColumnLayout { Layout.minimumWidth: optionPanel.width Layout.minimumHeight: 35 spacing: 0 Rectangle { Layout.alignment: Qt.AlignLeft Layout.minimumWidth: snapSourceView.width - (snapSourceView.anchors.margins / 2) Layout.minimumHeight: 35 color: "#E9DFEA"
Text { text: qsTr("Snapping enabled") font.pixelSize: 15 anchors { left: parent.left margins: 10 verticalCenter: parent.verticalCenter } }
Switch { anchors { right: parent.right margins: 10 verticalCenter: parent.verticalCenter } onCheckedChanged: snapGeometryEditsSampleModel.snappingEnabledStatus(checked) } } Rectangle { Layout.alignment: Qt.AlignLeft Layout.minimumWidth: snapSourceView.width - (snapSourceView.anchors.margins / 2) Layout.minimumHeight: 35 color: "#E9DFEA"
Text { text: qsTr("Geometry guides") font.pixelSize: 15 anchors { left: parent.left margins: 10 verticalCenter: parent.verticalCenter } }
Switch { anchors { right: parent.right margins: 10 verticalCenter: parent.verticalCenter } onCheckedChanged: snapGeometryEditsSampleModel.geometryGuidesEnabledStatus(checked) } } Rectangle { Layout.alignment: Qt.AlignLeft Layout.minimumWidth: snapSourceView.width - (snapSourceView.anchors.margins / 2) Layout.minimumHeight: 35 color: "#E9DFEA"
Text { text: qsTr("Feature snapping") font.pixelSize: 15 anchors { left: parent.left margins: 10 verticalCenter: parent.verticalCenter } }
Switch { anchors { right: parent.right margins: 10 verticalCenter: parent.verticalCenter } onCheckedChanged: snapGeometryEditsSampleModel.featureSnappingEnabledStatus(checked) } } } }
model: snapGeometryEditsSampleModel.snapSourceModel
section { property: "section" criteria: ViewSection.FullString delegate: Item { height: 35 width: optionPanel.width
RowLayout { Layout.fillWidth: true Layout.minimumWidth: optionPanel.width Layout.minimumHeight: 25
Text { Layout.alignment: Qt.AlignLeft Layout.fillWidth: true Layout.minimumWidth: optionPanel.width * 0.5 Layout.minimumHeight: 25 color: "#8434C1" text: section + " layers" font.pixelSize: 15 font.bold: true Layout.topMargin: 10 Layout.bottomMargin: 10 }
Text { Layout.alignment: Qt.AlignRight Layout.minimumWidth: optionPanel.width * 0.5 Layout.minimumHeight: 25 text: "Enable All Sources" font.pixelSize: 15 color: "#8434C1" font.bold: true Layout.topMargin: 10 Layout.bottomMargin: 10
MouseArea { anchors.fill: parent onClicked: snapGeometryEditsSampleModel.enableAllLayersInSection(section); } } } } }
delegate: Item { height: 35 width: optionPanel.width id:delegate Rectangle { id: wrapper color: "#E9DFEA" width: snapSourceView.width - (snapSourceView.anchors.margins / 2) height: delegate.height anchors { margins: 15 }
RowLayout { id : row Layout.fillWidth: true Layout.minimumWidth: optionPanel.width width: wrapper.width
Text { Layout.alignment: Qt.AlignLeft Layout.fillWidth: true Layout.minimumWidth: optionPanel.width / 2 text: name font.pixelSize: 15 Layout.leftMargin: 10 }
Switch { Layout.alignment: Qt.AlignRight Layout.fillWidth: true checked: isEnabled onCheckedChanged: isEnabled = checked; } } } } } }}// [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]
// sample headers#include "SnapSourceListModel.h"
// ArcGIS Maps SDK headers#include "FeatureLayer.h"#include "FeatureTable.h"#include "GeometryTypes.h"#include "GraphicsOverlay.h"#include "SnapSourceSettings.h"
// Qt headers#include <QAbstractListModel>#include <QByteArray>#include <QDir>#include <QFileInfo>#include <QHash>#include <QList>#include <QModelIndex>#include <QObject>#include <QVariant>
using namespace Esri::ArcGISRuntime;
SnapSourceListModel::SnapSourceListModel(QObject* parent) : QAbstractListModel(parent){ setupRoles();}
void SnapSourceListModel::setupRoles(){ m_roles[SectionRole] = "section"; m_roles[NameRole] = "name"; m_roles[IsEnabledRole] = "isEnabled";}
void SnapSourceListModel::enableAllLayersInSection(const QString §ion){ beginResetModel(); for (auto* source : m_snapSourceSettings) { if (determineSection(source) == section) source->setEnabled(true); } endResetModel();}
int SnapSourceListModel::rowCount(const QModelIndex& parent) const{ Q_UNUSED(parent); return m_snapSourceSettings.count();}
QVariant SnapSourceListModel::data(const QModelIndex& index, int role) const{ if (index.row() < 0 || index.row() >= m_snapSourceSettings.count()) return QVariant();
SnapSourceSettings* snapSourceSetting = m_snapSourceSettings[index.row()];
QVariant retVal; QString section = determineSection(snapSourceSetting); QString name = determineName(snapSourceSetting);
switch (role) { case SectionRole: retVal = section.isEmpty() ? "Unknown" : section; break; case NameRole: retVal = name.isEmpty() ? "Unknown Name" : name; break; case IsEnabledRole: retVal = snapSourceSetting->isEnabled(); break; default: break; }
return retVal;}
bool SnapSourceListModel::setData(const QModelIndex& index, const QVariant& value, int role){ if (index.isValid()) { SnapSourceSettings* snapSourceSettings = m_snapSourceSettings.at(index.row()); if (snapSourceSettings) { switch (role) { case IsEnabledRole: { if (snapSourceSettings->isEnabled() != value.toBool()) { snapSourceSettings->setEnabled(value.toBool()); emit dataChanged(index, index, QVector<int>() << role); } return true; } default: break; } } }
return false;}
QHash<int, QByteArray> SnapSourceListModel::roleNames() const{ return m_roles;}
QString SnapSourceListModel::determineSection(Esri::ArcGISRuntime::SnapSourceSettings *snapSourceSettings) const{ // determine if it is a feature layer or graphics overlay FeatureLayer* featureLayer = dynamic_cast<FeatureLayer*>(snapSourceSettings->source());
GraphicsOverlay* graphicsOverlay = dynamic_cast<GraphicsOverlay*>(snapSourceSettings->source());
QString section;
if (featureLayer) { GeometryType type = featureLayer->featureTable()->geometryType();
switch(type) { case GeometryType::Point: section = "Point"; break; case GeometryType::Polyline: section = "Polyline"; break; case GeometryType::Polygon: section = "Polygon"; break; default: section = "Unknown"; break; } } else if (graphicsOverlay) { section = "Graphics Overlay"; }
return section;}
QString SnapSourceListModel::determineName(Esri::ArcGISRuntime::SnapSourceSettings *snapSourceSettings) const{ // determine if it is a feature layer or graphics overlay FeatureLayer* featureLayer = dynamic_cast<FeatureLayer*>(snapSourceSettings->source());
GraphicsOverlay* graphicsOverlay = dynamic_cast<GraphicsOverlay*>(snapSourceSettings->source());
if (featureLayer) return featureLayer->name(); else if (graphicsOverlay) return graphicsOverlay->overlayId().isEmpty() ? "Default Graphics Overlay" : graphicsOverlay->overlayId(); else return "Unknown";}
void SnapSourceListModel::setSnapSourceSettings(QList<Esri::ArcGISRuntime::SnapSourceSettings*> snapSourceSettings){ beginResetModel(); m_snapSourceSettings.clear(); m_snapSourceSettings = snapSourceSettings; endResetModel();}// [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]
// 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.
#ifndef SNAPSOURCELISTMODEL_H#define SNAPSOURCELISTMODEL_H
// Qt headers#include <QAbstractListModel>#include <QByteArray>#include <QHash>#include <QList>
namespace Esri::ArcGISRuntime { class SnapSourceSettings;}
class SnapSourceListModel : public QAbstractListModel{ Q_OBJECT
public: enum SnapSourceRoles { SectionRole = Qt::UserRole + 1, NameRole = Qt::UserRole + 2, IsEnabledRole = Qt::UserRole + 3 };
explicit SnapSourceListModel(QObject* parent = nullptr); ~SnapSourceListModel() override = default;
public: void setSnapSourceSettings(QList<Esri::ArcGISRuntime::SnapSourceSettings*> snapSourceSettings); void setupRoles(); int size() { return m_snapSourceSettings.size(); } void enableAllLayersInSection(const QString& section);
// QAbstractItemModel interface int rowCount(const QModelIndex& parent = QModelIndex()) const override; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
protected: QHash<int, QByteArray> roleNames() const override;
private: QHash<int, QByteArray> m_roles; QList<Esri::ArcGISRuntime::SnapSourceSettings*> m_snapSourceSettings; QString determineSection(Esri::ArcGISRuntime::SnapSourceSettings* snapSourceSettings) const; QString determineName(Esri::ArcGISRuntime::SnapSourceSettings* snapSourceSettings) const;};
#endif // SNAPSOURCELISTMODEL_H// [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]
// sample headers#include "SnapGeometryEdits.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false); QGuiApplication app(argc, argv); app.setApplicationName(QString("SnapGeometryEdits"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample SnapGeometryEdits::init();
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
#ifdef ARCGIS_RUNTIME_IMPORT_PATH_2 engine.addImportPath(ARCGIS_RUNTIME_IMPORT_PATH_2);#endif
// Set the source engine.load(QUrl("qrc:/Samples/EditData/SnapGeometryEdits/main.qml"));
return app.exec();}// 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.
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
SnapGeometryEdits { anchors.fill: parent }}