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.pswp.cn/news/397581.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/397581.shtml
英文地址,請注明出處:http://en.pswp.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