[Shapefile C Library]读取shp图形(.net Wapper)
ShapeLib的.net Wapper版可以在官网下载到,在WorldWind中也有使用。ORG据说也是使用的ShapeLib实现的shp文件的读写。
官网:http://shapelib.maptools.org/
1. C++读取shpfile文件代码
- int main()
- {
- //读取shp
- const char * pszShapeFile = "data\\LineSegments2.shp";
- SHPHandle hShp= SHPOpen(pszShapeFile, "r");
- int nShapeType, nVertices;
- int nEntities = 0;
- double* minB = new double[4];
- double* maxB = new double[4];
- SHPGetInfo(hShp, &nEntities, &nShapeType, minB, maxB);
- printf("ShapeType:%d\n", nShapeType);
- printf("Entities:%d\n", nEntities);
- for (int i = 0; i < nEntities;i++)
- {
- int iShape = i;
- SHPObject *obj = SHPReadObject(hShp, iShape);
- printf("--------------Feature:%d------------\n",iShape);
- int parts = obj->nParts;
- int verts=obj->nVertices;
- printf("nParts:%d\n", parts);
- printf("nVertices:%d\n", verts);
- for (size_t i = 0; i < verts; i++)
- {
- double x=obj->padfX[i];
- double y = obj->padfY[i];
- printf("%f,%f;", x,y);
- }
- printf("\n");
- }
- SHPClose(hShp);
- system("pause");
- }
输出结果:
2. 以下是.net读取Shp文件中图形的代码:
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog dlg = new OpenFileDialog();
- dlg.Filter = "(*.shp)|*.shp";
- if (dlg.ShowDialog() == DialogResult.OK)
- {
- string fileName = dlg.FileName;
- txtFilePath.Text = fileName;
- ReadSHP(fileName);
- }
- }
- private void ReadSHP(string FILENAME)
- {
- IntPtr hShp;
- hShp = ShapeLib.SHPOpen(FILENAME, "rb+");
- // get shape info and verify shapes were created correctly
- double[] minB = new double[];
- double[] maxB = new double[];
- int nEntities = ;
- ShapeLib.ShapeType shapeType = ;
- ShapeLib.SHPGetInfo(hShp, ref nEntities, ref shapeType, minB, maxB);
- listBox1.Items.Add(string.Format("Number Entries: {0}", nEntities));
- listBox1.Items.Add(string.Format("ShapeType: {0}", shapeType));
- listBox1.Items.Add(string.Format("Min XY: {0}, {1}", minB[], minB[]));
- listBox1.Items.Add(string.Format("Max XY: {0}, {1}", maxB[], maxB[]));
- // test SHPReadObject on the first shape
- for (int i = ; i < nEntities; i++)
- {
- int iShape = i;
- listBox1.Items.Add(string.Format("Shape({0}): ", iShape));
- IntPtr pshpObj = ShapeLib.SHPReadObject(hShp, iShape);
- // Get the SHPObject associated with our IntPtr pshpObj
- // We create a new SHPObject in managed code, then use Marshal.PtrToStructure
- // to copy the unmanaged memory pointed to by pshpObj into our managed copy.
- ShapeLib.SHPObject shpObj = new ShapeLib.SHPObject();
- Marshal.PtrToStructure(pshpObj, shpObj);
- listBox1.Items.Add(string.Format("Min XY of shape({0}): ({1}, {2})", iShape, shpObj.dfXMin, shpObj.dfYMin));
- listBox1.Items.Add(string.Format("Max XY of shape({0}): ({1}, {2})", iShape, shpObj.dfXMax, shpObj.dfYMax));
- listBox1.Items.Add(string.Format("Points of shape({0}): ({1})", iShape, shpObj.nVertices));
- int parts = shpObj.nParts;
- listBox1.Items.Add(string.Format("Parts of shape({0}): ({1})", iShape, parts));
- if (parts>)
- {
- int[] partStart = new int[parts];
- Marshal.Copy(shpObj.paPartStart, partStart, , parts);
- for (int j = ; j < partStart.Length; j++)
- {
- listBox1.Items.Add(string.Format("FirstPart of shape({0}): ({1})", iShape, partStart[j]));
- }
- int[] partType = new int[parts];
- Marshal.Copy(shpObj.paPartType, partType, , parts);
- for (int j = ; j < partType.Length; j++)
- {
- listBox1.Items.Add(string.Format("FirstPartType of shape({0}): ({1})", iShape, (MapTools.ShapeLib.PartType)partType[j]));
- }
- }
- ShapeLib.SHPDestroyObject(pshpObj);
- }
- ShapeLib.SHPClose(hShp);
- Console.WriteLine("\nPress any key to continue...");
- Console.ReadLine();
- }
3.新建shp并保存属性
- //简化后保存
- const char* saveFileName = "data\\simplyRoom.shp";
- int nShpTpyeSave = SHPT_POLYGON;
- SHPHandle outShp = SHPCreate(saveFileName, nShpTpyeSave);
- DBFHandle dbf_h = DBFCreate(saveFileName);
- int fieldIdx=DBFAddField(dbf_h, "Shape", FTInteger, 2, 0);
- SHPObject *psShape;
- for (int ir=0;ir<rooms_.size();ir++)
- {
- printf("--------------Room:%d------------\n",ir);
- std::vector<Coordinate> coords=rooms_[ir].simplyCoords_;
- double *xCoords = new double[coords.size()];
- double *yCoords = new double[coords.size()];
- for (int ip=0;ip<coords.size();ip++)
- {
- double x=coords[ip].x;
- double y=coords[ip].y;
- xCoords[ip] = x;
- yCoords[ip] = y;
- printf("%f,%f;\n", x,y);
- }
- printf("\n");
- psShape = SHPCreateObject(nShpTpyeSave, -1, 0, NULL, NULL, coords.size(), xCoords, yCoords, NULL, NULL);
- std::cout << std::endl;
- int ishape=SHPWriteObject(outShp, -1, psShape);
- SHPDestroyObject(psShape);
- DBFWriteIntegerAttribute(dbf_h, ishape, 0, ishape);
- }
- SHPClose(outShp);
- DBFClose(dbf_h);
[Shapefile C Library]读取shp图形(.net Wapper)的更多相关文章
- [Shapefile C Library]读写shp图形(C++&.net Wapper)
ShapeLib的.net Wapper版可以在官网下载到,在WorldWind中也有使用.ORG据说也是使用的ShapeLib实现的shp文件的读写. 官网:http://shapelib.mapt ...
- C#读取shp文件并获取图形保存到sde要素类中(不使用ESRI的类库,纯c#实现)
说明:首先要将sde要素类发布成对应的要素服务,通过对要素服务的操作,实现数据在sde要素类中的增删 //向服务器发出请求 public string getPostData(string postS ...
- GDAL读取Shp问题解决:Unable to open EPSG support file gcs.csv
在GIS软件的开发中,经常用到开源库GDAL读取Shp数据,当shp数据中包含投影信息时,可能会遇到“Unable to open EPSG support file gcs.csv”错误提示,该错误 ...
- VS2017编译GDAL(64bit)+解决C#读取Shp数据中文路径的问题
编译GDAL过程比较繁琐,查阅了网上相关资料,同时通过实践,完成GDAL的编译,同时解决了SHP数据中文路径及中文字段乱码的问题,本文以“gdal-2.3.2”版本为例阐述整个编译过程. 一.编译准备 ...
- grads 读取shp
自从GrADS2.0.a8版本开始,GrADS引入了对shp图形的支持,关于此格式在这里不多说, 于是今晚就简单测试了一下最简单画图和查询命令(后续还将测试输出shp图形的命令) 测试数据采用的 ...
- Silverlight项目笔记8:层次布局、客户端读取shp、ExecuteCountAsync、柱状图、url传参
1.层次布局 由于地图窗口和菜单栏都在一个父容器内,在浏览器缩小到一定程度点击地图弹出infoWindow时,会出现菜单栏遮挡infoWindow中间部分的现象,于是通过设置Canvas.ZIndex ...
- GDAL C#读取shp中文属性值乱码问题
GDAL的C#版本读取shp中,如果属性值中含有中文,读出来有可能是乱码的问题,根据SWIG生成的C#代码调试发现问题所在,在Ogr.cs文件中有这么一个函数,代码如下: internal stati ...
- Java 在PPT中创建SmartArt图形、读取SmartArt图形中的文本
一.概述及环境准备 SmartArt 图形通过将文字.图形从多种不同布局.组合来表现内容和观点的逻辑关系,能够快速.有效地传达设计者的意图和信息.这种图文表达的视觉表示形式常用于PPT,Word,Ex ...
- GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换
GeoJson的生成与解析 一.wkt格式的geometry转成json格式 二.json格式转wkt格式 三.json格式的数据进行解析 四.Java读写geojson 五.geotools读取sh ...
随机推荐
- 对Get-Content参数-readcount的解释
绝大多数用户更关心最新的日志,下面给出一个简单的例子演示从一个文本日志中获取最后的某几行文本行: # 显示windowsupdate.log 文件的最新5行日志 $logs = Get-Conte ...
- iOS 用宏定义写一个单例(Singleton)
用如下方法定义单例 @interface singleton_interface(ClassName); @end 实现单例在 @implemention singleton_implemention ...
- metasploit 常用命令汇总
1.连接数据并显示一些信息 db_connet username:password@ip address/database name db_destroy 同上 db_import 文件名字 db_h ...
- PowerCmd-cmd命令行管理工具
缘由 看了大漠在慕课网关于angular的讲解,用到了PowerCmd.就百度一下准备下载,很小很实用的工具,发现有人总结了.备忘 http://www.cnblogs.com/xing901022/ ...
- Linux改变文件或目录的访问权限命令
使用 ll 或 ls -l 指令时 第一列会显示出目录下文件的权限 例如∶ -rw-r-r- 横线代表空许可.r代表只读,w代表写,x代表可执行.注意这里共有10个位置.第一个字符指定了文件类型 ...
- VIM常用快捷键~网页上查找
转自~木枫林 转自~鸟哥的私房菜 第十章.vim 程序编辑器 第十章.vim 程序编辑器 最近更新日期:2009/08/20 2. vi 的使用 2.1 简易执行范例 2.2 按键说明 2.3 一个案 ...
- System.Data.OracleClient.OracleConnection已过时
解决办法如下: 1.把原来的using System.Data.OracleClient;去掉 2.在oracle安装目录下找到Oracle.DataAccess.dll 添加引用:using Ora ...
- buffer overflow vulnerabilitie
Computer Systems A Programmer's Perspective Second Edition Avoiding security holes.For many years,bu ...
- Java List 如何传值
// 合并 List 中的相同数据行 // Source : tmpOrderEntryListBeanList // Target : resultOrderEntryListBeanList // ...
- comms.nottingham.ac.uk/learningtechnology
http://comms.nottingham.ac.uk/learningtechnology/