Edit and sync features

View inC++QMLView on GitHubSample viewer app

Synchronize offline edits with a feature service.

screenshot

Use case

A survey worker who works in an area without an internet connection could take a geodatabase of survey features offline at their office, make edits and add new features to the offline geodatabase in the field, and sync the updates with the online feature service after returning to the office.

How to use the sample

Pan and zoom to position the red rectangle around the area you want to take offline. Click "Generate geodatabase" to take the area offline. When complete, the map will update to only show the offline area. To edit features, click to select a feature, and click again anywhere else on the map to move the selected feature to the clicked location. To sync the edits with the feature service, click the "Sync geodatabase" button.

How it works

  1. Create a GeodatabaseSyncTask from a URL to a feature service.
  2. Create GenerateGeodatabaseParameters, passing in an Envelope extent as the parameter.
  3. Create a GenerateGeodatabaseJob from the GeodatabaseSyncTask using generateGeodatabase(...), passing in the parameters and a path to where the geodatabase should be downloaded locally.
  4. Start the job and get the result Geodatabase.
  5. Load the geodatabase and get its feature tables. Create feature layers from the feature tables and add them to the map's operational layers collection.
  6. Create SyncGeodatabaseParameters and set the sync direction.
  7. Create a SyncGeodatabaseJob from GeodatabaseSyncTask using syncGeodatabase(...) passing in the parameters and geodatabase as arguments.
  8. Start the sync job to synchronize the edits.

Relevant API

  • FeatureLayer
  • FeatureTable
  • GenerateGeodatabaseJob
  • GenerateGeodatabaseParameters
  • GeodatabaseSyncTask
  • SyncGeodatabaseJob
  • SyncGeodatabaseParameters
  • SyncLayerOption

Offline Data

To set up the sample's offline data, see the Use offline data in the samples section of the Qt Samples repository overview.

Link Local Location
San Francisco Streets TPKX <userhome>/ArcGIS/Runtime/Data/tpkx/SanFrancisco.tpkx

About the data

The basemap uses an offline tile package of San Francisco. The online feature service has features with wildfire information.

Tags

feature service, geodatabase, offline, synchronize

Sample Code

EditAndSyncFeatures.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// [WriteFile Name=EditAndSyncFeatures, Category=EditData]
// [Legal]
// Copyright 2016 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 QtQuick 2.6
import QtQuick.Controls 2.2
import QtGraphicalEffects 1.0
import Esri.ArcGISRuntime 100.15
import Esri.ArcGISExtras 1.1

Rectangle {
    width: 800
    height: 600

    readonly property url dataPath: System.userHomePath + "/ArcGIS/Runtime/Data/"
    readonly property url outputGdb: System.temporaryFolder.url + "/WildfireQml_%1.geodatabase".arg(new Date().getTime().toString())
    readonly property string featureServiceUrl: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Sync/WildfireSync/FeatureServer"
    property Envelope generateExtent: null
    property string statusText: ""
    property string featureLayerId: "0"
    property string instructionText: ""
    property bool isOffline: false
    property var selectedFeature: null
    property Geodatabase offlineGdb: null

    MapView {
        id: mapView
        anchors.fill: parent

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        Map {
            id: map

            Basemap {
                ArcGISTiledLayer {
                    TileCache {
                        path: dataPath + "tpkx/SanFrancisco.tpkx"
                    }
                }
            }

            FeatureLayer {
                ServiceFeatureTable {
                    url: featureServiceUrl + "/" + featureLayerId
                }
            }

            // set an initial viewpoint
            ViewpointExtent {
                Envelope {
                    xMax: -122.43843016064368
                    xMin: -122.50017717584528
                    yMax: 37.81638388695054
                    yMin: 37.745000054347535
                    spatialReference: Factory.SpatialReference.createWgs84()
                }
            }
        }

        onMouseClicked: {
            if (isOffline && !selectedFeature) {
                // identify to select a feature
                mapView.identifyLayer(map.operationalLayers.get(0), mouse.x, mouse.y, 5, false);
            } else if (isOffline && selectedFeature) {

                // connect to feature table signal
                const featureTable = map.operationalLayers.get(0).featureTable;
                featureTable.updateFeatureStatusChanged.connect(()=> {
                    if (featureTable.updateFeatureStatus === Enums.TaskStatusCompleted) {
                        // clear selections
                        featureTable.featureLayer.clearSelection();
                        selectedFeature = null;
                        instructionText = "Tap the sync button";
                        syncButton.visible = true;
                    }
                });

                // update selected feature's geometry
                selectedFeature.geometry = mouse.mapPoint;
                featureTable.updateFeature(selectedFeature);
            }
        }

        onIdentifyLayerStatusChanged: {
            if (identifyLayerStatus === Enums.TaskStatusCompleted) {
                const featureLayer = map.operationalLayers.get(0);

                // clear any previous selections
                featureLayer.clearSelection();
                selectedFeature = null;

                // select the feature
                if (identifyLayerResult.geoElements.length > 0) {
                    const geoElement = identifyLayerResult.geoElements[0];
                    featureLayer.selectFeature(geoElement);
                    selectedFeature = geoElement;
                    instructionText = "Tap on map to move feature";
                }
            }
        }
    }

    //! [EditAndSyncFeatures create GeodatabaseSyncTask]
    // create the GeodatabaseSyncTask to generate the local geodatabase
    GeodatabaseSyncTask {
        id: geodatabaseSyncTask
        url: featureServiceUrl
        property var generateJob
        property var syncJob

        function executeGenerate() {
            // execute the asynchronous task and obtain the job
            generateJob = generateGeodatabase(generateParameters, outputGdb);

            // check if the job is valid
            if (generateJob) {

                // show the sync window
                syncWindow.visible = true;

                // connect to the job's status changed signal to know once it is done
                generateJob.statusChanged.connect(updateGenerateJobStatus);

                // start the job
                generateJob.start();
            } else {
                // a valid job was not obtained, so show an error
                syncWindow.visible = true;
                statusText = "Generate failed";
                syncWindow.hideWindow(5000);
            }
        }

        function updateGenerateJobStatus() {
            switch(generateJob.jobStatus) {
            case Enums.JobStatusFailed:
                statusText = "Generate failed";
                syncWindow.hideWindow(5000);
                break;
            case Enums.JobStatusNotStarted:
                statusText = "Job not started";
                break;
            case Enums.JobStatusPaused:
                statusText = "Job paused";
                break;
            case Enums.JobStatusStarted:
                statusText = "In progress...";
                break;
            case Enums.JobStatusSucceeded:
                statusText = "Complete";
                syncWindow.hideWindow(1500);
                offlineGdb = generateJob.geodatabase;
                displayLayersFromGeodatabase();
                isOffline = true;
                break;
            default:
                break;
            }
        }

        function executeSync() {
            // execute the asynchronous task and obtain the job
            syncJob = syncGeodatabase(syncParameters, offlineGdb);

            // check if the job is valid
            if (syncJob) {

                // show the sync window
                syncWindow.visible = true;

                // connect to the job's status changed signal to know once it is done
                syncJob.statusChanged.connect(updateSyncJobStatus);

                // start the job
                syncJob.start();
            } else {
                // a valid job was not obtained, so show an error
                syncWindow.visible = true;
                statusText = "Sync failed";
                syncWindow.hideWindow(5000);
            }
        }

        function updateSyncJobStatus() {
            switch(syncJob.jobStatus) {
            case Enums.JobStatusFailed:
                statusText = "Sync failed";
                syncWindow.hideWindow(5000);
                break;
            case Enums.JobStatusNotStarted:
                statusText = "Job not started";
                break;
            case Enums.JobStatusPaused:
                statusText = "Job paused";
                break;
            case Enums.JobStatusStarted:
                statusText = "In progress...";
                break;
            case Enums.JobStatusSucceeded:
                statusText = "Complete";
                syncWindow.hideWindow(1500);
                isOffline = true;
                break;
            default:
                break;
            }
        }

        // ...
        //! [EditAndSyncFeatures create GeodatabaseSyncTask]

        function displayLayersFromGeodatabase() {
            // remove the original online feature layers
            map.operationalLayers.clear();

            // load the geodatabase to access the feature tables
            offlineGdb.loadStatusChanged.connect(()=> {
                if (offlineGdb.loadStatus === Enums.LoadStatusLoaded) {
                    // create a feature layer from each feature table, and add to the map
                    for (let i = 0; i < offlineGdb.geodatabaseFeatureTables.length; i++) {
                        const featureTable = offlineGdb.geodatabaseFeatureTables[i];
                        const featureLayer = ArcGISRuntimeEnvironment.createObject("FeatureLayer");
                        featureLayer.featureTable = featureTable;
                        map.operationalLayers.append(featureLayer);
                    }

                    // hide the extent rectangle and download button
                    extentRectangle.visible = false;
                    syncButton.visible = false;
                }
            });
            offlineGdb.load();
        }

        Component.onDestruction: {
            generateJob.statusChanged.disconnect(updateGenerateJobStatus);
            syncJob.statusChanged.disconnect(updateSyncJobStatus);
        }
    }

    //! [EditAndSyncFeatures GeodatabaseSyncTask parameters]
    // create the generate geodatabase parameters
    GenerateGeodatabaseParameters {
        id: generateParameters
        extent: generateExtent
        outSpatialReference: SpatialReference { wkid: 3857 }
        returnAttachments: false

        // only generate a geodatabase with 1 layer
        layerOptions: [
            GenerateLayerOption {
                layerId: featureLayerId
            }
        ]
    }

    // create the sync geodatabase parameters
    SyncGeodatabaseParameters {
        id: syncParameters

        // only sync the 1 layer
        layerOptions: [
            SyncLayerOption {
                layerId: featureLayerId
            }
        ]
        // push up edits, and receive any new edits
        geodatabaseSyncDirection: Enums.SyncDirectionBidirectional
    }
    //! [EditAndSyncFeatures GeodatabaseSyncTask parameters]

    // create an extent rectangle for the output geodatabase
    Rectangle {
        id: extentRectangle
        anchors.centerIn: parent
        width: parent.width - (50)
        height: parent.height - (125)
        color: "transparent"
        border {
            color: "red"
            width: 3
        }
    }

    // Create the button to generate/sync geodatabase
    Rectangle {
        id: syncButton
        property bool pressed: false
        anchors {
            horizontalCenter: parent.horizontalCenter
            bottom: parent.bottom
            bottomMargin: 23
        }

        width: isOffline ? 175 : 200
        height: 35
        color: pressed ? "#959595" : "#D6D6D6"
        radius: 8
        border {
            color: "#585858"
            width: 1
        }

        Row {
            anchors.fill: parent
            spacing: 5
            Image {
                width: 38
                height: width
                source: isOffline ? "qrc:/Samples/EditData/EditAndSyncFeatures/sync.png" : "qrc:/Samples/EditData/EditAndSyncFeatures/download.png"
            }

            Text {
                anchors.verticalCenter: parent.verticalCenter
                text: isOffline ? "Sync Geodatabase" : "Generate Geodatabase"
                font.pixelSize: 14
                color: "#474747"
            }
        }

        MouseArea {
            anchors.fill: parent
            onPressed: syncButton.pressed = true
            onReleased: syncButton.pressed = false
            onClicked: {
                if (isOffline) {
                    instructions.visible = false;
                    geodatabaseSyncTask.executeSync();
                } else {
                    getRectangleEnvelope();
                    geodatabaseSyncTask.executeGenerate();
                }
            }

            function getRectangleEnvelope() {
                const corner1 = mapView.screenToLocation(extentRectangle.x, extentRectangle.y);
                const corner2 = mapView.screenToLocation((extentRectangle.x + extentRectangle.width), (extentRectangle.y + extentRectangle.height));
                const envBuilder = ArcGISRuntimeEnvironment.createObject("EnvelopeBuilder");
                envBuilder.setCorners(corner1, corner2);
                generateExtent = GeometryEngine.project(envBuilder.geometry, Factory.SpatialReference.createWebMercator());
            }
        }
    }

    // Create a bar to display editing instructions
    Rectangle {
        id: instructions
        anchors {
            top: parent.top
            left: parent.left
            right: parent.right
        }
        height: 25
        color: "gray"
        opacity: 0.9
        visible: false

        Text {
            anchors.centerIn: parent
            text: instructionText
            font.pixelSize: 16
            color: "white"
        }
    }

    // Create a window to display the generate/sync window
    Rectangle {
        id: syncWindow
        anchors.fill: parent
        color: "transparent"
        clip: true
        visible: false

        RadialGradient {
            anchors.fill: parent
            opacity: 0.7
            gradient: Gradient {
                GradientStop { position: 0.0; color: "lightgrey" }
                GradientStop { position: 0.7; color: "black" }
            }
        }

        MouseArea {
            anchors.fill: parent
            onClicked: mouse.accepted = true
            onWheel: wheel.accepted = true
        }

        Rectangle {
            anchors.centerIn: parent
            width: 125
            height: 100
            color: "lightgrey"
            opacity: 0.8
            radius: 5
            border {
                color: "#4D4D4D"
                width: 1
            }

            Column {
                anchors {
                    fill: parent
                    margins: 10
                }
                spacing: 10

                BusyIndicator {
                    anchors.horizontalCenter: parent.horizontalCenter
                }

                Text {
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: statusText
                    font.pixelSize: 16
                }
            }
        }

        Timer {
            id: hideWindowTimer

            onTriggered: {
                syncWindow.visible = false;
                instructions.visible = true;
                instructionText = "Tap on a feature";
            }
        }

        function hideWindow(time) {
            hideWindowTimer.interval = time;
            hideWindowTimer.restart();
        }
    }

    FileFolder {
        url: dataPath

        // create the data path if it does not yet exist
        Component.onCompleted: {
            if (!exists) {
                makePath(dataPath);
            }
        }
    }

    Component.onDestruction: {
        // unregister the replica
        if (offlineGdb)
            geodatabaseSyncTask.unregisterGeodatabase(offlineGdb)
    }
}

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