Synchronize offline edits with a feature service.
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. Tap "Generate geodatabase" to take the area offline. When complete, the map will update to only show 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, tap the "Sync geodatabase" button.
How it works
- Create a
GeodatabaseSyncTask
from a URL to a feature service. - Use
createDefaultGenerateGeodatabaseParametersAsync()
on the geodatabase sync task to createGenerateGeodatabaseParameters
, passing in anEnvelope
extent as the parameter. - Create a
GenerateGeodatabaseJob
from theGeodatabaseSyncTask
usinggenerateGeodatabaseAsync(...)
, passing in the parameters and a path to where the geodatabase should be downloaded locally. - Start the job and get the result
Geodatabase
. - 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.
- Create
SyncGeodatabaseParameters
and set the sync direction. - Create a
SyncGeodatabaseJob
fromGeodatabaseSyncTask
using.syncGeodatabaseAsync(...)
passing in the parameters and geodatabase as arguments. - Start the sync job to synchronize the edits.
Relevant API
- FeatureLayer
- FeatureTable
- GenerateGeodatabaseJob
- GenerateGeodatabaseParameters
- GeodatabaseSyncTask
- SyncGeodatabaseJob
- SyncGeodatabaseParameters
- SyncLayerOption
Offline data
This sample uses a San Francisco offline basemap tile package.
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
// 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.
using ArcGIS.Samples.Managers;
using Esri.ArcGISRuntime.ArcGISServices;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Maui;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks;
using Esri.ArcGISRuntime.Tasks.Offline;
using Esri.ArcGISRuntime.UI;
using Colors = System.Drawing.Color;
namespace ArcGIS.Samples.EditAndSyncFeatures
{
[ArcGIS.Samples.Shared.Attributes.Sample(
name: "Edit and sync features",
category: "Data",
description: "Synchronize offline edits with a feature service.",
instructions: "Pan and zoom to position the red rectangle around the area you want to take offline. Tap \"Generate geodatabase\" to take the area offline. When complete, the map will update to only show 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, tap the \"Sync geodatabase\" button.",
tags: new[] { "feature service", "geodatabase", "offline", "synchronize" })]
[ArcGIS.Samples.Shared.Attributes.OfflineData("e4a398afe9a945f3b0f4dca1e4faccb5")]
public partial class EditAndSyncFeatures : ContentPage
{
// Enumeration to track which phase of the workflow the sample is in.
private 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.
}
// URL for a feature service that supports geodatabase generation.
private Uri _featureServiceUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Sync/WildfireSync/FeatureServer");
// Path to the geodatabase file on disk.
private string _gdbPath;
// Task to be used for generating the geodatabase.
private GeodatabaseSyncTask _gdbSyncTask;
// Flag to indicate which stage of the edit process the sample is in.
private EditState _readyForEdits = EditState.NotReady;
// Hold a reference to the generated geodatabase.
private Geodatabase _resultGdb;
public EditAndSyncFeatures()
{
InitializeComponent();
// Create the UI, setup the control references and execute initialization.
_ = Initialize();
}
private async Task Initialize()
{
// Create a tile cache and load it with the SanFrancisco streets tpk.
try
{
TileCache tileCache = new TileCache(DataManager.GetDataFolder("e4a398afe9a945f3b0f4dca1e4faccb5", "SanFrancisco.tpkx"));
// Create the corresponding layer based on the tile cache.
ArcGISTiledLayer tileLayer = new ArcGISTiledLayer(tileCache);
// Create the basemap based on the tile cache.
Basemap sfBasemap = new Basemap(tileLayer);
// Create the map with the tile-based basemap.
Map myMap = new Map(sfBasemap);
// Assign the map to the MapView.
MyMapView.Map = myMap;
// Create a new symbol for the extent graphic.
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Colors.Red, 2);
// Create a graphics overlay for the extent graphic and apply a renderer.
GraphicsOverlay extentOverlay = new GraphicsOverlay
{
Renderer = new SimpleRenderer(lineSymbol)
};
// Add the graphics overlay to the map view.
MyMapView.GraphicsOverlays.Add(extentOverlay);
// Set up an event handler for when the viewpoint (extent) changes.
MyMapView.ViewpointChanged += MapViewExtentChanged;
// Create a task for generating a geodatabase (GeodatabaseSyncTask).
_gdbSyncTask = await GeodatabaseSyncTask.CreateAsync(_featureServiceUri);
// Add all layers from the service to the map.
foreach (IdInfo layer in _gdbSyncTask.ServiceInfo.LayerInfos)
{
// Get the URL for this layer.
Uri onlineTableUri = new Uri(_featureServiceUri + "/" + layer.Id);
// Create the ServiceFeatureTable.
ServiceFeatureTable onlineTable = new ServiceFeatureTable(onlineTableUri);
// Wait for the table to load.
await onlineTable.LoadAsync();
// Skip tables that aren't for point features.{
if (onlineTable.GeometryType != GeometryType.Point)
{
continue;
}
// Add the layer to the map's operational layers if load succeeds.
if (onlineTable.LoadStatus == Esri.ArcGISRuntime.LoadStatus.Loaded)
{
myMap.OperationalLayers.Add(new FeatureLayer(onlineTable));
}
}
// Update the graphic - needed in case the user decides not to interact before pressing the button.
UpdateMapExtent();
// Enable the generate button now that sample is ready.
MyGenerateButton.IsEnabled = true;
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK");
}
}
private async void GeoViewTapped(object sender, GeoViewInputEventArgs e)
{
try
{
// Disregard if not ready for edits.
if (_readyForEdits == EditState.NotReady) { return; }
// If an edit is in process, finish it.
if (_readyForEdits == EditState.Editing)
{
// Hold a list of any selected features.
List<Feature> selectedFeatures = new List<Feature>();
// Get all selected features then clear selection.
foreach (FeatureLayer layer in MyMapView.Map.OperationalLayers)
{
// Get the selected features.
FeatureQueryResult layerFeatures = await layer.GetSelectedFeaturesAsync();
// FeatureQueryResult implements IEnumerable, so it can be treated as a collection of features.
selectedFeatures.AddRange(layerFeatures);
// Clear the selection.
layer.ClearSelection();
}
// Update all selected features' geometry.
foreach (Feature feature in selectedFeatures)
{
// Get a reference to the correct feature table for the feature.
GeodatabaseFeatureTable table = (GeodatabaseFeatureTable)feature.FeatureTable;
// Ensure the geometry type of the table is point.
if (table.GeometryType != GeometryType.Point)
{
continue;
}
// Set the new geometry.
feature.Geometry = e.Location;
// Update the feature in the table.
await table.UpdateFeatureAsync(feature);
}
// Update the edit state.
_readyForEdits = EditState.Ready;
// Enable the sync button.
MySyncButton.IsEnabled = true;
// Update the help label.
MyHelpLabel.Text = "4. Click 'Synchronize' or keep editing";
}
// Otherwise, start an edit.
else
{
// Define a tolerance for use with identifying the feature.
double tolerance = 15 * MyMapView.UnitsPerPixel;
// Define the selection envelope.
Envelope selectionEnvelope = new Envelope(e.Location.X - tolerance, e.Location.Y - tolerance, e.Location.X + tolerance, e.Location.Y + tolerance);
// Define query parameters for feature selection.
QueryParameters query = new QueryParameters()
{
Geometry = selectionEnvelope
};
// Track whether any selections were made.
bool selectedFeature = false;
// Select the feature in all applicable tables.
foreach (FeatureLayer layer in MyMapView.Map.OperationalLayers)
{
FeatureQueryResult res = await layer.SelectFeaturesAsync(query, Esri.ArcGISRuntime.Mapping.SelectionMode.New);
selectedFeature = selectedFeature || res.Any();
}
// Only update state if a feature was selected.
if (selectedFeature)
{
// Set the edit state.
_readyForEdits = EditState.Editing;
// Update the help label.
MyHelpLabel.Text = "3. Tap on the map to move the point";
}
}
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK");
}
}
private void UpdateMapExtent()
{
// Return if mapview is null.
if (MyMapView == null) { return; }
// Get the new viewpoint.
Viewpoint myViewPoint = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry);
// Return if viewpoint is null.
if (myViewPoint == null) { return; }
// Get the updated extent for the new viewpoint.
Envelope extent = myViewPoint.TargetGeometry as Envelope;
// Return if extent is null.
if (extent == null) { return; }
// Create an envelope that is a bit smaller than the extent.
EnvelopeBuilder envelopeBldr = new EnvelopeBuilder(extent);
envelopeBldr.Expand(0.80);
// Get the (only) graphics overlay in the map view.
GraphicsOverlay extentOverlay = MyMapView.GraphicsOverlays.FirstOrDefault();
// Return if the extent overlay is null.
if (extentOverlay == null) { return; }
// Get the extent graphic.
Graphic extentGraphic = extentOverlay.Graphics.FirstOrDefault();
// Create the extent graphic and add it to the overlay if it doesn't exist.
if (extentGraphic == null)
{
extentGraphic = new Graphic(envelopeBldr.ToGeometry());
extentOverlay.Graphics.Add(extentGraphic);
}
else
{
// Otherwise, update the graphic's geometry.
extentGraphic.Geometry = envelopeBldr.ToGeometry();
}
}
private async Task StartGeodatabaseGeneration()
{
// Update the geodatabase path.
_gdbPath = $"{Path.GetTempFileName()}.geodatabase";
// Create a task for generating a geodatabase (GeodatabaseSyncTask).
_gdbSyncTask = await GeodatabaseSyncTask.CreateAsync(_featureServiceUri);
// Get the (only) graphic in the map view.
Graphic redPreviewBox = MyMapView.GraphicsOverlays.First().Graphics.First();
// Get the current extent of the red preview box.
Envelope extent = redPreviewBox.Geometry as Envelope;
// Get the default parameters for the generate geodatabase task.
GenerateGeodatabaseParameters generateParams = await _gdbSyncTask.CreateDefaultGenerateGeodatabaseParametersAsync(extent);
// Create a generate geodatabase job.
GenerateGeodatabaseJob generateGdbJob = _gdbSyncTask.GenerateGeodatabase(generateParams, _gdbPath);
// Show the progress bar.
MyProgressBarGrid.IsVisible = true;
// Handle the progress changed event with an inline (lambda) function to show the progress bar.
generateGdbJob.ProgressChanged += (sender, e) =>
{
// Get the job.
GenerateGeodatabaseJob job = (GenerateGeodatabaseJob)sender;
// Update the progress bar.
UpdateProgress(job.Progress);
};
// Start the job.
generateGdbJob.Start();
// Get the result.
_resultGdb = await generateGdbJob.GetResultAsync();
// Hide the progress bar.
MyProgressBarGrid.IsVisible = false;
// Handle the job completion.
await HandleGenerationStatusChange(generateGdbJob);
}
private async Task HandleGenerationStatusChange(GenerateGeodatabaseJob job)
{
// If the job completed successfully, add the geodatabase data to the map.
if (job.Status == JobStatus.Succeeded)
{
// Clear out the existing layers.
MyMapView.Map.OperationalLayers.Clear();
// Loop through all feature tables in the geodatabase and add a new layer to the map.
foreach (GeodatabaseFeatureTable table in _resultGdb.GeodatabaseFeatureTables)
{
// Skip non-point tables.
await table.LoadAsync();
if (table.GeometryType != GeometryType.Point)
{
continue;
}
// Create a new feature layer for the table.
FeatureLayer layer = new FeatureLayer(table);
// Add the new layer to the map.
MyMapView.Map.OperationalLayers.Add(layer);
}
// Enable editing features.
_readyForEdits = EditState.Ready;
// Update the help label.
MyHelpLabel.Text = "2. Tap a point feature to select";
}
// See if the job failed.
else if (job.Status == JobStatus.Failed)
{
// Create a message to show the user.
string message = "Generate geodatabase job failed";
// Show an error message (if there is one).
if (job.Error != null)
{
message += ": " + job.Error.Message;
}
else
{
// If no error, show messages from the job.
foreach (JobMessage m in job.Messages)
{
// Get the text from the JobMessage and add it to the output string.
message += "\n" + m.Message;
}
}
// Show the message.
await Application.Current.MainPage.DisplayAlert("Error", message, "OK");
}
}
private async Task SyncGeodatabase()
{
// Return if not ready.
if (_readyForEdits != EditState.Ready) { return; }
// Create parameters for the sync task.
SyncGeodatabaseParameters parameters = new SyncGeodatabaseParameters()
{
GeodatabaseSyncDirection = SyncDirection.Bidirectional,
RollbackOnFailure = false
};
// Get the layer ID for each feature table in the geodatabase, then add to the sync job.
foreach (GeodatabaseFeatureTable table in _resultGdb.GeodatabaseFeatureTables)
{
// Get the ID for the layer.
long id = table.ServiceLayerId;
// Create the SyncLayerOption.
SyncLayerOption option = new SyncLayerOption(id);
// Add the option.
parameters.LayerOptions.Add(option);
}
// Create job.
SyncGeodatabaseJob job = _gdbSyncTask.SyncGeodatabase(parameters, _resultGdb);
// Subscribe to progress updates.
job.ProgressChanged += (o, e) =>
{
UpdateProgress(job.Progress);
};
// Show the progress bar.
MyProgressBarGrid.IsVisible = true;
// Disable the sync button.
MySyncButton.IsEnabled = false;
// Start the sync.
job.Start();
// Wait for the result.
await job.GetResultAsync();
// Hide the progress bar.
MyProgressBarGrid.IsVisible = false;
// Do the rest of the work.
HandleSyncCompleted(job);
// Re-enable the sync button.
MySyncButton.IsEnabled = true;
}
private void HandleSyncCompleted(SyncGeodatabaseJob job)
{
// Tell the user about job completion.
if (job.Status == JobStatus.Succeeded)
{
Application.Current.MainPage.DisplayAlert("Alert", "Geodatabase synchronization succeeded.", "OK");
}
// See if the job failed.
else if (job.Status == JobStatus.Failed)
{
// Create a message to show the user.
string message = "Sync geodatabase job failed";
// Show an error message (if there is one).
if (job.Error != null)
{
message += ": " + job.Error.Message;
}
else
{
// If no error, show messages from the job.
foreach (JobMessage m in job.Messages)
{
// Get the text from the JobMessage and add it to the output string.
message += "\n" + m.Message;
}
}
// Show the message.
Application.Current.MainPage.DisplayAlert("Error", message, "OK");
}
}
private async void GenerateButton_Clicked(object sender, EventArgs e)
{
// Fix the selection graphic extent.
MyMapView.ViewpointChanged -= MapViewExtentChanged;
// Disable the generate button.
try
{
MyGenerateButton.IsEnabled = false;
// Call the geodatabase generation method.
await StartGeodatabaseGeneration();
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK");
}
}
private void MapViewExtentChanged(object sender, EventArgs e)
{
// Call the map extent update method.
UpdateMapExtent();
}
private void UpdateProgress(int progress)
{
// Due to the nature of the threading implementation,
// the dispatcher needs to be used to interact with the UI.
// The dispatcher takes an Action, provided here as a lambda function.
Microsoft.Maui.ApplicationModel.MainThread.BeginInvokeOnMainThread(() =>
{
// Update the progress bar value.
MyProgressBar.Progress = progress / 100.0;
MyProgressLabel.Text = $"{progress}%";
});
}
private async void SyncButton_Click(object sender, EventArgs e)
{
try
{
await SyncGeodatabase();
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Alert", ex.ToString(), "OK");
}
}
}
}