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 ...
随机推荐
- Struts框架2
1.框架:是一个半成品,可以在其基础上在次开发. 2.struts2框架:它是一个web层使用的mvc框架. 3.struts2核心 1.struts2核心 2.xwork核心 4.struts2入门 ...
- HTML文本
1.HTML元素 2.HTML属性 3.HTML文本格式化 4.HTML样式 1.HTML元素 1.什么是HTML元素 HTML 元素指的是从开始标签(start tag)到结束标签(end tag) ...
- css预处理语言--让你的css编写更加简单方便
CSS预处理语言之一-------LESS Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. Less 可以运行在 Nod ...
- vue练手小项目--眼镜在线试戴
最近看到了一个眼镜在线试戴小项目使用纯js手写的,本人刚学习vue.js没多久,便试试用vue做做看了,还没完善. 其中包括初始图片加载,使用keywords查找,父子组件之间传递信息,子组件之间传递 ...
- SiganlR 系列之概述
简介 SignalR 是微软的 http 长连接(以下简称长连接)框架,它的出现为我们提供了一套行之有效的实时通信的解决方案. 背景 在http 1.0 时代,preRequest 都会建立新的tcp ...
- Java伪代码描述《大道至简》第一章
第一节 Begin //原始需求 惩山北之塞,出入之迂 //项目沟通的基本方式 聚室而谋曰 //项目目标 毕力平险,指通豫南,达于汉阴 //技术方案 扣石垦壤,箕畚运于渤海之尾 //技术人员和工程管理 ...
- 简单又炫酷的two.js 二维动画教程
前 言 S N 今天呢给大家介绍一个小js框架,Two.JS.其实在自己学习的过程中并没有找到合适的教程,所以我这种学习延迟的同学是有一定难度的,然后准备给大家整理一份,简单易懂的小教程 ...
- Thirft框架快速入门
Thrift介绍1.什么是thrift?thrift早期由facebook内部团队开发,主要用于实现跨语言间的方法调用,属于远程方法调用的一种,后开源纳入apache中,成为了apache thrif ...
- 550 Create directory operation failed
往Linux系统中上传文件时候,我们经常会使用FTP连接Linux,也经常会使用mkdir命令来创建目录.最近发现用mkdir创建目录时提示550 Create directory operation ...
- 【转】TCP/IP报文格式
1.IP报文格式 IP协议是TCP/IP协议族中最为核心的协议.它提供不可靠.无连接的服务,也即依赖其他层的协议进行差错控制.在局域网环境,IP协议往往被封装在以太网帧(见本章1.3节)中传送.而所有 ...