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图形(C++&.net Wapper)的更多相关文章

  1. [Shapefile C Library]读取shp图形(.net Wapper)

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

  2. 关于GDAL读写Shp乱码的问题总结

    目录 1. 正文 1.1. shp文件本身的编码的问题 1.2. 设置读取的编码方式 1.2.1. GDAL设置 1.2.2. 解码方式 1.2.3. 其他 2. 参考 1. 正文 最近在使用GDAL ...

  3. 通过geotools读写shp文件

    依赖jar包 读取shp public static void main(String[] path) { DbaseFileReader reader = null; try { reader = ...

  4. python GDAL 读写shp文件

    gdal包用于处理栅格数据,ogr用于处理矢量数据. 1 #!C:\Program Files\pythonxy\python\python.exe 2 #-*- coding:gb2312 -*- ...

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

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

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

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

  7. C#、C++用GDAL读shp文件(转载)

    C#.C++用GDAL读shp文件 C#用GDAL读shp文件 (2012-08-14 17:09:45) 标签: 杂谈 分类: c#方面的总结 1.目前使用开发环境为VS2008+GDAL1.81 ...

  8. grads 读取shp

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

  9. Android GIS开发系列-- 入门季(13)Gdal简单写个shp文件

    Gdal是用来读写栅格与矢量数据的,在Gdal官网,可以下载相关的资源进行平台的编译.其实Arcgis底层也是用Gdal来读取shp文件的,那在Android中可以直接读写shp文件吗,是可以的.这里 ...

随机推荐

  1. Bitmap(三)

    转自:http://www.open-open.com/lib//view/open1333418945202.html Bitmap是Android系统中的图像处理的最重要类之一.用它可以获取图像文 ...

  2. oracle 创建表同时添加注释

    创建数据库表.添加注释的方法: create table WARNINGRECORD ( RecordID ) primary key not null ); comment on column WA ...

  3. HTTP 响应状态码

    MDN https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status section 10 of RFC 2616 https://tools.ie ...

  4. KMP 算法详解

    之前模模糊糊的理解了KMP,结果由于并不是完全弄清楚而导致自己在一道题目上疯狂的T,似乎是next函数写的有问题,于是痛心疾首的回来写一篇报告,警示自己 对KMP来说,匹配串的next数组是重中之重, ...

  5. IIS 使用域账户访问SQL 需要配置

    打开应用程序的 Web.config 文件并添加以下元素:   <authentication mode="Windows" /> <identity imper ...

  6. [No0000106]配置PLSQL,提升工作效率

    界面模板的配置: 方便用户快速点击需要的功能.如打开SQL Window 1.打开customize,用户自定义Toolbars对话框. 2.在Commands命令标签页,选中要添加的命令,拖动到工具 ...

  7. [转载]win7x64下的redis安装与使用

    原文应用为:`https://www.cnblogs.com/koal/p/5484916.html` 先引用百度百科的一段话吧,具体可以到百科查看吧. Redis是一个开源的使用ANSI C语言编写 ...

  8. 【每日一题】 UVA - 11809 Floating-Point Numbers 阅读题+取对数处理爆double

    https://cn.vjudge.net/problem/UVA-11809 题意:很长orz 题解:算一下输入范围,发现用double是读不进来的,在这里wa了半天,(double 1e300  ...

  9. hive优化之并行执行任务

    1.与Oracle并行技术一样,hive在执行mapreduce作业时也可以执行并行查询.针对于不同业务场景SQL语句的执行情况,有些场景下SQL的执行是需要分割成几段去执行的,而且期间并不全是存在依 ...

  10. 网络层block,delegate之优劣分析

    正常情况下, block 缺点: 1.block很难追踪,难以维护 2.block会延长先关对象的生命周期 block会给内部所有的对象引用计数+1, 一方面会带来潜在的循环引用(retain cyc ...