ArcGIS 10.1中有一个新的功能就是动态图层(NOT DynamicLayer,动态地图服务),该动态图层简单的说,就是向一个动态服务注册一个工作空间,该工作空间中含有shape,栅格等数据,这样的话,客户端可以通过API获取这个工作空间下的所有shape数据,并可以对其进行渲染。ArcGIS Runtime和ArcGIS Server的架构类似,而API也十分相像,ArcGIS Runtime加载shape数据除了我们前面介绍的方法,还可以通过动态图层这样的方式,原理和ArcGIS Server的一样,现在说下步骤:
加载MPK(相当于ArcGIS Server的动态地图);
注册一个包含shape数据的工作空间,也就是一个目录;
遍历该目录,获取shape数据,然后加载。
核心代码:
OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image files (*.bmp,*.png,*.sid,*.tif)|*.bmp;*.png;*.sid;*.tif;"; openFileDialog.RestoreDirectory = true; openFileDialog.Multiselect = true; if (openFileDialog.ShowDialog() == true) { try { // // 添加栅格 AddFileDatasetToDynamicMapServiceLayer(WorkspaceFactoryType.Raster, Path.GetDirectoryName(openFileDialog.FileName), new List<string>(openFileDialog.SafeFileNames), arcGisLocalDynamicMapServiceLayer => { _map.Layers.Add(arcGisLocalDynamicMapServiceLayer); }); } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } }
public void AddFileDatasetToDynamicMapServiceLayer(WorkspaceFactoryType workspaceType, string directoryPath, List<string> fileNames, Action<ArcGISLocalDynamicMapServiceLayer> callback) { try { // 产生UID string uniqueId = Guid.NewGuid().ToString(); // 工作空间信息 WorkspaceInfo workspaceInfo = new WorkspaceInfo(uniqueId, workspaceType, "DATABASE=" + directoryPath); // 本地服务,该方法是NET3新特性 LocalMapService localMapService = new LocalMapService { Path = _emptyMpkPath, //这个属性很关键 EnableDynamicLayers = true }; // 给动态地图服务注册工作空间 localMapService.DynamicWorkspaces.Add(workspaceInfo); // 异步方法在ArcGIS Runtime中处处可见 localMapService.StartAsync(x => { ArcGISLocalDynamicMapServiceLayer arcGisLocalDynamicMapServiceLayer = null; arcGisLocalDynamicMapServiceLayer = new ArcGISLocalDynamicMapServiceLayer(localMapService) { // 工作空间ID ID = "Workspace: " + (new DirectoryInfo(directoryPath)).Name, //动态图层 EnableDynamicLayers = true, }; arcGisLocalDynamicMapServiceLayer.Initialized += (s, e) => { // 动态图层集合 DynamicLayerInfoCollection dynamicLayerInfoCollection = new DynamicLayerInfoCollection(); // 符号 LayerDrawingOptionsCollection layerDrawingOptionsCollection = new LayerDrawingOptionsCollection(); int counter = 0; foreach (string fileName in fileNames) { DynamicLayerInfo dynamicLayerInfo = new DynamicLayerInfo { ID = counter, Name = "Dataset: " + fileName }; DataSource dataSource = null; if (workspaceInfo.FactoryType == WorkspaceFactoryType.Raster) { dataSource = new RasterDataSource { DataSourceName = fileName, WorkspaceID = workspaceInfo.Id }; } else { dataSource = new TableDataSource { DataSourceName = fileName, WorkspaceID = workspaceInfo.Id }; var layerDrawOpt = new LayerDrawingOptions() { LayerID = counter, // 简单渲染 Renderer = new SimpleRenderer() { Symbol = new SimpleMarkerSymbol() { } }, }; layerDrawingOptionsCollection.Add(layerDrawOpt); } dynamicLayerInfo.Source = new LayerDataSource { DataSource = dataSource }; dynamicLayerInfoCollection.Add(dynamicLayerInfo); counter++; } arcGisLocalDynamicMapServiceLayer.DynamicLayerInfos = dynamicLayerInfoCollection; // 这两个一一对应 arcGisLocalDynamicMapServiceLayer.LayerDrawingOptions = layerDrawingOptionsCollection; arcGisLocalDynamicMapServiceLayer.Refresh(); callback(arcGisLocalDynamicMapServiceLayer); }; arcGisLocalDynamicMapServiceLayer.Initialize(); }); } catch (Exception ex) { MessageBox.Show(ex.Message,"Error",MessageBoxButton.OK,MessageBoxImage.Error); } }
效果如下: