GDAL(Geospatial Data Abstractionlibrary)是一个在X/MIT许可协议下的开源栅格空间数据转换库。它利用抽象数据模型来表达所支持的各种文件格式。它还有一系列命令行工具来进行数据转换和处理。
本文将使用GDAL/OGR库读写Postgresql数据库中的表,代码执行环境在ubuntu12.04,
直接上代码
#include "/usr/include/gdal/ogrsf_frmts.h"#include "/usr/include/gdal/ogr_feature.h"#include "/usr/include/gdal/ogr_geometry.h"#include "/usr/include/gdal/gdal_priv.h" /////////////////////////////////使用OGR读Postgresql///////////////////////////////int getFeature( vector<RoadRec>& RoadList //RoadRec是自定义数据结构 ){ OGRRegisterall(); const char* filepath = "PG:dbname=test host=172.0.0.1 port=5432 user=postgres password=postgres"; const char* drivename = "Postgresql"; //标明是Postgresql数据库 *** 作 const char* ptablename = "roadList";//数据表名称 table name OGRSfdriver* pdriver = NulL; OGRLayer* player = NulL; OGrdataSource* pDS = NulL; //注册驱动,这样ogr就知道即将打开的是什么类型的文件 pdriver = OGRSfdriverRegistrar::GetRegistrar()->GetDriverByname(drivename); if ( pdriver == NulL ) { return FAILURE; } //驱动注册完毕打开数据库 pDS = pdriver->Open(filepath,0); if ( NulL == pDS ) { return FAILURE; } //打开数据库中的数据表 player = pDS->GetLayerByname(ptablename); if ( NulL == player ) { return FAILURE; } //OGRFeature*相当于指向数据表中一条记录的指针,根据它可以获取每一个字段 OGRFeature* pogrfeature = NulL; player->resetReading(); int gID = 0; //循环遍历每一条记录,这里的遍历是按照表中数据的存储顺序遍历 //并不会按照主键唯一值顺序遍历,这和sql的select结果的顺序不一样 //想要一样应该创建索引,并将表数据按索引顺序存储/*CREATE INDEX roadList_gID_IDx ON roadList USING btree (gID);cluster roadList using roadList_gID_IDx;*/ while( (pogrfeature = player->GetNextFeature()) != NulL ) { gID++; //获取一条记录中的几何属性字段的引用 OGRGeometry *pgeo = pogrfeature->GetGeometryRef(); if ( NulL != pgeo ) { //判断一下是不是自己想要的类型,这里我的数据是道路,line数据 if ( wkbMultilinestring == pgeo->getGeometryType() || wkblinestring == pgeo->getGeometryType() ) { OGRGeometry* pgeometry = pgeo; //单独处理一下multilinestring的情况 if ( wkbMultilinestring == pgeo->getGeometryType() ) { OGRMultilinestring* pmultilinestring = (OGRMultilinestring*)pgeo; if( 1 != pmultilinestring->getNumGeometrIEs() ) { return FAILURE; } pgeometry = pmultilinestring->getGeometryRef(0); } //定义OGRlinestring类型指针指向几何数据 //这样就可以使用OGRlinestring提供的函数接口了 OGRlinestring* pline = (OGRlinestring *)pgeometry; int pointnum = pline->getNumPoints(); RoadRec tmp;//自定义数据类型 //使用OGRFeature类提供的 GetFIEldAsInteger//方法获取每个字段的值,”link_ID”,”road_name”都是字段名 tmp.link_ID = pogrfeature->GetFIEldAsInteger("link_ID"); //tmp.src_ID = pogrfeature->GetFIEldAsInteger("src_ID"); tmp.road_name = pogrfeature->GetFIEldAsstring("road_name"); tmp.one_way = pogrfeature->GetFIEldAsInteger("one_way"); //获得几何属性的每一个点坐标信息 for ( int pointID = 0; pointID < pointnum;++pointID ) { OGRPoint point; pline->getPoint(pointID,&point); GEO_POINT geo_point; geo_point.x = point.getX(); geo_point.y = point.getY(); tmp.vstShpList.push_back(geo_point); } RoadList.push_back(tmp); } } //释放Feature资源 OGRFeature::DestroyFeature(pogrfeature); //cout<<link_ID<<endl; } //释放指向该数据库的指针OGrdataSource::DestroyDataSource(pDS); return SUCCESS;}/////////////////////////////////使用OGR写Postgresql///////////////////////////////int setFeature( const vector<RoadRec>& RoadList ){ OGRRegisterall(); const char* filepath = "PG:dbname=test host=172.0.0.1 port=5432 user=postgres password=postgres"; const char* drivename = "Postgresql"; const char* ptablename = "roadList"; OGRSfdriver* pdriver = NulL; OGRLayer* player = NulL; OGrdataSource* pDS = NulL; pdriver = OGRSfdriverRegistrar::GetRegistrar()->GetDriverByname(drivename); if ( pdriver == NulL ) { return FAILURE; } pDS = pdriver->Open(filepath,0); if ( NulL == pDS ) { return FAILURE; } //相当于SQL语句中的创建数据表,只是这里只先指定表名称和几何字段属性 player = pDS->CreateLayer( ptablename,NulL,wkblinestring,NulL ); if ( NulL == player ) { return FAILURE; } //定义一个字段one_way OGRFIEldDefn* pfIElddefn_oneway = new OGRFIEldDefn("one_way",OFTInteger); //在数据表中创建定义的字段 player->CreateFIEld(pfIElddefn_oneway); OGRFIEldDefn* pfIElddefn_name = new OGRFIEldDefn("road_name",OFTString); player->CreateFIEld(pfIElddefn_name); //删除字段定义指针 delete pfIElddefn_oneway; delete pfIElddefn_name; int roadnum = RoadList.size(); //循环写入每一条道路数据 for ( int roadcnt = 0; roadcnt < roadnum ;++roadcnt ) { const RoadRec& roadrec = RoadList.at(roadcnt); OGRlinestring* pline = new OGRlinestring;//要写入的几何字段 int pointnum = roadrec.vstShpList.size(); for ( int pointcnt = 0; pointcnt < pointnum ;++pointcnt ) { const GEO_POINT& point = roadrec.vstShpList.at(pointcnt); pline->addPoint(point.x,point.y); } OGRGeometry* pgeo = (OGRGeometry*)pline; pgeo->setCoordinateDimension(2);//设置坐标系维度 //创建一个指向要写入的记录的指针//指定要写入的数据库player->GetLayerDefn() OGRFeature* pfeature = OGRFeature::CreateFeature( player->GetLayerDefn() ); //设置当前记录的字段值 pfeature->SetFIEld("one_way",roadrec.one_way); pfeature->SetFIEld("road_name",roadrec.road_name.c_str()); if ( OGRERR_NONE != pfeature->SetGeometry( pgeo ) ) { return FAILURE; } //将记录写入数据表 if ( OGRERR_NONE != player->CreateFeature( pfeature ) ) { return FAILURE; } delete pline; OGRFeature::DestroyFeature(pfeature); } OGrdataSource::DestroyDataSource(pDS); return SUCCESS;}int main(){ vector<RoadRec> roadList; getFeature(roadList); cout<<roadList.size()<<endl; setFeature(roadList); return 0;}
编译链接:g++ -o feature feature_pro.cpp -lgdal
总结以上是内存溢出为你收集整理的使用GDAL/OGR *** 作Postgresql数据库全部内容,希望文章能够帮你解决使用GDAL/OGR *** 作Postgresql数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)