Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for Qt

Update geometry (feature service)

Sample Viewer View Sample on GitHub

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

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 again to set the updated location for that feature.

How it works

Selecting a feature on the map highlights the feature. Selecting another location on the map moves the feature to the new location. This is done by first obtaining the feature after it is selected. Once the feature is obtained, the feature's geometry is updated by setting the geometry to the new map point. To update the feature in the feature table, call updateFeature and pass in the edited feature. Finally, to update the feature in the service, call applyEdits.

How it works

  1. Create a ServiceFeatureTable object from a URL.
  2. Create a FeatureLayer object from the ServiceFeatureTable.
  3. Select a feature from the FeatureLayer using featureLayer.selectFeatures.
  4. Load the selected feature.
  5. Change the selected feature's location using feature.geometry.
  6. After the change, update the table on the server using applyEdits.

Relevant API

  • Feature
  • FeatureLayer
  • ServiceFeatureTable

Tags

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

Sample Code

import QtQuick 2.6
import QtQuick.Controls 2.2
import QtGraphicalEffects 1.0
import Esri.ArcGISRuntime 100.9

Rectangle {
    width: 800
    height: 600
    
    property bool featureSelected: false
    property Point newLocation
    property var selectedFeature: null

    // Create MapView that contains a Map
    MapView {
        id: mapView
        anchors.fill: parent
        wrapAroundMode: Enums.WrapAroundModeDisabled

        Map {
            // Set the initial basemap to Streets
            BasemapStreets { }

            ViewpointCenter {
                Point {
                    x: -10800000
                    y: 4500000
                    spatialReference: SpatialReference {
                        wkid: 102100
                    }
                }
                targetScale: 3e7
            }

            FeatureLayer {
                id: featureLayer

                // declare as child of feature layer, as featureTable is the default property
                ServiceFeatureTable {
                    id: featureTable
                    url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0"

                    // make sure edits are successfully applied to the service
                    onApplyEditsStatusChanged: {
                        if (applyEditsStatus === Enums.TaskStatusCompleted) {
                            console.log("successfully updated feature");
                        }
                    }

                    // signal handler for the asynchronous updateFeature method
                    onUpdateFeatureStatusChanged: {
                        if (updateFeatureStatus === Enums.TaskStatusCompleted) {
                            // apply the edits to the service
                            featureTable.applyEdits();
                        }
                    }
                }

                function doUpdateAttribute(){
                    if (selectedFeature.loadStatus === Enums.LoadStatusLoaded) {
                        selectedFeature.onLoadStatusChanged.disconnect(doUpdateAttribute);

                        // set the geometry
                        selectedFeature.geometry = newLocation;
                        // update the feature in the feature table asynchronously
                        featureTable.updateFeature(selectedFeature);

                        featureSelected = false;
                        selectedFeature = null;
                        featureLayer.clearSelection();
                    }
                }

                // signal handler for asynchronously fetching the selected feature
                onSelectedFeaturesStatusChanged: {
                    if (selectedFeaturesStatus === Enums.TaskStatusCompleted) {
                        while (selectedFeaturesResult.iterator.hasNext) {
                            // obtain the feature
                            selectedFeature = selectedFeaturesResult.iterator.next();

                            selectedFeature.onLoadStatusChanged.connect(doUpdateAttribute);
                            selectedFeature.load();
                        }
                    }
                }

                // signal handler for selecting features
                onSelectFeaturesStatusChanged: {
                    if (selectFeaturesStatus === Enums.TaskStatusCompleted) {
                        if (!selectFeaturesResult.iterator.hasNext)
                            featureSelected = false;
                        else
                            featureSelected = true;
                    }
                }
            }
        }

        QueryParameters {
            id: params
        }

        onMouseClicked: {
            // if a feature is selected, move it to a new location
            if (featureSelected) {
                // obtain the new point to move the feature to
                newLocation = mouse.mapPoint;
                // asynchronously fetch the selected feature
                featureLayer.selectedFeatures();
            } else {
                // call identify on the mapview
                mapView.identifyLayerWithMaxResults(featureLayer, mouse.x, mouse.y, 10, false, 1);
            }
        }

        onIdentifyLayerStatusChanged: {
            if (identifyLayerStatus === Enums.TaskStatusCompleted) {
                if (identifyLayerResult.geoElements.length > 0) {
                    // get the objectid of the identifed object
                    params.objectIdsAsInts = [identifyLayerResult.geoElements[0].attributes.attributeValue("objectid")];
                    // query for the feature using the objectid
                    featureLayer.selectFeaturesWithQuery(params, Enums.SelectionModeNew);
                }
            }
        }
    }
}