[Shapefile C Library]读写shp图形(C++&.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图形(C++&.net Wapper)的更多相关文章
- [Shapefile C Library]读取shp图形(.net Wapper)
ShapeLib的.net Wapper版可以在官网下载到,在WorldWind中也有使用.ORG据说也是使用的ShapeLib实现的shp文件的读写. 官网:http://shapelib.mapt ...
- 关于GDAL读写Shp乱码的问题总结
目录 1. 正文 1.1. shp文件本身的编码的问题 1.2. 设置读取的编码方式 1.2.1. GDAL设置 1.2.2. 解码方式 1.2.3. 其他 2. 参考 1. 正文 最近在使用GDAL ...
- 通过geotools读写shp文件
依赖jar包 读取shp public static void main(String[] path) { DbaseFileReader reader = null; try { reader = ...
- python GDAL 读写shp文件
gdal包用于处理栅格数据,ogr用于处理矢量数据. 1 #!C:\Program Files\pythonxy\python\python.exe 2 #-*- coding:gb2312 -*- ...
- delphi使用Foxit Quick PDF Library读写pdf文本和图片
简介: Debenu Quick PDF Library(PDF编程开发工具)提供一套全方位的 PDF API 函数,帮助您快速简便地处理 PDF 文件.从文档属性的基本操作到创建您自己的 PDF 查 ...
- shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言
博客背景和目的 最近在用C++写一个底层的东西,需要读取和创建shp文件.虽然接触shp文件已经几年了,但是对于shp文件内到底包含什么东西一直是一知半解.以前使用shp文件都是利用软件(如ArcGI ...
- C#、C++用GDAL读shp文件(转载)
C#.C++用GDAL读shp文件 C#用GDAL读shp文件 (2012-08-14 17:09:45) 标签: 杂谈 分类: c#方面的总结 1.目前使用开发环境为VS2008+GDAL1.81 ...
- grads 读取shp
自从GrADS2.0.a8版本开始,GrADS引入了对shp图形的支持,关于此格式在这里不多说, 于是今晚就简单测试了一下最简单画图和查询命令(后续还将测试输出shp图形的命令) 测试数据采用的 ...
- Android GIS开发系列-- 入门季(13)Gdal简单写个shp文件
Gdal是用来读写栅格与矢量数据的,在Gdal官网,可以下载相关的资源进行平台的编译.其实Arcgis底层也是用Gdal来读取shp文件的,那在Android中可以直接读写shp文件吗,是可以的.这里 ...
随机推荐
- Nordic NRF51822 从零开始系列(一)开发环境的搭建
硬件准备 (1)nrf51822 开发板一块(此处使用的是青云系列的,自带jlijnk ob+usb串口芯片)或者使用nrf51822模块+jlink_ob ( ...
- day8 八、文件操作模式、文件的复制与文件游标操作
一.文件操作 1.wr模式结合 ① w = open('1.txt', 'w', encoding='utf-8') # w:没有文件新建文件,有文件就清空文件 w.write('000\n') w. ...
- Prometheus的架构及持久化
原文: https://my.oschina.net/go4it/blog/855598 Prometheus是什么 Prometheus是一个开源的系统监控和报警工具,特点是 多维数据模型(时序列数 ...
- vim保存只读文件时获得sudo权限
有时使用vim时忘记输入sudo,导致编辑了一大堆文字,最后保存时发现是只读文件,无法保存,每次保存都会提示read only.这时可以使用如下命令代替原有的 :wq 命令 :w !sudo tee ...
- [No0000174]Spring常用注解(收藏大全)
Spring部分 1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @C ...
- Spark安装部署(local和standalone模式)
Spark运行的4中模式: Local Standalone Yarn Mesos 一.安装spark前期准备 1.安装java $ sudo tar -zxvf jdk-7u67-linux-x64 ...
- iOS10原生的语音转文字功能
#import <Foundation/Foundation.h> #import <Speech/Speech.h> @interface SpeechListener : ...
- 结构体指针 Pointers to Structures struct Books Book1; struct Books *struct_pointer;
小结: 1.To access the members of a structure using a pointer to that structure, you must use the → ope ...
- AutoMapper介绍(未完待续、部分没实现)
实体间转换工具.其实也可以用Json来实现同名属性.异名属性(用JsonProperty指明)的自动转换 最新版本6.11 需要使用vs2013以上.vs2012下载新版 nuget会遇到问题.只能旧 ...
- [archlinux][crypto] 从T450迁移archlinux操作系统至T460s笔记本
从T450笔记本迁移archlinux操作系统之T460s笔记本,同时: 1. 使用cryptsetup做底层块加密. 2. 全新使用btrfs文件系统. 一,硬盘分区. 1T的SSD,使用U ...