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 ...
随机推荐
- django的admin或者应用中使用KindEditor富文本编辑器
由于django后台管理没有富文本编辑器,看着好丑,展示出来的页面不美观,无法做到所见即所得的编辑方式,所以我们需要引入第三方富文本编辑器. 之前找了好多文档已经博客才把这个功能做出来,有些博客虽然写 ...
- servlet生成验证码
1.因为朋友们都说现在很少用java自带的图形用户接口,所以小白的我就没去看awt和swing组件,因为要抓紧时间学习后面的,完了出去找工作,等以后再回来了解awt和swing:所以制作验证码的代码是 ...
- HDFS概述(5)————HDFS HA
HA With QJM 目标 本指南概述了HDFS高可用性(HA)功能以及如何使用Quorum Journal Manager(QJM)功能配置和管理HA HDFS集群. 本文档假设读者对HDFS集群 ...
- Query DSL(1)
https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl.html Query DSL GET _search { & ...
- 51nod 2006 飞行员配对(二分图最大匹配) 裸匈牙利算法 求二分图最大匹配题
题目: 题目已经说了是最大二分匹配题, 查了一下最大二分匹配题有两种解法, 匈牙利算法和网络流. 看了一下觉得匈牙利算法更好理解, 然后我照着小红书模板打了一遍就过了. 匈牙利算法:先试着把没用过的左 ...
- Divide Sum 比赛时竟然想不出。。。。。。。
Divide Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSt ...
- eclipse建立一个jsp项目遇到的问题及解决
打开eclipse 在workplace 区域空白处,右键 填写好Project name,之后,点击finished 即可. 选中webcontent,新建一个文件夹,并新建一个jsp 文件 新建 ...
- Python selenium 文件自动下载 (自动下载器)
MyGithub:https://github.com/williamzxl 最新代码已经上传到Github,以下版本为stupid版本. 由于在下载过程中需要下载不同文件,所以可以把所有类型放在Va ...
- Jquery EasyUI Base基础
<pre><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...
- 使用siege对web接口进行post方式的压力测试
为了达到压力测试的效果,需要申请一台线上机器,并且安装压力测试的工具siege. 安装新版siege.资料说yum安装的版本2.70对于post方式支持的不好,验证后发现请求可以正常发过去,但是打开d ...