系统是死的,数据是活的,在一些涉及敏感数据的地方,可能需要提供访问客户端数据的功能。例如:在某单位内部有一个基于ArcGIS Silverlight API的WebGIS系统,提供了一些常用的业务功能,能够访问一些基础地理数据和基本的业务数据。而有的业务数据,由于保密要求,可能不通过系统直接提供,需要在客户端直接访问。
在客户端访问Shapefile,可以通过直接读取的方式,以及将数据传回服务器端读取的方式来实现。ESRI已经公开了Shapefile的文件格式,这为我们从客户端直接读取Shapefile提供了便利。
下文将演示如何从客户端直接读取Shapefile文件。
下面的代码是处理Map的Drop事件,即通过拖放文件的方式来读取Shapefile。注意需要同时拖放.shp 和.dbf文件。
private voID myMap_Drop(object sender,DragEventArgs e){ try { //获取拖放到地图上的文件信息 IDataObject dataObject = e.Data as IDataObject; fileInfo[] files = dataObject.GetData(DataFormats.fileDrop) as fileInfo[]; //判断拖放的文件是否为.shp和.dbf fileInfo shapefile = null; fileInfo dbffile = null; foreach (fileInfo fi in files) { if (fi.Extension.Tolower() == ".shp") shapefile = fi; if (fi.Extension.Tolower() == ".dbf") dbffile = fi; } // 读取Shapefile数据 Shapefile shapefileReader = new Shapefile(); if (shapefile != null && dbffile != null) { shapefileReader.Read(shapefile,dbffile); } else { MessageBox.Show("请将.dbf和.shp文件同时拖放到地图上!"); return; } IList<Graphic> lstGraphics = new List<Graphic>(); foreach (ShapefileRecord record in shapefileReader.Records) { //将从Shapefile中读取的记录转换为Graphic Graphic graphic = record.topointGraphic(); if (graphic != null) lstGraphics.Add(graphic); } // 如果空间参考不一致,可能需要投影 if (lstGraphics.Count > 0) { GeometryService projectTask = new GeometryService("http://localhost/arcgis/rest/services/Geometry/GeometryServer"); projectTask.ProjectCompleted += new EventHandler<GraphicsEventArgs>(projectTask_ProjectCompleted); projectTask.Failed += new EventHandler<TaskFailedEventArgs>(projectTask_Failed); //将平面坐标转换为经纬度 projectTask.ProjectAsync(lstGraphics,myMap.SpatialReference); } } catch (Exception ex) { MessageBox.Show("拖放文件错误:" + ex.ToString()); }}然后需要在投影完成事件中处理结果,为Graphic指定符号,并添加到图层中。
下面是效果图:
底图数据(波特兰市区)
Shapefile数据(波特兰911报警电话拨打位置)
拖放效果(波特兰报警电话拨打区域分布)
篇幅所限,只贴了核心代码,如需完整代码,请在评论住留下邮箱地址。
欢迎加入ArcGIS Silverlight API讨论群交流:147535735
总结以上是内存溢出为你收集整理的ArcGIS API for Silverlight应用开发系列(2)客户端读取Shapefile全部内容,希望文章能够帮你解决ArcGIS API for Silverlight应用开发系列(2)客户端读取Shapefile所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)