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. Qt编写守护程序保证程序一直运行(开源)

    没有任何人敢保证自己写的程序没有任何BUG,尤其是在商业项目中,程序量越大,复杂度越高,出错的概率越大,尤其是现场环境千差万别,和当初本地电脑测试环境很可能不一样,有很多特殊情况没有考虑到,如果需要保 ...

  2. ResDepot CRC码

    参考: 百度百科 crc校验 百度百科 crc编码 生日悖论 CRC32能不能用于检验文件的相同性 Egret RES版本控制 一.Egret的ResDepot在发布时,可以添加crc码. 发布前 发 ...

  3. AutoFac记录

    概念 Autofac是一个轻量级的依赖注入的框架,同类型的框架还有Spring.NET,Unity,Castle等: ContainerBuilder:将组件注册成服务的创建者. 组件:Lambda表 ...

  4. HTML+CSS:圆形和圆角图片格式

    效果展示 实现代码 <!DOCTYPE html> <html> <head> <title>JcMan</title> <style ...

  5. 错误票据|2013年蓝桥杯B组题解析第七题-fishers

    错误票据 某涉密单位下发了某种票据,并要在年终全部收回. 因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号. 你的任务是通过编程,找出断号的ID和重号的ID. ...

  6. DataGridView实时提交

    自定义了一个工具,根据DataGridView中的值进行其他操作.在DataGridView中修改了值,直接做其他操作时, 结果DataGridview中的值显示为a,则操作的属性却是没修改后的值b. ...

  7. JS 通过字符串取得对应对象

    //对Array的扩展,查找所有满足条件的元素 Array.prototype.findAll = function (match) { var tmp = []; for (var i = 0; i ...

  8. F#周报2018年第49期

    新闻 ML.NET 0.8--Machine Learning for .NET .NET Core 3预览 1以及开源Windows桌面框架 .NET Core 2.2 尝试C# 8.0 .NET ...

  9. MySQL 聚合函数以及 优先级

    1 from  2 where  3 group by      4 having     5select    6distinct  7 order by  8 limit sum 求和   avg ...

  10. CH 4701 - 天使玩偶 - [CDQ分治]

    题目链接:传送门 关于CDQ分治(参考李煜东<算法竞赛进阶指南>): 对于一系列操作,其中的任何一个询问操作,其结果必然等价于:初始值 + 此前所有的修改操作产生的影响. 假设共有 $m$ ...