Generate a local geodatabase replica from an online feature service.
Use case
Generating geodatabase replicas 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 tap the generate button 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
Create a GeodatabaseSyncTask with the URL of the feature service and load it.
Create GenerateGeodatabaseParameters specifying the extent and whether to include attachments.
Create a GenerateGeodatabaseJob with geodatabaseSyncTask.generateGeodatabaseAsync(parameters, downloadPath). Start the job with job.start().
When the job is done, job.getResult() will return the geodatabase. Inside the geodatabase are feature tables which can be used to add feature layers to the map.
Call syncTask.unregisterGeodatabaseAsync(geodatabase) after generation when you're not planning on syncing changes to the service.
Extract the contents of the downloaded zip file to disk.
Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
Push the data into the scoped storage of the sample app: adb push SanFrancisco.tpkx /Android/data/com.esri.arcgisruntime.sample.generategeodatabase/files/SanFrancisco.tpkx
Tags
disconnected, local geodatabase, offline, replica, sync
Sample Code
MainActivity.java
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
/* Copyright 2017 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.
*
*/package com.esri.arcgisruntime.sample.generategeodatabasereplicafromfeatureservice;
import java.io.File;
import java.util.concurrent.ExecutionException;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.concurrent.Job;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Geodatabase;
import com.esri.arcgisruntime.data.GeodatabaseFeatureTable;
import com.esri.arcgisruntime.data.TileCache;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.tasks.geodatabase.GenerateGeodatabaseJob;
import com.esri.arcgisruntime.tasks.geodatabase.GenerateGeodatabaseParameters;
import com.esri.arcgisruntime.tasks.geodatabase.GeodatabaseSyncTask;
publicclassMainActivityextendsAppCompatActivity{
privatefinal String TAG = MainActivity.class.getSimpleName();
private MapView mMapView;
private TextView mProgressTextView;
private RelativeLayout mProgressLayout;
// objects that implement Loadable must be class fields to prevent being garbage collected before loadingprivate GeodatabaseSyncTask mGeodatabaseSyncTask;
private Geodatabase mGeodatabase;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// use local tile package for the base map TileCache sanFrancisco = new TileCache(getExternalFilesDir(null) + getString(R.string.san_francisco_tpkx));
ArcGISTiledLayer tiledLayer = new ArcGISTiledLayer(sanFrancisco);
// create a map view and add a map mMapView = findViewById(R.id.mapView);
final ArcGISMap map = new ArcGISMap(new Basemap(tiledLayer));
mMapView.setMap(map);
// create a graphics overlay and symbol to mark the extentfinal GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
mMapView.getGraphicsOverlays().add(graphicsOverlay);
final SimpleLineSymbol boundarySymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 5);
// inflate button and progress layoutfinal Button genGeodatabaseButton = findViewById(R.id.genGeodatabaseButton);
mProgressLayout = findViewById(R.id.progressLayout);
final ProgressBar progressBar = findViewById(R.id.taskProgressBar);
mProgressTextView = findViewById(R.id.progressTextView);
// create a geodatabase sync task mGeodatabaseSyncTask = new GeodatabaseSyncTask(getString(R.string.wildfire_sync));
mGeodatabaseSyncTask.loadAsync();
mGeodatabaseSyncTask.addDoneLoadingListener(() -> {
// generate the geodatabase sync task genGeodatabaseButton.setOnClickListener(v -> {
// show the progress layout progressBar.setProgress(0);
mProgressLayout.setVisibility(View.VISIBLE);
// clear any previous operational layers and graphics if button clicked more than once map.getOperationalLayers().clear();
graphicsOverlay.getGraphics().clear();
// show the extent used as a graphic Envelope extent = mMapView.getVisibleArea().getExtent();
Graphic boundary = new Graphic(extent, boundarySymbol);
graphicsOverlay.getGraphics().add(boundary);
// create generate geodatabase parameters for the current extentfinal ListenableFuture<GenerateGeodatabaseParameters> defaultParameters = mGeodatabaseSyncTask
.createDefaultGenerateGeodatabaseParametersAsync(extent);
defaultParameters.addDoneListener(() -> {
try {
// set parameters and don't include attachments GenerateGeodatabaseParameters parameters = defaultParameters.get();
parameters.setReturnAttachments(false);
// define the local path where the geodatabase will be storedfinal String localGeodatabasePath =
getCacheDir() + File.separator + getString(R.string.wildfire_geodatabase);
// create and start the jobfinal GenerateGeodatabaseJob generateGeodatabaseJob = mGeodatabaseSyncTask
.generateGeodatabase(parameters, localGeodatabasePath);
generateGeodatabaseJob.start();
mProgressTextView.setText(getString(R.string.progress_started));
// update progress generateGeodatabaseJob.addProgressChangedListener(() -> {
progressBar.setProgress(generateGeodatabaseJob.getProgress());
mProgressTextView.setText(getString(R.string.progress_fetching));
});
// get geodatabase when done generateGeodatabaseJob.addJobDoneListener(() -> {
mProgressLayout.setVisibility(View.INVISIBLE);
if (generateGeodatabaseJob.getStatus() == Job.Status.SUCCEEDED) {
mGeodatabase = generateGeodatabaseJob.getResult();
mGeodatabase.loadAsync();
mGeodatabase.addDoneLoadingListener(() -> {
if (mGeodatabase.getLoadStatus() == LoadStatus.LOADED) {
mProgressTextView.setText(getString(R.string.progress_done));
for (GeodatabaseFeatureTable geodatabaseFeatureTable : mGeodatabase
.getGeodatabaseFeatureTables()) {
geodatabaseFeatureTable.loadAsync();
map.getOperationalLayers().add(new FeatureLayer(geodatabaseFeatureTable));
}
genGeodatabaseButton.setVisibility(View.GONE);
Log.i(TAG, "Local geodatabase stored at: " + localGeodatabasePath);
} else {
Log.e(TAG, "Error loading geodatabase: " + mGeodatabase.getLoadError().getMessage());
}
});
// unregister since we're not syncing ListenableFuture unregisterGeodatabase = mGeodatabaseSyncTask.unregisterGeodatabaseAsync(mGeodatabase);
unregisterGeodatabase.addDoneListener(() -> {
Log.i(TAG, "Geodatabase unregistered since we wont be editing it in this sample.");
Toast.makeText(MainActivity.this,
"Geodatabase unregistered since we wont be editing it in this sample.",
Toast.LENGTH_LONG).show();
});
} elseif (generateGeodatabaseJob.getError() != null) {
Log.e(TAG, "Error generating geodatabase: " + generateGeodatabaseJob.getError().getMessage());
} else {
Log.e(TAG, "Unknown Error generating geodatabase");
}
});
} catch (InterruptedException | ExecutionException e) {
Log.e(TAG, "Error generating geodatabase parameters : " + e.getMessage());
}
});
});
});
}
@OverrideprotectedvoidonPause(){
super.onPause();
mMapView.pause();
}
@OverrideprotectedvoidonResume(){
super.onResume();
mMapView.resume();
}
@OverrideprotectedvoidonDestroy(){
super.onDestroy();
mMapView.dispose();
}
}