java gdal postgresql_使用GDAL/OGR操作Postgresql数据库



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& 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<

}

//释放指向该数据库的指针

OGRDataSource::DestroyDataSource(pDS);

return SUCCESS;

}

///

//使用OGR写Postgresql

///

int setFeature( const vector& 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 roadlist;

getFeature(roadlist);

cout<

setFeature(roadlist);

return 0;

}

编译链接:g++ -o feature feature_pro.cpp -lgdal

相关文章

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/397581.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Go语言基础之1--标识符、关键字、变量和常量、数据类型、Go的基本程序结构、Golang的特性...

一、前言 当我们项目较为简单时&#xff0c;我们在src目录下新建一个该项目目录&#xff0c;里面存放源码文件即可&#xff0c;见下图&#xff1a; 当我们一个项目较为复杂时&#xff0c;我们可以在src目录下新建一个该项目目录&#xff0c;在针对该项目不同模块创建不同目录&a…

java参数转换匹配规律_隐式转换规则

隐式转换在如下三种不同的情况会被考虑&#xff1a;1、当对象A调用某个方法时&#xff0c;这个方法不存在例如在前一节介绍的1 to 10。会将Int转换为RichInt&#xff0c;然后再调用to方法。在这种情况下&#xff0c;会将对象A(方法的调用者)隐式转换为另一个对象。2、当对象A调…

Semantic-UI的React实现(二):CSS类构造模块

更简单的类名标签 Semantic-UI使用了更简单的类名声明。用过Bootstrap的同学都会被其复杂的类名标签折磨过&#xff0c;例如一个简单的按键样式&#xff0c;不论颜色或是大小&#xff0c;都需要btn-前缀声明&#xff1a; <button type"button" class"btn btn…

T-SQL LIKE子句 模糊查询

MS SQL Server LIKE子句用于使用通配符运算符将值与类似值进行比较。 有两个通配符与LIKE运算符结合使用: 百分号&#xff08;&#xff05;&#xff09;下划线&#xff08;_&#xff09;百分号表示零个&#xff0c;一个或多个字符。 下划线表示单个数字或字符。 符号可以组合使…

ext springmvc mysql_基于ExtJs6前台,SpringMVC-Spring-Mybatis,resteasy,mysql无限极表设计,实现树状展示数据(treepanel)...

先从后台讲起1.表的设计2.mysql查询很容易&#xff0c;关键是要把id,text,parentId查出来/p>"http://mybatis.org/dtd/mybatis-3-mapper.dtd">SELECTbp.id,bb.name brandName,bp.name text,bp.photo_url photoUrl,bp.number,bp.add_time addTime,bp.modify_tim…

C# 之 Int16 Int32 Int64 的区别

Int16 值类型表示值介于 -32768 到 32767 之间的有符号整数。 Int32 值类型表示值介于 -2,147,483,648 到 2,147,483,647 之间的有符号整数。 Int64 值类型表示值介于 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 之间的整数。 --------------------------------…

php说明代码怎么写,代码怎么写 - 起步 - PHP基础 - KK的小故事

起步 - 代码怎么写 作者&#xff1a;KK发表日期&#xff1a;2016.3.9要写PHP代码就需要建立.php后缀的文件,并且在文件里要以<?php 具体代码 ?>这样的形式来书写PHP代码我们在网站目录下新建一个叫index.php的文件,并在里面编写这样的代码:echo Hello World!;?>然后…

python中的计算符号

1、算数计算符号&#xff1a; - * /   //&#xff08;取整&#xff09;  %&#xff08;取余&#xff09;  **&#xff08;次方&#xff09; 1 >>> 682 143 >>> 9-34 65 >>> 3*46 127 >>> 16/28 8.09 >>> 9/2 10 4.5 11 >…

MySQL 索引优化全攻略

2019独角兽企业重金招聘Python工程师标准>>> 所谓索引就是为特定的mysql字段进行一些特定的算法排序,比如二叉树的算法和哈希算法,哈希算法是通过建立特征值,然后根据特征值来快速查找。而用的最多,并且是mysql默认的就是二叉树算法 BTREE,通过BTREE算法建立索引的字…

织梦DedeCMS实现 三级栏目_二级栏目_一级栏目_网站名称 的效果代码

1.将官方原来的排列方式反过来&#xff0c;找到include/typelink.class.php第164行 $this->valuePositionName $tinfos[typename].$this->SplitSymbol.$this->valuePositionName; 修改为&#xff1a; $this->valuePositionName $this->valuePositionName.$…

MyEclipse 14 设置文件特定的打开方式

2019独角兽企业重金招聘Python工程师标准>>> 打开windows -> preferences&#xff1b; 转载于:https://my.oschina.net/AaronDMC/blog/755481

安装安全狗后php5.5无法访问,关于安全狗的详细介绍

这篇文章主要介绍了win2008 R2安装网站安全狗提示HTTP 错误 500.21的解决方法,需要的朋友可以参考下WINDOWS 2008 R2系统IIS7.5&#xff0c;在没安装网站安全狗前一切正常&#xff0c;安装网站安全狗3.3版后&#xff0c;有部分php网站无法访问。提示如下错误&#xff1a;HTTP 错…

Android 里的数据储存

数据持久化关于数据储存,这个话题已经被反复讨论过很多次了,我是不建议把网络存储这种方式纳入到数据储存的范围的,因为这个和Android没多少关系,因此就有如下的分类: 本地储存(也称之为数据持久化,包含文件储存,SharedPreferences,SQLite储存和ContentProvider(内容提供者)) 内…

[故障解决]Mysql爆出ERROR 1044 (42000)的错误怎么办?

情况如图&#xff0c;使用dvlopenhls可以登陆到这个host&#xff0c;并且可以查看里面的tables&#xff0c;但是使用tables其中的op_flow就会报错&#xff0c;查看了很多地方&#xff0c;有人说要改密码&#xff0c;有人说要grant给权限。五花八门&#xff0c;乱七八糟。其实这…

php如何拼接数组,PHP怎么合并数组

本篇文章主要给大家介绍PHP怎么实现两个数组合并&#xff0c;并且其中一个数组的值为下标&#xff0c;另一个数组的值为对应的值。PHP进行普通数组的合并&#xff0c;相信大家都已经有所掌握。但是对于新手朋友们来说&#xff0c;合并两个数组&#xff0c;新数组的下标和值分别…

UITableView,UICollectionView,UIScrollView快速返回顶部

UITableView&#xff0c; UICollectionView都继承自UIScrollView&#xff0c;所以可以使用UIScrollView的方法&#xff0c;设置显示内容的偏移量 [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES]; 原文链接http://wpdome.sinaapp.com/?p189转载于:https://…

代码编译 Compile、Make、Build 的区别

代码编译 Compile、Make、Build 的区别 https://blog.csdn.net/fanzheng220112583/article/details/7780250 VC6.0中Compile和Build的区别"compile"是“编译”的意思&#xff0c;“build”是“链接”的意思。compile 的作用是对你的代码进行语法检查&#xff0c;将你…

php5 mongodb,ThinkPHP5之Mongodb使用技巧

安装composer require topthink/think-mongo目录结构实践安装完成之后&#xff0c;就根据文档中的介绍开始进行codeing了&#xff0c;但是……首先我们来看下官方的使用文档配置说明不要以为这样就能够正常的使用了&#xff0c;结果远比预想中的艰难直接爆了这样的错误&#xf…

查看并设置oracle并发连接数

1.Sql代码1.select count(*) from v$process select count(*) from v$process --当前的数据库连接数2.Sql代码1.select value from v$parameter where name processes select value from v$parameter where name processes--数据库允许的最大连接数3.Sql代码1.alter system …

spring boot 下载

spring boot 下载 posted on 2018-07-06 22:38 zhouixi 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/1-Admin/p/9275802.html