C#读写Shapefile
Shapefile文件是ArcGIS存储矢量要素的标准格式,要读写Shapefile最简单的方法当然是基于ArcObject(或者ArcEngine)开发,不过网上也有一些开源的解译Shapefile的代码都是值得参考的,lz曾经用到过一个,源码已经贴到下边,有兴趣的可以下载看看(来源已经记不清了,如果这是您的代码请联系我),下边是两种方法的代码,其实代码很简单,但由于经常会用到所以记下来以便日后查阅。直接上代码。
打开Shapefile:
public static IFeatureClass GetShpFile(string filePath, string fileName)
{
IFeatureWorkspace featureWorkspace;
IFeatureClass featureClass; featureWorkspace = GetShapeWorkspace(filePath) as IFeatureWorkspace; try
{
featureClass = featureWorkspace.OpenFeatureClass(fileName);
}
catch
{
featureClass = null;
} System.Runtime.InteropServices.Marshal.ReleaseComObject(featureWorkspace); return featureClass;
}
public static IWorkspace GetShapeWorkspace(string filePath)
{
IWorkspace workspace; IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass(); workspace = workspaceFactory.OpenFromFile(filePath, ); System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFactory);
workspaceFactory = null; return workspace;
}
向Shapefile中添加要素(一点元素为例):
public bool AddPointsToLayer(ILayer pLayer, List<IPoint> listPoint)
{
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
if (pFeatureLayer == null)
{
System.Windows.Forms.MessageBox.Show(pLayer.Name + "不是矢量图层!");
return false;
} IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
if (pFeatureClass.ShapeType != esriGeometryType.esriGeometryPoint)
{
System.Windows.Forms.MessageBox.Show(pLayer.Name + "不是点图层!");
return false;
} IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true);
IFeatureBuffer pFeatureBuffer = null;
foreach (var p in listPoint)
{
pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
IFeature pNewFeature = pFeatureBuffer as IFeature;
pNewFeature.Shape = p as IGeometry; pNewFeature.set_Value(pNewFeature.Fields.FindField("Name"), "point1");
//pNewFeature.Store(); pFeatureCursor.InsertFeature(pFeatureBuffer);
}
pFeatureCursor.Flush(); return true;
}
读取Shapefile的开源代码点击这里下载,下边是读取代码:
private void openShapeFile()
{
//Create the dialog allowing the user to select the "*.shp" and the "*.dbf" files
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true; if (!(ofd.ShowDialog() ?? false))
return; //Get the file info objects for the SHP and the DBF file selected by the user
FileInfo shapeFile = null;
FileInfo dbfFile = null;
foreach (FileInfo fi in ofd.Files)
{
if (fi.Extension.ToLower() == ".shp")
{
shapeFile = fi;
}
if (fi.Extension.ToLower() == ".dbf")
{
dbfFile = fi;
}
} //Read the SHP and DBF files into the ShapeFileReader
ShapeFile shapeFileReader = new ShapeFile();
if (shapeFile != null && dbfFile != null)
{
shapeFileReader.Read(shapeFile, dbfFile);
}
else
{
HtmlPage.Window.Alert("Please select a SP and a DBF file to proceed.");
return;
} //Add the shapes from the shapefile into a graphics layer named "shapefileGraphicsLayer"
//the greaphics layer should be present in the XAML or created earlier
GraphicsLayer graphicsLayer = MyMap.Layers["shapefileGraphicsLayer"] as GraphicsLayer;
foreach (ShapeFileRecord record in shapeFileReader.Records)
{
Graphic graphic = record.ToGraphic();
if (graphic != null)
graphicsLayer.Graphics.Add(graphic);
}
}
直接拖拽打开:
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.ToGraphic(); //若没有空间参考将会报错,默认设置为地图参考
if (graphic.Geometry.SpatialReference == null)
graphic.Geometry.SpatialReference = MyMap.SpatialReference; if (graphic != null) lstGraphics.Add(graphic);
} // 如果空间参考不一致,可能需要投影
if (lstGraphics.Count > )
{
GeometryService projectTask = new GeometryService("http://tasks.arcgisonline.com/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());
}
}
C#读写Shapefile的更多相关文章
- Java 使用GDAL 读写 shapefile
读取shp文件,并把它转化为json import org.gdal.ogr.*; import org.gdal.ogr.Driver; import org.gdal.gdal.*; public ...
- shapefile 输出的地理处理注意事项
多年来,ESRI 为存储地理信息开发了三种主要数据格式 - coverage 格式.shapefile 格式及地理数据库格式.其中,所开发的 Shapefile 为存储地理及属性信息提供了一种简单的非 ...
- shapefile 输出的地理处理注意事项(转载)
来源:http://resources.arcgis.com/zh-cn/help/main/10.1/index.html#//005600000013000000 多年来,Esri 为存储地理信息 ...
- 常用开源GIS项目
常用开源GIS项目 常用开源桌面GIS软件 QGIS 始于2002年5月,算得上是开源GIS平台中的后起之秀.界面友好,分析功能可与GRASS GIS相媲美.主页:http://www.qgi ...
- 浅谈设计模式在GIS中的应用
设计模式在GIS中的应用 一.设计模式概述 随着面向对象技术的广泛应用,软件复用在越来越多的开发工程中被采用.在研究软件复用的过程中,设计模式的概念被提了出来.所谓设计模式就是一些设计面向对象的软件的 ...
- (数据科学学习手札89)geopandas&geoplot近期重要更新
本文示例代码及数据已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 最近一段时间(本文写作于2020-07-1 ...
- Spring-Boot ☞ ShapeFile文件读写工具类+接口调用
一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...
- [Shapefile C Library]读写shp图形(C++&.net Wapper)
ShapeLib的.net Wapper版可以在官网下载到,在WorldWind中也有使用.ORG据说也是使用的ShapeLib实现的shp文件的读写. 官网:http://shapelib.mapt ...
- JAVA用geotools读写shape格式文件
转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...
随机推荐
- Spring - lookup-method方式实现依赖注入
引言 假设一个单例模式的bean A需要引用另外一个非单例模式的bean B,为了在我们每次引用的时候都能拿到最新的bean B,我们可以让bean A通过实现ApplicationContextWa ...
- eclipse配置maven + 创建maven项目(三)
上篇博文中我们介绍了maven下载.安装和配置(二),这篇博文我们配置一下eclipse,将它和maven结合,并我们创建一个maven的项目. 准备工作 在eclipse配置maven之前需要我们做 ...
- ①【javascript设计到的技术点】
一.dom操作: document.getElementById() document.getElementsByTagName() 二.事件操作: dom2级事件 主流浏览器 addEventLis ...
- jQuery自定义插件--banner图滚动
前言 jQuery是一个功能强大的库,提供了开发JavaScript项目所需的所有核心函数.很多时候我们使用jQuery的原因就是因为其使用插件的功能,然而,有时候我们还是需要使用自定义代码来扩展这些 ...
- 框架基础:ajax设计方案(六)--- 全局配置、请求格式拓展和优化、请求二进制类型、浏览器错误搜集以及npm打包发布
距离上一次博客大概好多好多时间了,感觉再不搞点东西出来,感觉就废了的感觉.这段时间回老家学习驾照,修养,然后7月底来上海求职(面了4家,拿了3家office),然后入职同程旅游,项目赶进度等等一系列的 ...
- css常用属性1
1 背景相关 背景颜色 background-color = 颜色名称/rgb值/十六进制值 背景图片 background-image = url('') 背景图片平铺方式 backgro ...
- 【重点突破】——Cookie的使用
cookie:小甜饼 cookie:保存客户端浏览器中一个纯文本文件 版本高的浏览器可查看 F12->Resource 左下方cookie 查看 cookie作用: 保存:[安全性要 ...
- String Problem hdu 3374 最小表示法加KMP的next数组
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ(1195)(单点修改,区间查询)(二维)
题目大意 给定一个N*N的网格,刚开始每个网格的值都是0,接下来会对这些网格进行操作,有一下两种操作: 1."X Y A"对网格C[x][y]增加A 2."L B R T ...
- bzoj1036 [ZJOI2008]树的统计
一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从 ...