Create mobile geodatabase

View inQMLC++
View on GitHub
Sample viewer app

Create and share a mobile geodatabase.

screenshot

Use case

A mobile geodatabase is a collection of various types of GIS datasets contained in a single file (.geodatabase) on disk that can store, query, and manage spatial and nonspatial data. Mobile geodatabases are stored in a SQLite database and can contain up to 2 TB of portable data. Users can create, edit and share mobile geodatabases across ArcGIS Pro, ArcGIS Runtime, or any SQL software. These mobile geodatabases support both viewing and editing and enable new offline editing workflows that don’t require a feature service.

For example, a user would like to track the location of their device at various intervals to generate a heat map of the most visited locations. The user can add each location as a feature to a table and generate a mobile geodatabase. The user can then instantly share the mobile geodatabase to ArcGIS Pro to generate a heat map using the recorded locations stored as a geodatabase feature table.

How to use the sample

Select "Create new .geodatabase" to create a new mobile geodatabase in a temporary directory. Click or tap on the map to add features to the mobile geodatabase. Select "View feature table" to view the contents of the geodatabase feature table. Select "Clear features" to remove all features from the geodatabase feature table. Select "Close .geodatabase" to save and close the geodatabase so it is ready to be shared or uploaded to ArcGIS Online.

How it works

  1. Create a new Geodatabase at a given path that does not already exist with the GeodatabaseUtility singleton.
  2. Create a TableDescription component and add a list of FieldDescriptions to the table description upon component completion.
  3. Create a GeodatabaseFeatureTable from the geodatabase with the TableDescription using Geodatabase.createTable(tableDescription).
  4. Create an ArcGISFeature on a selected map point using FeatureTable.createFeatureWithAttributes(attributes, point)
  5. Add the feature to the table using FeatureTable.addFeature(feature).
  6. Each feature added to the feature table is committed to the mobile geodatabase file.
  7. Close the mobile geodatabase to safely share the ".geodatabase" file using Geodatabase.close()

Relevant API

  • ArcGISFeature
  • FeatureLayer
  • FeatureTable
  • FieldDescription
  • Geodatabase
  • GeodatabaseFeatureTable
  • TableDescription

Additional information

Learn more about mobile geodatabases and how to utilize them on the Mobile geodatabases page. The following mobile geodatabase behaviors are supported in ArcGIS Runtime: annotation, attachments, attribute rules, contingent values, dimensions, domains, feature-linked annotation, subtypes, utility network and relationship classes.

Visit ArcGIS field data types to learn more about the types of fields supported.

Tags

arcgis pro, database, feature, feature table, geodatabase, mobile geodatabase, sqlite

Sample Code

CreateMobileGeodatabase.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
// [WriteFile Name=CreateMobileGeodatabase, Category=Features]
// [Legal]
// Copyright 2022 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.12
import Esri.ArcGISRuntime 100.15
import Esri.ArcGISExtras 1.1
import QtQuick.Controls 2.12

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    property Geodatabase mobileGeodatabase: null
    property FeatureTable featureTable: null

    property bool gdbOpen: false
    property int numberOfFeatures: 0
    property var featureListModel: []

    MapView {
        id: mapView
        anchors.fill: parent

        Map {
            id: map
            initBasemapStyle: Enums.BasemapStyleArcGISTopographic
            initialViewpoint: ViewpointCenter {
                center: Point {
                    x: -77.7332
                    y: 39.3238
                    spatialReference: SpatialReference { wkid: 4326 }
                }
                targetScale: 10000
            }
        }

        onMouseClicked: {
            // Add a feature to the current feature table at the given mouse click or screen tap
            if (!featureTable)
                return;

            const addFeatureToFeatureTable = () => {
                if (featureTable.addFeatureStatus !== Enums.TaskStatusCompleted)
                    return;

                featureListModel.push({"oid": feature.attributes.attributeValue("oid"), "collection_timestamp": feature.attributes.attributeValue("collection_timestamp")})
                tableView.model = featureListModel;
                featureTable.addFeatureStatusChanged.disconnect(addFeatureToFeatureTable);
                numberOfFeatures = featureTable.numberOfFeatures
            }

            const feature = featureTable.createFeatureWithAttributes({"collection_timestamp": new Date()}, mouse.mapPoint);
            featureTable.addFeatureStatusChanged.connect(addFeatureToFeatureTable);
            featureTable.addFeature(feature);
        }
    }

    Rectangle {
        id: buttonListRectangle
        width: 250
        height: buttonColumn.height + 20
        color: "#ffffff"
        anchors.right: parent.right
        anchors.top: parent.top
        anchors.topMargin: 10
        anchors.rightMargin: 10

        // Prevent mouse interaction from propagating to the MapView
        MouseArea {
            anchors.fill: parent
            onPressed: mouse.accepted = true;
            onWheel: wheel.accepted = true;
        }

        Column {
            id: buttonColumn
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.rightMargin: 10
            anchors.leftMargin: 10
            anchors.topMargin: 10
            height: children.height
            spacing: 5

            onHeightChanged: {
                buttonListRectangle.height = height + 20
            }

            Button {
                id: createGdbButton
                text: qsTr("Create new .geodatabase")
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.rightMargin: 0
                anchors.leftMargin: 0
                enabled: !gdbOpen
                onClicked: {
                    createGeodatabase();
                }
            }

            Button {
                id: viewGdbTableButton
                text: qsTr("View feature table")
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.rightMargin: 0
                anchors.leftMargin: 0
                enabled: gdbOpen
                onClicked: {
                    featureTableDisplay.visible = true;
                    buttonListRectangle.visible = false;
                }
            }
            Button {
                id: clearFeaturesButton
                text: qsTr("Clear features")
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.rightMargin: 0
                anchors.leftMargin: 0
                enabled: gdbOpen
                onClicked: {
                    clearTable();
                }
            }

            Button {
                id: closeGdbButton
                text: qsTr("Close .geodatabase")
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.rightMargin: 0
                anchors.leftMargin: 0
                enabled: gdbOpen
                onClicked: {
                    closeGeodatabase();
                    gdbClosedNoticeRectangle.visible = true;
                    buttonListRectangle.visible = false;
                }
            }

            Text {
                text: mobileGeodatabase ? "Created new geodatabase:\n" + mobileGeodatabase.path.toString().split("/").pop() : "Geodatabase path not found"
                anchors.left: parent.left
                anchors.right: parent.right
                font.pixelSize: 12
                wrapMode: Text.WrapAnywhere
                anchors.rightMargin: 0
                anchors.leftMargin: 0
                visible: gdbOpen
            }

            Text {
                text: "Number of features: " + numberOfFeatures + (numberOfFeatures === 0 ? "\n(Click or tap the map to add new features)" : "")
                anchors.left: parent.left
                anchors.right: parent.right
                font.pixelSize: 12
                wrapMode: Text.WordWrap
                anchors.rightMargin: 0
                anchors.leftMargin: 0
                visible: gdbOpen
            }
        }
    }

    Rectangle {
        id: gdbClosedNoticeRectangle
        anchors.centerIn: parent
        width: parent.width * 0.75
        height: gdbInfoColumn.height + 20
        color: "white"
        border.color: "black"
        clip: true
        visible: false

        // Prevent mouse interaction from propagating to the MapView
        MouseArea {
            anchors.fill: parent
            onPressed: mouse.accepted = true;
            onWheel: wheel.accepted = true;
        }

        Column {
            id: gdbInfoColumn
            anchors.centerIn: parent
            anchors.margins: 10
            spacing: 10
            width: parent.width - 20
            height: children.height

            Text {
                id: gdbNameText
                width: parent.width
                text: "Closed and saved geodatabase to the temporary path:"
                wrapMode: Text.WordWrap
            }
            TextEdit {
                id: gdbPathText
                width: parent.width
                readOnly: true
                selectByMouse: true
                text: mobileGeodatabase ? mobileGeodatabase.path : "The geodatabase returned no path property"
                wrapMode: Text.WrapAnywhere
            }
            Button {
                id: gdbInfoClose
                anchors.horizontalCenter: parent.horizontalCenter
                text: "Ok"
                onClicked: {
                    gdbClosedNoticeRectangle.visible = false;
                    buttonListRectangle.visible = true;
                }
            }
        }
    }

    // Declare the C++ instance which creates the scene etc. and supply the view
    Rectangle {
        id: featureTableDisplay
        anchors {
            verticalCenter: parent.verticalCenter
            horizontalCenter: parent.horizontalCenter
        }
        width: parent.width - parent.width * 0.2
        height: parent.height - parent.height * 0.3
        color: "#80808080"
        visible: false

        // Prevent mouse interaction from propagating to the MapView
        MouseArea {
            anchors.fill: parent
            onPressed: mouse.accepted = true;
            onWheel: wheel.accepted = true;
        }

        ListView {
            id: tableView
            anchors.fill: parent
            anchors.margins: 10
            ScrollBar.vertical: ScrollBar {
                active: true
            }
            clip: true

            header: Row {
                height: 40
                width: tableView.width
                Rectangle {
                    color: "grey"
                    border.color: "black"
                    width: parent.width * 0.2
                    height: parent.height
                    Text {
                        anchors.centerIn: parent
                        text: "Object ID"
                        color: "white"
                        font.bold: true
                        wrapMode: Text.WordWrap

                    }
                }
                Rectangle {
                    color: "grey"
                    border.color: "black"
                    width: parent.width * 0.8
                    height: parent.height
                    Text {
                        anchors.centerIn: parent
                        text: "Timestamp"
                        color: "white"
                        font.bold: true
                        wrapMode: Text.WordWrap
                    }
                }
            }

            model: featureListModel
            delegate: Row {
                height: 40
                width: tableView.width
                Rectangle {
                    color: "white"
                    border.color: "black"
                    width: parent.width * 0.2
                    height: parent.height
                    Text {
                        anchors.centerIn: parent
                        text: modelData.oid
                        wrapMode: Text.WordWrap
                    }
                }
                Rectangle {
                    color: "white"
                    border.color: "black"
                    width: parent.width * 0.8
                    height: parent.height
                    Text {
                        anchors.centerIn: parent
                        text: modelData.collection_timestamp
                        wrapMode: Text.WordWrap
                    }
                }
            }
        }
    }

    Rectangle {
        id: buttonRectangle
        width: closeTableButton.width + 10
        height: closeTableButton.height + 10
        color: "#ffffff"
        anchors.right: parent.right
        anchors.top: parent.top
        anchors.topMargin: 10
        anchors.rightMargin: 10
        visible: featureTableDisplay.visible

        // Prevent mouse interaction from propagating to the MapView
        MouseArea {
            anchors.fill: parent
            onPressed: mouse.accepted = true;
            onWheel: wheel.accepted = true;
        }

        Button {
            id: closeTableButton
            anchors.centerIn: parent

            text: "Close table view"

            onClicked: {
                featureTableDisplay.visible = false;
                buttonListRectangle.visible = true;
            }
        }
    }

    // Create a table description with which to create a feature table from the mobile geodatabase
    TableDescription {
        id: tableDescription
        tableName: "LocationHistory"
        spatialReference: SpatialReference { wkid: 4326 }
        geometryType: Enums.GeometryTypePoint
        hasAttachments: false
        hasM: false
        hasZ: false

        Component.onCompleted: {
            fieldDescriptions.append(ArcGISRuntimeEnvironment.createObject("FieldDescription", {name: "oid", fieldType: Enums.FieldTypeOID}));
            fieldDescriptions.append(ArcGISRuntimeEnvironment.createObject("FieldDescription", {name: "collection_timestamp", fieldType: Enums.FieldTypeDate}));
        }
    }

    // This ArcGISExtras component is used to ensure the given file path does not already exist
    FileFolder {
        id: fileFolder
    }

    function createGeodatabase() {
        const path = System.temporaryFolder.url + "/LocationHistory_%1.geodatabase".arg((new Date().getTime() % 1000).toString());

        // We cannot call create() with a path that already exists
        if (fileFolder.fileExists(path))
            return;

        const createGdbStatusChanged = () => {
            if (GeodatabaseUtility.createStatus === Enums.TaskStatusCompleted) {
                mobileGeodatabase = GeodatabaseUtility.createResult;

                // Create a feature table from the geodatabase once it has been created
                createTable();
                GeodatabaseUtility.createStatusChanged.disconnect(createGdbStatusChanged);
            }
        }

        GeodatabaseUtility.createStatusChanged.connect(createGdbStatusChanged);
        GeodatabaseUtility.create(path);
    }

    function createTable() {
        gdbOpen = true;
        const tableCreationStatusChanged = () => {
            if (mobileGeodatabase.createTableStatus !== Enums.TaskStatusCompleted)
                return;

            featureTable = mobileGeodatabase.createTableResult;
            const layer = ArcGISRuntimeEnvironment.createObject("FeatureLayer", {featureTable: featureTable});
            map.operationalLayers.append(layer);

        }
        mobileGeodatabase.onCreateTableStatusChanged.connect(tableCreationStatusChanged);
        mobileGeodatabase.createTable(tableDescription);
    }

    function closeGeodatabase() {
        // Call Geodatabase.close() to safely share the ".geodatabase" file
        mobileGeodatabase.close();

        // Clear any information in the UI about the previous geodatabase
        gdbOpen = false;
        map.operationalLayers.clear();
        featureListModel = [];
        tableView.model = [];
        numberOfFeatures = 0;
    }

    function clearTable() {
        if (featureTable.numberOfFeatures > 0)
            deleteAllFeatures();

        featureListModel = [];
        tableView.model = [];
    }

    QueryParameters {
        id: params
        whereClause: "1=1"
    }

    function deleteAllFeatures() {
        const queryFeaturesCompleted = () => {
            if (featureTable.queryFeaturesStatus !== Enums.TaskStatusCompleted)
                return;

            const results = featureTable.queryFeaturesResult;
            const features = results.iterator.features;
            featureTable.deleteFeatures(features);
            numberOfFeatures = 0;
            featureTable.queryFeaturesStatusChanged.disconnect(queryFeaturesCompleted);
        }

        featureTable.queryFeaturesStatusChanged.connect(queryFeaturesCompleted);
        featureTable.queryFeatures(params);
    }
}

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