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的更多相关文章

  1. Java 使用GDAL 读写 shapefile

    读取shp文件,并把它转化为json import org.gdal.ogr.*; import org.gdal.ogr.Driver; import org.gdal.gdal.*; public ...

  2. shapefile 输出的地理处理注意事项

    多年来,ESRI 为存储地理信息开发了三种主要数据格式 - coverage 格式.shapefile 格式及地理数据库格式.其中,所开发的 Shapefile 为存储地理及属性信息提供了一种简单的非 ...

  3. shapefile 输出的地理处理注意事项(转载)

    来源:http://resources.arcgis.com/zh-cn/help/main/10.1/index.html#//005600000013000000 多年来,Esri 为存储地理信息 ...

  4. 常用开源GIS项目

    常用开源GIS项目     常用开源桌面GIS软件 QGIS 始于2002年5月,算得上是开源GIS平台中的后起之秀.界面友好,分析功能可与GRASS GIS相媲美.主页:http://www.qgi ...

  5. 浅谈设计模式在GIS中的应用

    设计模式在GIS中的应用 一.设计模式概述 随着面向对象技术的广泛应用,软件复用在越来越多的开发工程中被采用.在研究软件复用的过程中,设计模式的概念被提了出来.所谓设计模式就是一些设计面向对象的软件的 ...

  6. (数据科学学习手札89)geopandas&geoplot近期重要更新

    本文示例代码及数据已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 最近一段时间(本文写作于2020-07-1 ...

  7. Spring-Boot ☞ ShapeFile文件读写工具类+接口调用

    一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...

  8. [Shapefile C Library]读写shp图形(C++&.net Wapper)

    ShapeLib的.net Wapper版可以在官网下载到,在WorldWind中也有使用.ORG据说也是使用的ShapeLib实现的shp文件的读写. 官网:http://shapelib.mapt ...

  9. JAVA用geotools读写shape格式文件

    转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...

随机推荐

  1. 易语言 【寻找文本】命令的bug

    最近在重写易语言模块的时候,在取子文本操作时老是出错,经常出现一些奇怪的问题,一开始以为是代码问题,可是找半天硬是找不到问题所在. 于是进入了找bug模式,这么几行代码,看了我半个小时,左改右改,总感 ...

  2. 《Java从入门到放弃》JavaSE入门篇:网络编程(入门版)

    要进行网络编程,首先要搞清楚目的是什么. 网络编程说简单点就是在网络上的计算机进行数据的交互. 既然要进行数据交互,那就需要有一个发送方和一个接受方. 按网络上的说法,就是一个攻一个受· 当然,现阶段 ...

  3. Magento 2.1.X 插件(Plugin)的创建

    Magento 2的插件主要作用:在Magento 1中,为了自定义不同的类和方法,你可以重写一个类. 这是一个非常强大和灵活的定制平台的方式. 这也造成了麻烦,因为两个模块不可以重写同一个类, 重写 ...

  4. angularjs之ui-bootstrap的Datepicker Popup实现双日期选择控件

    最开始使用ui-bootstrap的Datepicker Popup日期选择插件实现双日期选择时间范围时,在网上搜了一些通过JS去实现的方法,不过后来发现可以不必通过JS去处理,只需要使用其自身的属性 ...

  5. Jquery Ajax type的4种类型

    Ajax type这个里面填什么呢?通常大家都会说get和post.那么还有2个是什么呢 $.ajax({ url: 'http://www.cnblogs.com/youku/', type: '' ...

  6. Android中Parcelable接口

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  7. jenkins~集群分发功能和职责处理

    jenkins的多节点集群 在进行自动化部署时,你可以按着它们的项目类型去进行分别部署,这样即可以达到负载均衡,又可以达到一种职责的明确,比如像java的项目你可以使用linux服务来进行部署(拉代码 ...

  8. js特效遮罩层(弹出层)

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  9. uva11401

    题目大意:计算从1,2,3,...,n中选出3个不同的整数,使得以它们为边长可以构成三角形的个数. 思路:用一般的方法需要三重循环,时间复杂度为O(n^3),肯定超时,因此可用数学的方法对问题进行分析 ...

  10. bzoj2330(差分约束)

    题解:这道题是练差分约束的一道好题目吧,我具体在代码中注释,这样更加好理解, 为什么求最长路呢?因为这样保证了满足条件,如果存在正权环,就表示无解,就是 正权环之间不断要更多的糖果才行. #inclu ...