[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 ...
随机推荐
- Scrum会议2
小组名称:天天向上 项目名称:连连看 参会成员:王森(Master)张金生 张政 栾骄阳 时间:2016.10.17 已完成内容: 1.连连游戏的图片素材选取. 2.连连看具体的功能种类. 计划完成: ...
- PAT天梯赛练习题 L3-011. 直捣黄龙(多关键字SPFA+DFS)
L3-011. 直捣黄龙 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题是一部战争大片 —— 你需要从己方大本营出发,一路 ...
- CSS兼容性
1,浮动 ie6中,设置浮动的元素,width自动包裹内容了.通常我们要设定一下这个元素的宽度 子元素浮动父容器高度不能自适应的CSS解决方法 网页前端工作者经常会遇到子元素设置float浮动后导致父 ...
- Bash 快捷键大全
快捷键的一些说明: CTRL=C:这个键是指PC键盘上的Ctrl键 ALT=M:这个键是PC键盘上的ALT键,如果你键盘上没有这个键,可以尝试使用ESC键代替 SHIFT=S:此键是PC上的Shift ...
- RocEDU.阅读.写作
RocEDU.阅读.写作 一.选择图书 <黑客大曝光> 二.读书计划 56天内学习完.时间:2016.01.25--2016.03.20 三.承诺 宋宸宁郑重承诺: 1.在56天内(开始时 ...
- Redis学习笔记(7)-事务
package cn.com; import java.util.List; import redis.clients.jedis.Jedis; import redis.clients.jedis. ...
- 管子&小白
管夷吾已入朝,稽首谢罪,桓公亲手扶起,赐之以坐.夷吾曰:“臣乃俘戮之余,得蒙宥死,实为万幸,敢辱过礼!”桓公曰:“寡人有问于子,子必坐,然后敢请."夷吾再拜就坐. 桓公曰:“齐,千乘之国,先 ...
- sphinx续4-coreseek的工作原理
原文地址:http://blog.itpub.net/29806344/viewspace-1399621/ 在分析sphix原理之前,我先澄清一下为什么经常出现coreseek这个词? 因为sphi ...
- php--rbac权限
- 设计模式:中介者模式(Mediator)
定 义:用一个中介对象来封装一系列对象的交互.中介者使各个对象不需要显示地相互作用,从而耦合松散,而且可以独立的改变他们之间的交互. 结构图: Mediator类,抽象中介者类 abstract ...