ArcGIS 10.1中有一个新的功能就是动态图层(NOT DynamicLayer,动态地图服务),该动态图层简单的说,就是向一个动态服务注册一个工作空间,该工作空间中含有shape,栅格等数据,这样的话,客户端可以通过API获取这个工作空间下的所有shape数据,并可以对其进行渲染。ArcGIS Runtime和ArcGIS Server的架构类似,而API也十分相像,ArcGIS Runtime加载shape数据除了我们前面介绍的方法,还可以通过动态图层这样的方式,原理和ArcGIS Server的一样,现在说下步骤:
加载MPK(相当于ArcGIS Server的动态地图);
注册一个包含shape数据的工作空间,也就是一个目录;
遍历该目录,获取shape数据,然后加载。
核心代码:
01.OpenFileDialog openFileDialog = new OpenFileDialog(); 02. openFileDialog.Filter = "Image files (*.bmp,*.png,*.sid,*.tif)|*.bmp;*.png;*.sid;*.tif;"; 03. openFileDialog.RestoreDirectory = true; 04. openFileDialog.Multiselect = true; 05. if (openFileDialog.ShowDialog() == true) 06. { 07. try 08. { 09. // // 添加栅格 10. AddFileDatasetToDynamicMapServiceLayer(WorkspaceFactoryType.Raster, 11. Path.GetDirectoryName(openFileDialog.FileName), 12. new List<string>(openFileDialog.SafeFileNames), 13. arcGisLocalDynamicMapServiceLayer => 14. { 15. 16. _map.Layers.Add(arcGisLocalDynamicMapServiceLayer); 17. }); 18. } 19. catch (Exception ex) 20. { 21. MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); 22. } 23. }
01.public void AddFileDatasetToDynamicMapServiceLayer(WorkspaceFactoryType workspaceType, string directoryPath, List<string> fileNames, Action<ArcGISLocalDynamicMapServiceLayer> callback) 02. { 03. try 04. { 05. // 产生UID 06. string uniqueId = Guid.NewGuid().ToString(); 07. 08. // 工作空间信息 09. WorkspaceInfo workspaceInfo = new WorkspaceInfo(uniqueId, workspaceType, "DATABASE=" + directoryPath); 10. 11. // 本地服务,该方法是NET3新特性 12. LocalMapService localMapService = new LocalMapService 13. { 14. 15. Path = _emptyMpkPath, 16. 17. //这个属性很关键 18. EnableDynamicLayers = true 19. }; 20. 21. // 给动态地图服务注册工作空间 22. localMapService.DynamicWorkspaces.Add(workspaceInfo); 23. 24. // 异步方法在ArcGIS Runtime中处处可见 25. localMapService.StartAsync(x => 26. { 27. 28. ArcGISLocalDynamicMapServiceLayer arcGisLocalDynamicMapServiceLayer = null; 29. 30. 31. arcGisLocalDynamicMapServiceLayer = new ArcGISLocalDynamicMapServiceLayer(localMapService) 32. { 33. // 工作空间ID 34. ID = "Workspace: " + (new DirectoryInfo(directoryPath)).Name, 35. 36. //动态图层 37. EnableDynamicLayers = true, 38. }; 39. 40. 41. arcGisLocalDynamicMapServiceLayer.Initialized += (s, e) => 42. { 43. // 动态图层集合 44. DynamicLayerInfoCollection dynamicLayerInfoCollection = new DynamicLayerInfoCollection(); 45. 46. // 符号 47. LayerDrawingOptionsCollection layerDrawingOptionsCollection = new LayerDrawingOptionsCollection(); 48. 49. 50. int counter = 0; 51. foreach (string fileName in fileNames) 52. { 53. 54. DynamicLayerInfo dynamicLayerInfo = new DynamicLayerInfo 55. { 56. 57. ID = counter, 58. 59. 60. Name = "Dataset: " + fileName 61. }; 62. 63. 64. DataSource dataSource = null; 65. 66. 67. if (workspaceInfo.FactoryType == WorkspaceFactoryType.Raster) 68. { 69. 70. dataSource = new RasterDataSource 71. { 72. 73. DataSourceName = fileName, 74. 75. 76. WorkspaceID = workspaceInfo.Id 77. }; 78. } 79. else 80. { 81. 82. dataSource = new TableDataSource 83. { 84. 85. DataSourceName = fileName, 86. 87. 88. WorkspaceID = workspaceInfo.Id 89. }; 90. 91. 92. var layerDrawOpt = new LayerDrawingOptions() 93. { 94. 95. LayerID = counter, 96. 97. // 简单渲染 98. Renderer = new SimpleRenderer() { Symbol = new SimpleMarkerSymbol() { } }, 99. }; 100. 101. 102. layerDrawingOptionsCollection.Add(layerDrawOpt); 103. } 104. 105. 106. dynamicLayerInfo.Source = new LayerDataSource { DataSource = dataSource }; 107. 108. 109. dynamicLayerInfoCollection.Add(dynamicLayerInfo); 110. 111. 112. counter++; 113. } 114. 115. 116. arcGisLocalDynamicMapServiceLayer.DynamicLayerInfos = dynamicLayerInfoCollection; 117. 118. // 这两个一一对应 119. arcGisLocalDynamicMapServiceLayer.LayerDrawingOptions = layerDrawingOptionsCollection; 120. 121. 122. arcGisLocalDynamicMapServiceLayer.Refresh(); 123. 124. 125. callback(arcGisLocalDynamicMapServiceLayer); 126. }; 127. 128. 129. arcGisLocalDynamicMapServiceLayer.Initialize(); 130. }); 131. } 132. catch (Exception ex) 133. { 134. MessageBox.Show(ex.Message,"Error",MessageBoxButton.OK,MessageBoxImage.Error); 135. } 136. }
转自:http://blog.csdn.net/arcgis_cs/article/details/7858477