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

官网:http://shapelib.maptools.org/

1. C++读取shpfile文件代码

  1. int main()
  2. {
  3. //读取shp
  4. const char * pszShapeFile = "data\\LineSegments2.shp";
  5. SHPHandle hShp= SHPOpen(pszShapeFile, "r");
  6. int nShapeType, nVertices;
  7. int nEntities = 0;
  8. double* minB = new double[4];
  9. double* maxB = new double[4];
  10. SHPGetInfo(hShp, &nEntities, &nShapeType, minB, maxB);
  11. printf("ShapeType:%d\n", nShapeType);
  12. printf("Entities:%d\n", nEntities);
  13. for (int i = 0; i < nEntities;i++)
  14. {
  15. int iShape = i;
  16. SHPObject *obj = SHPReadObject(hShp, iShape);
  17. printf("--------------Feature:%d------------\n",iShape);
  18. int parts = obj->nParts;
  19. int verts=obj->nVertices;
  20. printf("nParts:%d\n", parts);
  21. printf("nVertices:%d\n", verts);
  22. for (size_t i = 0; i < verts; i++)
  23. {
  24. double x=obj->padfX[i];
  25. double y = obj->padfY[i];
  26. printf("%f,%f;", x,y);
  27. }
  28. printf("\n");
  29. }
  30. SHPClose(hShp);
  31. system("pause");
  32.  
  33. }

输出结果:


2. 以下是.net读取Shp文件中图形的代码:

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. OpenFileDialog dlg = new OpenFileDialog();
  4. dlg.Filter = "(*.shp)|*.shp";
  5. if (dlg.ShowDialog() == DialogResult.OK)
  6. {
  7. string fileName = dlg.FileName;
  8. txtFilePath.Text = fileName;
  9. ReadSHP(fileName);
  10. }
  11. }
  12.  
  13. private void ReadSHP(string FILENAME)
  14. {
  15. IntPtr hShp;
  16. hShp = ShapeLib.SHPOpen(FILENAME, "rb+");
  17.  
  18. // get shape info and verify shapes were created correctly
  19. double[] minB = new double[];
  20. double[] maxB = new double[];
  21. int nEntities = ;
  22. ShapeLib.ShapeType shapeType = ;
  23. ShapeLib.SHPGetInfo(hShp, ref nEntities, ref shapeType, minB, maxB);
  24. listBox1.Items.Add(string.Format("Number Entries: {0}", nEntities));
  25. listBox1.Items.Add(string.Format("ShapeType: {0}", shapeType));
  26. listBox1.Items.Add(string.Format("Min XY: {0}, {1}", minB[], minB[]));
  27. listBox1.Items.Add(string.Format("Max XY: {0}, {1}", maxB[], maxB[]));
  28.  
  29. // test SHPReadObject on the first shape
  30. for (int i = ; i < nEntities; i++)
  31. {
  32. int iShape = i;
  33. listBox1.Items.Add(string.Format("Shape({0}): ", iShape));
  34. IntPtr pshpObj = ShapeLib.SHPReadObject(hShp, iShape);
  35.  
  36. // Get the SHPObject associated with our IntPtr pshpObj
  37. // We create a new SHPObject in managed code, then use Marshal.PtrToStructure
  38. // to copy the unmanaged memory pointed to by pshpObj into our managed copy.
  39. ShapeLib.SHPObject shpObj = new ShapeLib.SHPObject();
  40. Marshal.PtrToStructure(pshpObj, shpObj);
  41.  
  42. listBox1.Items.Add(string.Format("Min XY of shape({0}): ({1}, {2})", iShape, shpObj.dfXMin, shpObj.dfYMin));
  43. listBox1.Items.Add(string.Format("Max XY of shape({0}): ({1}, {2})", iShape, shpObj.dfXMax, shpObj.dfYMax));
  44. listBox1.Items.Add(string.Format("Points of shape({0}): ({1})", iShape, shpObj.nVertices));
  45. int parts = shpObj.nParts;
  46. listBox1.Items.Add(string.Format("Parts of shape({0}): ({1})", iShape, parts));
  47. if (parts>)
  48. {
  49. int[] partStart = new int[parts];
  50. Marshal.Copy(shpObj.paPartStart, partStart, , parts);
  51. for (int j = ; j < partStart.Length; j++)
  52. {
  53. listBox1.Items.Add(string.Format("FirstPart of shape({0}): ({1})", iShape, partStart[j]));
  54. }
  55. int[] partType = new int[parts];
  56. Marshal.Copy(shpObj.paPartType, partType, , parts);
  57. for (int j = ; j < partType.Length; j++)
  58. {
  59. listBox1.Items.Add(string.Format("FirstPartType of shape({0}): ({1})", iShape, (MapTools.ShapeLib.PartType)partType[j]));
  60. }
  61. }
  62.  
  63. ShapeLib.SHPDestroyObject(pshpObj);
  64. }
  65. ShapeLib.SHPClose(hShp);
  66. Console.WriteLine("\nPress any key to continue...");
  67. Console.ReadLine();
  68. }

3.新建shp并保存属性

  1. //简化后保存
  2. const char* saveFileName = "data\\simplyRoom.shp";
  3. int nShpTpyeSave = SHPT_POLYGON;
  4. SHPHandle outShp = SHPCreate(saveFileName, nShpTpyeSave);
  5. DBFHandle dbf_h = DBFCreate(saveFileName);
  6. int fieldIdx=DBFAddField(dbf_h, "Shape", FTInteger, 2, 0);
  7. SHPObject *psShape;
  8.  
  9. for (int ir=0;ir<rooms_.size();ir++)
  10. {
  11. printf("--------------Room:%d------------\n",ir);
  12. std::vector<Coordinate> coords=rooms_[ir].simplyCoords_;
  13. double *xCoords = new double[coords.size()];
  14. double *yCoords = new double[coords.size()];
  15. for (int ip=0;ip<coords.size();ip++)
  16. {
  17. double x=coords[ip].x;
  18. double y=coords[ip].y;
  19. xCoords[ip] = x;
  20. yCoords[ip] = y;
  21. printf("%f,%f;\n", x,y);
  22. }
  23. printf("\n");
  24. psShape = SHPCreateObject(nShpTpyeSave, -1, 0, NULL, NULL, coords.size(), xCoords, yCoords, NULL, NULL);
  25.  
  26. std::cout << std::endl;
  27. int ishape=SHPWriteObject(outShp, -1, psShape);
  28. SHPDestroyObject(psShape);
  29. DBFWriteIntegerAttribute(dbf_h, ishape, 0, ishape);
  30. }
  31. SHPClose(outShp);
  32. DBFClose(dbf_h);

[Shapefile C Library]读取shp图形(.net Wapper)的更多相关文章

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

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

  2. C#读取shp文件并获取图形保存到sde要素类中(不使用ESRI的类库,纯c#实现)

    说明:首先要将sde要素类发布成对应的要素服务,通过对要素服务的操作,实现数据在sde要素类中的增删 //向服务器发出请求 public string getPostData(string postS ...

  3. GDAL读取Shp问题解决:Unable to open EPSG support file gcs.csv

    在GIS软件的开发中,经常用到开源库GDAL读取Shp数据,当shp数据中包含投影信息时,可能会遇到“Unable to open EPSG support file gcs.csv”错误提示,该错误 ...

  4. VS2017编译GDAL(64bit)+解决C#读取Shp数据中文路径的问题

    编译GDAL过程比较繁琐,查阅了网上相关资料,同时通过实践,完成GDAL的编译,同时解决了SHP数据中文路径及中文字段乱码的问题,本文以“gdal-2.3.2”版本为例阐述整个编译过程. 一.编译准备 ...

  5. grads 读取shp

    自从GrADS2.0.a8版本开始,GrADS引入了对shp图形的支持,关于此格式在这里不多说, 于是今晚就简单测试了一下最简单画图和查询命令(后续还将测试输出shp图形的命令)    测试数据采用的 ...

  6. Silverlight项目笔记8:层次布局、客户端读取shp、ExecuteCountAsync、柱状图、url传参

    1.层次布局 由于地图窗口和菜单栏都在一个父容器内,在浏览器缩小到一定程度点击地图弹出infoWindow时,会出现菜单栏遮挡infoWindow中间部分的现象,于是通过设置Canvas.ZIndex ...

  7. GDAL C#读取shp中文属性值乱码问题

    GDAL的C#版本读取shp中,如果属性值中含有中文,读出来有可能是乱码的问题,根据SWIG生成的C#代码调试发现问题所在,在Ogr.cs文件中有这么一个函数,代码如下: internal stati ...

  8. Java 在PPT中创建SmartArt图形、读取SmartArt图形中的文本

    一.概述及环境准备 SmartArt 图形通过将文字.图形从多种不同布局.组合来表现内容和观点的逻辑关系,能够快速.有效地传达设计者的意图和信息.这种图文表达的视觉表示形式常用于PPT,Word,Ex ...

  9. GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换

    GeoJson的生成与解析 一.wkt格式的geometry转成json格式 二.json格式转wkt格式 三.json格式的数据进行解析 四.Java读写geojson 五.geotools读取sh ...

随机推荐

  1. 对Get-Content参数-readcount的解释

    绝大多数用户更关心最新的日志,下面给出一个简单的例子演示从一个文本日志中获取最后的某几行文本行:   # 显示windowsupdate.log 文件的最新5行日志 $logs = Get-Conte ...

  2. iOS 用宏定义写一个单例(Singleton)

    用如下方法定义单例 @interface singleton_interface(ClassName); @end 实现单例在 @implemention singleton_implemention ...

  3. metasploit 常用命令汇总

    1.连接数据并显示一些信息 db_connet username:password@ip address/database name db_destroy 同上 db_import 文件名字 db_h ...

  4. PowerCmd-cmd命令行管理工具

    缘由 看了大漠在慕课网关于angular的讲解,用到了PowerCmd.就百度一下准备下载,很小很实用的工具,发现有人总结了.备忘 http://www.cnblogs.com/xing901022/ ...

  5. Linux改变文件或目录的访问权限命令

    使用  ll  或  ls -l 指令时 第一列会显示出目录下文件的权限 例如∶ -rw-r-r- 横线代表空许可.r代表只读,w代表写,x代表可执行.注意这里共有10个位置.第一个字符指定了文件类型 ...

  6. VIM常用快捷键~网页上查找

    转自~木枫林 转自~鸟哥的私房菜 第十章.vim 程序编辑器 第十章.vim 程序编辑器 最近更新日期:2009/08/20 2. vi 的使用 2.1 简易执行范例 2.2 按键说明 2.3 一个案 ...

  7. System.Data.OracleClient.OracleConnection已过时

    解决办法如下: 1.把原来的using System.Data.OracleClient;去掉 2.在oracle安装目录下找到Oracle.DataAccess.dll 添加引用:using Ora ...

  8. buffer overflow vulnerabilitie

    Computer Systems A Programmer's Perspective Second Edition Avoiding security holes.For many years,bu ...

  9. Java List 如何传值

    // 合并 List 中的相同数据行 // Source : tmpOrderEntryListBeanList // Target : resultOrderEntryListBeanList // ...

  10. comms.nottingham.ac.uk/learningtechnology

    http://comms.nottingham.ac.uk/learningtechnology/