Generate geodatabase replica from feature service

View inC++QMLView on GitHubSample viewer app

Generate a local geodatabase from an online feature service.

screenshot

Use case

Generating geodatabases is the first step toward taking a feature service offline. It allows you to save features locally for offline display.

How to use the sample

Zoom to any extent. Then click 'Generate Geodatabse' to generate a geodatabase of features from a feature service filtered to the current extent. A red outline will show the extent used. The job's progress is shown while the geodatabase is generated. When complete, the map will reload with only the layers in the geodatabase, clipped to the extent.

How it works

  1. Create a GeodatabaseSyncTask with the URL of the feature service and load it.
  2. Create GenerateGeodatabaseParameters specifying the extent and whether to include attachments.
  3. Create a GenerateGeodatabaseJob with geodatabaseSyncTask::generateGeodatabase(parameters, downloadPath). Start the job with job::start().
  4. When the job is done, job::result() will return the geodatabase. Inside the geodatabase are feature tables which can be used to add feature layers to the map.
  5. Call syncTask::unregisterGeodatabase(geodatabase) after generation when you're not planning on syncing changes to the service.

Relevant API

  • GenerateGeodatabaseJob
  • GenerateGeodatabaseParameters
  • Geodatabase
  • GeodatabaseSyncTask

Offline Data

Read more about how to set up the sample's offline data here.

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

Tags

disconnected, local geodatabase, offline, replica, sync

Sample Code

GenerateGeodatabaseReplicaFromFeatureService.cppGenerateGeodatabaseReplicaFromFeatureService.cppGenerateGeodatabaseReplicaFromFeatureService.hGenerateGeodatabaseReplicaFromFeatureService.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
// [WriteFile Name=GenerateGeodatabaseReplicaFromFeatureService, Category=Features]
// [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]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "GenerateGeodatabaseReplicaFromFeatureService.h"

#include "Map.h"
#include "MapQuickView.h"
#include "FeatureLayer.h"
#include "Basemap.h"
#include "SpatialReference.h"
#include "ServiceFeatureTable.h"
#include "ArcGISTiledLayer.h"
#include "ArcGISFeatureServiceInfo.h"
#include "Envelope.h"
#include "GenerateGeodatabaseParameters.h"
#include "GeodatabaseSyncTask.h"
#include "GeometryEngine.h"
#include "GenerateLayerOption.h"
#include "GeodatabaseFeatureTable.h"

#include <QDir>
#include <QtCore/qglobal.h>
#include <QUrl>

#ifdef Q_OS_IOS
#include <QStandardPaths>
#endif // Q_OS_IOS

using namespace Esri::ArcGISRuntime;

// helper method to get cross platform data path
namespace
{
  QString defaultDataPath()
  {
    QString dataPath;

  #ifdef Q_OS_ANDROID
    dataPath = "/sdcard";
  #elif defined Q_OS_IOS
    dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
  #else
    dataPath = QDir::homePath();
  #endif

    return dataPath;
  }
} // namespace

GenerateGeodatabaseReplicaFromFeatureService::GenerateGeodatabaseReplicaFromFeatureService(QQuickItem* parent) :
  QQuickItem(parent),
  m_dataPath(defaultDataPath() + "/ArcGIS/Runtime/Data/")
{
}

GenerateGeodatabaseReplicaFromFeatureService::~GenerateGeodatabaseReplicaFromFeatureService() = default;

void GenerateGeodatabaseReplicaFromFeatureService::init()
{
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<GenerateGeodatabaseReplicaFromFeatureService>("Esri.Samples", 1, 0, "GenerateGeodatabaseReplicaFromFeatureServiceSample");
}

void GenerateGeodatabaseReplicaFromFeatureService::componentComplete()
{
  QQuickItem::componentComplete();

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");
  m_mapView->setWrapAroundMode(WrapAroundMode::Disabled);

  //! [Create a map using a local tile package]
  TileCache* tileCache = new TileCache(m_dataPath + "tpkx/SanFrancisco.tpkx", this);
  ArcGISTiledLayer* tiledLayer = new ArcGISTiledLayer(tileCache, this);
  Basemap* basemap = new Basemap(tiledLayer, this);
  m_map = new Map(basemap, this);
  //! [Create a map using a local tile package]

  // set an initial viewpoint
  Envelope env(-122.50017, 37.74500, -122.43843, 37.81638, SpatialReference(4326));
  Viewpoint viewpoint(env);
  m_map->setInitialViewpoint(viewpoint);

  // Set map to map view
  m_mapView->setMap(m_map);

  //! [Features GenerateGeodatabase Part 1]
  // create the GeodatabaseSyncTask
  m_syncTask = new GeodatabaseSyncTask(QUrl(m_featureServiceUrl), this);
  //! [Features GenerateGeodatabase Part 1]

  // connect to sync task doneLoading signal
  connect(m_syncTask, &GeodatabaseSyncTask::doneLoading, this, [this](Error error)
  {
    if (!error.isEmpty())
    {
      emit updateStatus("Generate failed");
      emit hideWindow(5000, false);
      return;
    }

    // add online feature layers to the map, and obtain service IDs
    m_featureServiceInfo = m_syncTask->featureServiceInfo();
    const auto infos = m_featureServiceInfo.layerInfos();
    for (const IdInfo& idInfo : infos)
    {
      // get the layer ID from the idInfo
      QString id = QString::number(idInfo.infoId());

      // add the layer to the map
      QUrl featureLayerUrl(m_featureServiceInfo.url().toString() + "/" + id);
      ServiceFeatureTable* serviceFeatureTable = new ServiceFeatureTable(featureLayerUrl, this);
      FeatureLayer* featureLayer = new FeatureLayer(serviceFeatureTable, this);
      m_map->operationalLayers()->append(featureLayer);

      // add the layer id to the string list
      m_serviceIds << id;
    }

  });

  // connect to map doneLoading signal
  connect(m_map, &Map::doneLoading, this, [this](Error error)
  {
    if (error.isEmpty())
    {
      // load the sync task once the map loads
      m_syncTask->load();
    }
  });
}

void GenerateGeodatabaseReplicaFromFeatureService::addFeatureLayers(const QString& serviceUrl, const QStringList& serviceIds)
{
  for (const QString& id : serviceIds)
  {
    ServiceFeatureTable* serviceFeatureTable = new ServiceFeatureTable(QUrl(serviceUrl + id), this);
    FeatureLayer* featureLayer = new FeatureLayer(serviceFeatureTable, this);
    m_map->operationalLayers()->append(featureLayer);
  }
}

//! [Features GenerateGeodatabase Part 2]
GenerateGeodatabaseParameters GenerateGeodatabaseReplicaFromFeatureService::getUpdatedParameters(Envelope gdbExtent)
{
  // create the parameters
  GenerateGeodatabaseParameters params;
  params.setReturnAttachments(false);
  params.setOutSpatialReference(SpatialReference::webMercator());
  params.setExtent(gdbExtent);

  // set the layer options for all of the service IDs
  QList<GenerateLayerOption> layerOptions;
  for (const QString& id : qAsConst(m_serviceIds))
  {
    GenerateLayerOption generateLayerOption(id.toInt());
    layerOptions << generateLayerOption;
  }
  params.setLayerOptions(layerOptions);

  return params;
}

void GenerateGeodatabaseReplicaFromFeatureService::generateGeodatabaseFromCorners(double xCorner1, double yCorner1, double xCorner2, double yCorner2)
{
  // create an envelope from the QML rectangle corners
  const Point corner1 = m_mapView->screenToLocation(xCorner1, yCorner1);
  const Point corner2 = m_mapView->screenToLocation(xCorner2, yCorner2);
  const Envelope extent(corner1, corner2);
  const Geometry geodatabaseExtent = GeometryEngine::project(extent, SpatialReference::webMercator());

  // get the updated parameters
  GenerateGeodatabaseParameters params = getUpdatedParameters(geodatabaseExtent);

  // execute the task and obtain the job
  const QString outputGdb = m_tempPath.path() + "/wildfire.geodatabase";
  GenerateGeodatabaseJob* generateJob = m_syncTask->generateGeodatabase(params, outputGdb);

  // connect to the job's status changed signal
  if (generateJob)
  {
    connect(generateJob, &GenerateGeodatabaseJob::statusChanged, this, [this, generateJob](JobStatus jobStatus)
    {
      // connect to the job's status changed signal to know once it is done
      switch (jobStatus) {
      case JobStatus::Failed:
        emit updateStatus("Generate failed");
        emit hideWindow(5000, false);
        break;
      case JobStatus::NotStarted:
        emit updateStatus("Job not started");
        break;
      case JobStatus::Paused:
        emit updateStatus("Job paused");
        break;
      case JobStatus::Started:
        emit updateStatus("In progress...");
        break;
      case JobStatus::Succeeded:
        emit updateStatus("Complete");
        emit hideWindow(1500, true);
        addOfflineData(generateJob->result());
        break;
      default:
        break;
      }
    });

    // start the generate job
    generateJob->start();
  }
  else
  {
    emit updateStatus("Generate failed");
    emit hideWindow(5000, false);
  }
}
//! [Features GenerateGeodatabase Part 2]

void GenerateGeodatabaseReplicaFromFeatureService::addOfflineData(Geodatabase* gdb)
{
  // remove the original online feature layers
  m_map->operationalLayers()->clear();

  // load the geodatabase
  connect(gdb, &Geodatabase::doneLoading, this, [this, gdb](Error)
  {
    // create a feature layer from each feature table, and add to the map
    const auto tables = gdb->geodatabaseFeatureTables();
    for (GeodatabaseFeatureTable* featureTable : tables)
    {
      FeatureLayer* featureLayer = new FeatureLayer(featureTable, this);
      m_map->operationalLayers()->append(featureLayer);
    }

    // unregister geodatabase since there will be no edits uploaded
    m_syncTask->unregisterGeodatabase(gdb);
  });
  gdb->load();
}

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