Runtime的两大用途,一是外业数据采集,二是地图数据呈现。这篇博客先来看看外业数据采集。
Runtime中数据采集有两种模式:桌面模式和服务模式。
桌面模式
1、 使用ArcMap制作地图.geodatabase文件
2、 将.geodatabase文件拷贝至移动端
3、 在移动端查看、查询、分析、编辑
4、 将数据同步回ArcMap
服务模式
1、 使用ArcMap制作地图(.mxd)
2、 将.mxd发布至ArcGIS Server
3、 移动端连接ArcGISServer下载数据
4、 在移动端查看、查询、分析、编辑
5、 将数据同步回ArcGISServer
桌面模式
1、在ArcMap中导入数据,并制作.geodatabase文件。
2、将数据拷贝至移动端
3、移动端操作代码如下,仅先给出加载代码,查询编辑保存的放到最后面。
private async void Loadgeodatabase_Click(object sender, RoutedEventArgs e) { string GDB_PATH = @"C:\Temp\Cache\LocalGeodatabase.geodatabase"; gdb = await Geodatabase.OpenAsync(GDB_PATH); foreach (var table in gdb.FeatureTables) { var flayer = new FeatureLayer() { ID = table.Name, DisplayName = table.Name, FeatureTable = table }; //将新建立的图层加入显示 MyMapView.Map.Layers.Add(flayer); } }4、外业采集归来后将.geodatabase文件拷贝至ArcMap打开。
服务模式
服务模式的制作地图文档、发布服务这里不去详细录屏解释。
但是,作者根据多年经验,觉得以下几点必须得说明,否则会报如下错误:00184: Layer is not configured for or cannot be used with the Sync capability
1、发布服务需要勾选Feature Access功能,否则是不行的。
2、Feature Access功能的服务需要SDE的支持,并且版本化,包含Globe ID字段或者不版本化而开启存档。
报错解决方案详情请参见:
http://resources.arcgis.com/zh-cn/help/main/10.2/index.html#//00sq000000w1000000(中文版)
http://resources.arcgis.com/en/help/main/10.2/index.html#//00sq000000w1000000(原汁原味的英文版)
好,到这里,我们假设,支持Runtime编辑同步的要素服务已经发布成功,服务地址为:https://sf.esri.com:6443/arcgis/rest/services/ChinaMap/FeatureServer
从服务器下载.geodatabase文件
var featureServiceUri = new Uri("https://sf.esri.com:6443/arcgis/rest/services/ChinaMap/FeatureServer"); var gdbSyncTask = await GeodatabaseSyncTask.CreateAsync(featureServiceUri); //确定要下载的范围,这里设置为MapView的当前视域范围。 Envelope extent = MainWindow.MapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry as Envelope; //获得gdb参数 var generateGdbParams = await gdbSyncTask.CreateDefaultGenerateGeodatabaseParametersAsync(extent); //设置同步模式为以图层为单位,还可以将其设置为SyncModel.Geodatabase,即以整库为单位同步。 generateGdbParams.SyncModel = SyncModel.Layer; //设置为同步第一个图层 generateGdbParams.LayerOptions.Clear(); generateGdbParams.LayerOptions.Add(0); //不返回附件 generateGdbParams.ReturnAttachments = false; //传入gdb参数、.geodatabase文件存放路径的参数,获得一个GenerateGeodatabaseJob对象-generateGdbJob var generateGdbJob = gdbSyncTask.GenerateGeodatabase(generateGdbParams, geodatabasefile); //设置监听generateGdbJob的状态改变 generateGdbJob.JobChanged += OnGenerateGdbJobChanged; generateGdbJob.Start();
private async void OnGenerateGdbJobChanged(object sender, EventArgs e) { var job = sender as GenerateGeodatabaseJob; if (job.Error != null) { Console.WriteLine("Error creating geodatabase: " + job.Error.Message); MessageBox.Show("失败"); return; } //检查状态 if (job.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded) { Console.WriteLine("文件已成功创建"); } else if (job.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed) { Console.WriteLine("创建失败"); } else { Console.WriteLine(job.Messages[job.Messages.Count - 1].Message); } }至此,.geodatabase文件已下载至移动终端。
离线编辑
添加要素
//构建空间属性 MapPoint mp = new MapPoint(108, 33); //构建非空间属性 var attributes = new Dictionary<string, object>(); attributes.Add("typdamage", "type1"); attributes.Add("primcause", "wildfire"); //从本地文件获得FeatureTable Geodatabase gdb = await Geodatabase.OpenAsync(FilePath); GeodatabaseFeatureTable ft = null; if(gdb.GeodatabaseFeatureTables.Count>0) ft = gdb.GeodatabaseFeatureTables[0]; var newfeature=ft.CreateFeature(attributes, mp); await ft.AddFeatureAsync(newfeature);
更新要素
Geodatabase gdb = await Geodatabase.OpenAsync(FilePath); FeatureTable ft = null; if (gdb.GeodatabaseFeatureTables.Count > 0) ft = gdb.GeodatabaseFeatureTables[0]; //选择要素的代码 var selectedFeatures = await fl.GetSelectedFeaturesAsync(); var selectedFeature = selectedFeatures.FirstOrDefault(); await ((ArcGISFeature)selectedFeature).LoadAsync(); selectedFeature.Attributes["typename"] = "type2"; selectedFeature.Geometry = new MapPoint(109, 33); await ft.UpdateFeatureAsync(selectedFeature);
删除要素
Geodatabase gdb = await Geodatabase.OpenAsync(FilePath); FeatureTable ft = null; if (gdb.GeodatabaseFeatureTables.Count > 0) ft = gdb.GeodatabaseFeatureTables[0]; //选择要素的代码 var selectedFeatures = await fl.GetSelectedFeaturesAsync(); var selectedFeature = selectedFeatures.FirstOrDefault(); await ft.DeleteFeatureAsync(selectedFeature);
添加附件
Geodatabase gdb = await Geodatabase.OpenAsync(FilePath); FeatureTable ft = null; if (gdb.GeodatabaseFeatureTables.Count > 0) ft = gdb.GeodatabaseFeatureTables[0]; //选择要素的代码 var selectedFeatures = await fl.GetSelectedFeaturesAsync(); var selectedFeature = selectedFeatures.FirstOrDefault(); await ((ArcGISFeature)selectedFeature).AddAttachmentAsync("attachmentname", "image/jpg", Imagedata); await ft.UpdateFeatureAsync(selectedFeature);
编辑后同步
SyncGeodatabaseParameters parameters = new SyncGeodatabaseParameters() { //双向同步,同时下载和上传改动 GeodatabaseSyncDirection = SyncDirection.Bidirectional, RollbackOnFailure = false }; foreach (GeodatabaseFeatureTable table in gdb.GeodatabaseFeatureTables) { long id = table.ServiceLayerId; SyncLayerOption option = new SyncLayerOption(id); parameters.LayerOptions.Add(option); } SyncGeodatabaseJob job = gdbSyncTask.SyncGeodatabase(parameters, gdb); job.JobChanged += Job_JobChanged; job.Start();
private void Job_JobChanged(object sender, EventArgs e) { SyncGeodatabaseJob job = sender as SyncGeodatabaseJob; if (job.Status == JobStatus.Succeeded) { Console.Write("同步成功"); } else if (job.Status == JobStatus.Failed) { Console.Write("同步失败"); } else { Console.Write("同步中……"); } }