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

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

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

int main()
{//读取shpconst 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文件中图形的代码:

 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[4];
20             double[] maxB = new double[4];
21             int nEntities = 0;
22             ShapeLib.ShapeType shapeType = 0;
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[0], minB[1]));
27             listBox1.Items.Add(string.Format("Max XY: {0}, {1}", maxB[0], maxB[1]));
28
29             // test SHPReadObject on the first shape
30             for (int i = 0; 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>0)
48                 {
49                     int[] partStart = new int[parts];
50                     Marshal.Copy(shpObj.paPartStart, partStart, 0, parts);
51                     for (int j = 0; 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, 0, parts);
57                     for (int j = 0; 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并保存属性

//简化后保存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图形(C++.net Wapper)相关推荐

  1. delphi使用Foxit Quick PDF Library读写pdf文本和图片

    简介: Debenu Quick PDF Library(PDF编程开发工具)提供一套全方位的 PDF API 函数,帮助您快速简便地处理 PDF 文件.从文档属性的基本操作到创建您自己的 PDF 查 ...

  2. 读写shp等空间数据,进行geometry、SimpleFeature等转换的工具类

    直接上代码 import org.geotools.data.*; import org.geotools.data.collection.ListFeatureCollection; import ...

  3. Shapefile文件组成,Shapefile文件的修复

    Shapefile维基百科:http://zh.wikipedia.org/wiki/Shapefile  (个人觉得写的很详细) 这些文件英文解释如下(看不懂可以看下面的中文解释): Shapefi ...

  4. SHP(shapefile)文件详细格式介绍

    shape文件由ESRI开发,一个ESRI(Environmental Systems Research Institute)的shape文件包括一个主文件,一个索引文件,和一个dBASE表.其中主文 ...

  5. py读shp文件_入门-Python-读取Shapefile

    前言 补一篇Shapefile的开源读取,Shapefile格式作为GIS的标准格式,几乎是各大GIS平台必须支持的一个交换格式,所以在各个平台上进行SHP的读写都是没问题的.但是如何在开发过程中读写 ...

  6. 绘制 polygons and polylines(shapefile读写):pyshp (shapefile)版本

    注意:安装命令:pip install pyshp,使用:import shapefile Before doing anything you must import the library. > ...

  7. .shp文件的存储结构是怎样的?底层读取shapefile文件

    .shp文件的存储结构是怎样的?底层读取shapefile文件 基础知识 shp的存储结构 python 字节流读取Shp文件 基础知识 大家都比较熟悉shp文件,它是GIS软件可以读取的矢量文件.但 ...

  8. shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言

    博客背景和目的 最近在用C++写一个底层的东西,需要读取和创建shp文件.虽然接触shp文件已经几年了,但是对于shp文件内到底包含什么东西一直是一知半解.以前使用shp文件都是利用软件(如ArcGI ...

  9. Geotools简介以及quickstsrt加载shp文件并显示

    场景 Geotools Geotools是一个java类库,它提供了很多的标准类和方法来处理空间数据,同时这个类库是构建在OGC标准之上的,是OGC思想的一种实现. 而OGC是国际标准,所以geoto ...

最新文章

  1. 清华 NLP 实验室:AI 诗人「九歌」喊你来对对子
  2. 各代程序设计语言拓扑
  3. solr源码分析之数据导入DataImporter追溯。
  4. 让193FW显示1440x900还真不容易
  5. python列表操作
  6. 《C++ Primer》13.1.6节练习(部分)
  7. SQL注入学习part06:(结合sqli-libs学习:51-61关)
  8. 在HTML中取得请求中的参数
  9. C++中内存分配方式、空指针及野指针的区别
  10. Hive metastore入门
  11. 高德地图两点间距离计算函数
  12. 440 亿美元成交!Twitter 「卖身」马斯克
  13. 基于STM32平台的数字温度显示器系统设计
  14. webpack 5.5.1 compiled with 1 error in 63 ms
  15. maven_使用Maven Failsafe和JUnit @Category将集成测试与单元测试分开
  16. (ROS)Moveit编程示例
  17. pymysql 插入错误:Warning(1265, “Data truncated for column ‘XXXX‘ at row xxx“)
  18. 单点登录之CAS SSO从入门到精通(第三天)
  19. 腾讯云基础认证是什么?多少分通过?
  20. python学习笔记(类)

热门文章

  1. C语言实现课堂随机点名系统
  2. 华为测试软件csfb分析,华为完成创新语音解决方案Ultra-Flash CSFB 的端到端测试
  3. Photoshop CS6 简体中文版
  4. 不建议安装macOS Ventura Beta的4个理由
  5. 收到浦发银行总行的体检通知
  6. 浅谈CCD工业相机主要类型有哪些
  7. redis安装-启动
  8. 2-3树的插入和删除操作
  9. 选择英雄,暴君,青巫,风灵,代码控制界面显示
  10. 22万个木箱!TWaver 3D极限压榨