Edit and sync features

View inJavaKotlinView on GitHubSample viewer app

Synchronize offline edits with a feature service.

Image of edit and sync features

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 into the desired area, making sure the area you want to take offline is within the current extent of the map view. Tap on the "Generate Geodatabase" button to take the area offline. When complete, the map will update with a red outline around the offline area. To edit features, tap to select a feature, and tap again anywhere else on the map to move the selected feature to the tapped 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. Use createDefaultGenerateGeodatabaseParametersAsync() on the geodatabase sync task to create GenerateGeodatabaseParameters, passing in an Envelope extent as the parameter.
  3. Create a GenerateGeodatabaseJob from the GeodatabaseSyncTask using generateGeodatabaseAsync(...) passing in parameters and a path to the local geodatabase.
  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 .syncGeodatabaseAsync(...) passing in the parameters and geodatabase as arguments.
  8. Start the sync job to synchronize the edits with syncGeodatabase.start().

Relevant API

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

Offline Data

  1. Download the data from ArcGIS Online.
  2. Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
  3. Push the data into the scoped storage of the sample app:

adb push SanFrancisco.tpkx /Android/data/com.esri.arcgisruntime.sample.editandsyncfeatures/files/SanFrancisco.tpkx

Tags

feature service, geodatabase, offline, synchronize

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
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
/* 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.editandsyncfeatures;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
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.Feature;
import com.esri.arcgisruntime.data.FeatureQueryResult;
import com.esri.arcgisruntime.data.Geodatabase;
import com.esri.arcgisruntime.data.GeodatabaseFeatureTable;
import com.esri.arcgisruntime.data.QueryParameters;
import com.esri.arcgisruntime.data.TileCache;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.layers.Layer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
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;
import com.esri.arcgisruntime.tasks.geodatabase.SyncGeodatabaseJob;
import com.esri.arcgisruntime.tasks.geodatabase.SyncGeodatabaseParameters;
import com.esri.arcgisruntime.tasks.geodatabase.SyncLayerOption;

public class MainActivity extends AppCompatActivity {

  private final String TAG = MainActivity.class.getSimpleName();

  private Button mGeodatabaseButton;

  private MapView mMapView;
  private GraphicsOverlay mGraphicsOverlay;
  // objects that implement Loadable must be class fields to prevent being garbage collected before loading
  private GeodatabaseSyncTask mGeodatabaseSyncTask;
  private Geodatabase mGeodatabase;

  private List<Feature> mSelectedFeatures;
  private MainActivity.EditState mCurrentEditState;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // set edit state to not ready until geodatabase job has completed successfully
    mCurrentEditState = MainActivity.EditState.NotReady;

    // create a map view and add a map
    mMapView = findViewById(R.id.mapView);
    // create a graphics overlay and symbol to mark the extent
    mGraphicsOverlay = new GraphicsOverlay();
    mMapView.getGraphicsOverlays().add(mGraphicsOverlay);

    // add listener to handle generate/sync geodatabase button
    mGeodatabaseButton = findViewById(R.id.geodatabaseButton);
    mGeodatabaseButton.setOnClickListener(v -> {
      if (mCurrentEditState == EditState.NotReady) {
        generateGeodatabase();
      } else if (mCurrentEditState == EditState.Ready) {
        syncGeodatabase();
      }
    });
    // add listener to handle motion events, which only responds once a geodatabase is loaded
    mMapView.setOnTouchListener(
        new DefaultMapViewOnTouchListener(MainActivity.this, mMapView) {
          @Override
          public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
            if (mCurrentEditState == MainActivity.EditState.Ready) {
              selectFeaturesAt(mapPointFrom(motionEvent), 10);
            } else if (mCurrentEditState == MainActivity.EditState.Editing) {
              moveSelectedFeatureTo(mapPointFrom(motionEvent));
            }
            return true;
          }
        });

    // use local tile package for the base map
    TileCache sanFranciscoTileCache = new TileCache(getExternalFilesDir(null) + "/SanFrancisco.tpkx");
    ArcGISTiledLayer tiledLayer = new ArcGISTiledLayer(sanFranciscoTileCache);
    final ArcGISMap map = new ArcGISMap(new Basemap(tiledLayer));
    mMapView.setMap(map);
  }

  /**
   * Generates a local geodatabase and sets it to the map.
   */
  private void generateGeodatabase() {
    // define geodatabase sync task
    mGeodatabaseSyncTask = new GeodatabaseSyncTask("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Sync/WildfireSync/FeatureServer");
    mGeodatabaseSyncTask.loadAsync();
    mGeodatabaseSyncTask.addDoneLoadingListener(() -> {
      final SimpleLineSymbol boundarySymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 5);
      // show the extent used as a graphic
      final Envelope extent = mMapView.getVisibleArea().getExtent();
      Graphic boundary = new Graphic(extent, boundarySymbol);
      mGraphicsOverlay.getGraphics().add(boundary);
      // create generate geodatabase parameters for the current extent
      final 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 stored
          final String localGeodatabasePath = getCacheDir() + "/wildfire.geodatabase";
          // create and start the job
          final GenerateGeodatabaseJob generateGeodatabaseJob = mGeodatabaseSyncTask
              .generateGeodatabase(parameters, localGeodatabasePath);
          generateGeodatabaseJob.start();
          createProgressDialog(generateGeodatabaseJob);
          // get geodatabase when done
          generateGeodatabaseJob.addJobDoneListener(() -> {
            if (generateGeodatabaseJob.getStatus() == Job.Status.SUCCEEDED) {
              mGeodatabase = generateGeodatabaseJob.getResult();
              mGeodatabase.loadAsync();
              mGeodatabase.addDoneLoadingListener(() -> {
                if (mGeodatabase.getLoadStatus() == LoadStatus.LOADED) {
                  // get only the first table which, contains points
                  GeodatabaseFeatureTable pointsGeodatabaseFeatureTable = mGeodatabase
                      .getGeodatabaseFeatureTables().get(0);
                  pointsGeodatabaseFeatureTable.loadAsync();
                  FeatureLayer geodatabaseFeatureLayer = new FeatureLayer(pointsGeodatabaseFeatureTable);
                  // add geodatabase layer to the map as a feature layer and make it selectable
                  mMapView.getMap().getOperationalLayers().add(geodatabaseFeatureLayer);
                  mGeodatabaseButton.setVisibility(View.GONE);
                  Log.i(TAG, "Local geodatabase stored at: " + localGeodatabasePath);
                } else {
                  Log.e(TAG, "Error loading geodatabase: " + mGeodatabase.getLoadError().getMessage());
                }
              });
              // set edit state to ready
              mCurrentEditState = EditState.Ready;
            } else if (generateGeodatabaseJob.getError() != null) {
              Log.e(TAG, "Error generating geodatabase: " + generateGeodatabaseJob.getError().getMessage());
              Toast.makeText(this,
                  "Error generating geodatabase: " + generateGeodatabaseJob.getError().getMessage(),
                  Toast.LENGTH_LONG).show();
            } else {
              Log.e(TAG, "Unknown Error generating geodatabase");
              Toast.makeText(this, "Unknown Error generating geodatabase", Toast.LENGTH_LONG).show();
            }
          });
        } catch (InterruptedException | ExecutionException e) {
          Log.e(TAG, "Error generating geodatabase parameters : " + e.getMessage());
          Toast.makeText(this, "Error generating geodatabase parameters: " + e.getMessage(),
              Toast.LENGTH_LONG).show();
        }
      });
    });
  }

  /**
   * Syncs changes made on either the local or web service geodatabase with each other.
   */
  private void syncGeodatabase() {
    // create parameters for the sync task
    SyncGeodatabaseParameters syncGeodatabaseParameters = new SyncGeodatabaseParameters();
    syncGeodatabaseParameters.setSyncDirection(SyncGeodatabaseParameters.SyncDirection.BIDIRECTIONAL);
    syncGeodatabaseParameters.setRollbackOnFailure(false);
    // get the layer ID for each feature table in the geodatabase, then add to the sync job
    for (GeodatabaseFeatureTable geodatabaseFeatureTable : mGeodatabase.getGeodatabaseFeatureTables()) {
      long serviceLayerId = geodatabaseFeatureTable.getServiceLayerId();
      SyncLayerOption syncLayerOption = new SyncLayerOption(serviceLayerId);
      syncGeodatabaseParameters.getLayerOptions().add(syncLayerOption);
    }

    final SyncGeodatabaseJob syncGeodatabaseJob = mGeodatabaseSyncTask
        .syncGeodatabase(syncGeodatabaseParameters, mGeodatabase);

    syncGeodatabaseJob.start();

    createProgressDialog(syncGeodatabaseJob);

    syncGeodatabaseJob.addJobDoneListener(() -> {
      if (syncGeodatabaseJob.getStatus() == Job.Status.SUCCEEDED) {
        Toast.makeText(this, "Sync complete", Toast.LENGTH_SHORT).show();
        mGeodatabaseButton.setVisibility(View.INVISIBLE);
      } else {
        Log.e(TAG, "Database did not sync correctly!");
        Toast.makeText(this, "Database did not sync correctly!", Toast.LENGTH_LONG).show();
      }
    });
  }

  /**
   * Create a progress dialog to show sync state
   */
  private void createProgressDialog(Job job) {

    ProgressDialog syncProgressDialog = new ProgressDialog(this);
    syncProgressDialog.setTitle("Sync geodatabase job");
    syncProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    syncProgressDialog.setCanceledOnTouchOutside(false);
    syncProgressDialog.show();

    job.addProgressChangedListener(() -> syncProgressDialog.setProgress(job.getProgress()));

    job.addJobDoneListener(syncProgressDialog::dismiss);
  }

  /**
   * Queries the features at the tapped point within a certain tolerance.
   *
   * @param point     contains an ArcGIS map point
   * @param tolerance distance from point within which features will be selected
   */
  private void selectFeaturesAt(Point point, int tolerance) {
    // define the tolerance for identifying the feature
    final double mapTolerance = tolerance * mMapView.getUnitsPerDensityIndependentPixel();
    // create objects required to do a selection with a query
    Envelope envelope = new Envelope(point.getX() - mapTolerance, point.getY() - mapTolerance,
        point.getX() + mapTolerance, point.getY() + mapTolerance, mMapView.getSpatialReference());
    QueryParameters query = new QueryParameters();
    query.setGeometry(envelope);
    mSelectedFeatures = new ArrayList<>();
    // select features within the envelope for all features on the map
    for (Layer layer : mMapView.getMap().getOperationalLayers()) {
      final FeatureLayer featureLayer = (FeatureLayer) layer;
      final ListenableFuture<FeatureQueryResult> featureQueryResultFuture = featureLayer
          .selectFeaturesAsync(query, FeatureLayer.SelectionMode.NEW);
      // add done loading listener to fire when the selection returns
      featureQueryResultFuture.addDoneListener(() -> {
        // Get the selected features
        final ListenableFuture<FeatureQueryResult> featureQueryResultFuture1 = featureLayer.getSelectedFeaturesAsync();
        featureQueryResultFuture1.addDoneListener(() -> {
          try {
            FeatureQueryResult layerFeatures = featureQueryResultFuture1.get();
            for (Feature feature : layerFeatures) {
              // Only select points for editing
              if (feature.getGeometry().getGeometryType() == GeometryType.POINT) {
                mSelectedFeatures.add(feature);
              }
            }
          } catch (Exception e) {
            Log.e(TAG, "Select feature failed: " + e.getMessage());
          }
        });
        // set current edit state to editing
        mCurrentEditState = EditState.Editing;
      });
    }
  }

  /**
   * Moves selected features to the given point.
   *
   * @param point contains an ArcGIS map point
   */
  private void moveSelectedFeatureTo(Point point) {
    for (Feature feature : mSelectedFeatures) {
      feature.setGeometry(point);
      feature.getFeatureTable().updateFeatureAsync(feature);
    }
    mSelectedFeatures.clear();
    mCurrentEditState = MainActivity.EditState.Ready;
    mGeodatabaseButton.setText(R.string.sync_geodatabase_button_text);
    mGeodatabaseButton.setVisibility(View.VISIBLE);
  }

  /**
   * Converts motion event to an ArcGIS map point.
   *
   * @param motionEvent containing coordinates of an Android screen point
   * @return a corresponding map point in the place
   */
  private Point mapPointFrom(MotionEvent motionEvent) {
    // get the screen point
    android.graphics.Point screenPoint = new android.graphics.Point(Math.round(motionEvent.getX()),
        Math.round(motionEvent.getY()));
    // return the point that was clicked in map coordinates
    return mMapView.screenToLocation(screenPoint);
  }

  @Override
  protected void onPause() {
    mMapView.pause();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override protected void onDestroy() {
    mMapView.dispose();
    super.onDestroy();
  }

  // enumeration to track editing of points
  enum EditState {
    NotReady, // Geodatabase has not yet been generated
    Editing, // A feature is in the process of being moved
    Ready // The geodatabase is ready for synchronization or further edits
  }
}

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