1. 程式人生 > >GDAL庫介面演示(C和C++)

GDAL庫介面演示(C和C++)

OGR API Tutorial

This document is intended to document using the OGR C++ classes to read and write data from a file. It is strongly advised that the read first review the OGR Architecture document describing the key classes and their roles in OGR.

It also includes code snippets for the corresponding functions in C and Python.

Reading From OGR

For purposes of demonstrating reading with OGR, we will construct a small utility for dumping point layers from an OGR data source to stdout in comma-delimited format.

Initially it is necessary to register all the format drivers that are desired. This is normally accomplished by calling 

GDALAllRegister() which registers all format drivers built into GDAL/OGR.

In C++ :

#include "ogrsf_frmts.h" int main() { GDALAllRegister();

In C :

#include "gdal.h" int
main() { GDALAllRegister();

Next we need to open the input OGR datasource. Datasources can be files, RDBMSes, directories full of files, or even remote web services depending on the driver being used. However, the datasource name is always a single string. In this case we are hardcoded to open a particular shapefile. The second argument (GDAL_OF_VECTOR) tells the OGROpen() method that we want a vector driver to be use and that don't require update access. On failure NULL is returned, and we report an error.

In C++ :

GDALDataset *poDS; poDS = ( GDALDataset*) GDALOpenEx( "point.shp", GDAL_OF_VECTOR, NULL, NULL, NULL ); if( poDS == NULL ) { printf( "Open failed.\n" ); exit( 1 ); }

In C :

GDALDatasetH hDS; hDS = GDALOpenEx( "point.shp", GDAL_OF_VECTOR, NULL, NULL, NULL ); if( hDS == NULL ) { printf( "Open failed.\n" ); exit( 1 ); }

GDALDataset can potentially have many layers associated with it. The number of layers available can be queried with GDALDataset::GetLayerCount() and individual layers fetched by index using GDALDataset::GetLayer(). However, we will just fetch the layer by name.

In C++ :

OGRLayer *poLayer; poLayer = poDS-> GetLayerByName( "point" );

In C :

OGRLayerH hLayer; hLayer = GDALDatasetGetLayerByName( hDS, "point" );

Now we want to start reading features from the layer. Before we start we could assign an attribute or spatial filter to the layer to restrict the set of feature we get back, but for now we are interested in getting all features.

While it isn't strictly necessary in this circumstance since we are starting fresh with the layer, it is often wise to call OGRLayer::ResetReading() to ensure we are starting at the beginning of the layer. We iterate through all the features in the layer usingOGRLayer::GetNextFeature(). It will return NULL when we run out of features.

In C++ :

OGRFeature *poFeature; poLayer-> ResetReading(); while( (poFeature = poLayer-> GetNextFeature()) != NULL ) {

In C :

OGRFeatureH hFeature; OGR_L_ResetReading(hLayer); while( (hFeature = OGR_L_GetNextFeature(hLayer)) != NULL ) {

In order to dump all the attribute fields of the feature, it is helpful to get the OGRFeatureDefn. This is an object, associated with the layer, containing the definitions of all the fields. We loop over all the fields, and fetch and report the attributes based on their type.

In C++ :

OGRFeatureDefn *poFDefn = poLayer-> GetLayerDefn(); int iField; for( iField = 0; iField < poFDefn-> GetFieldCount(); iField++ ) { OGRFieldDefn *poFieldDefn = poFDefn-> GetFieldDefn( iField ); if( poFieldDefn-> GetType() == OFTInteger ) printf( "%d,", poFeature-> GetFieldAsInteger( iField ) ); else if( poFieldDefn-> GetType() == OFTInteger64 ) printf( CPL_FRMT_GIB ",", poFeature-> GetFieldAsInteger64( iField ) ); else if( poFieldDefn-> GetType() == OFTReal ) printf( "%.3f,", poFeature-> GetFieldAsDouble(iField) ); else if( poFieldDefn-> GetType() == OFTString ) printf( "%s,", poFeature-> GetFieldAsString(iField) ); else printf( "%s,", poFeature-> GetFieldAsString(iField) ); }

In C :

OGRFeatureDefnH hFDefn = OGR_L_GetLayerDefn(hLayer); int iField; for( iField = 0; iField < OGR_FD_GetFieldCount(hFDefn); iField++ ) { OGRFieldDefnH hFieldDefn = OGR_FD_GetFieldDefn( hFDefn, iField ); if( OGR_Fld_GetType(hFieldDefn) == OFTInteger ) printf( "%d,", OGR_F_GetFieldAsInteger( hFeature, iField ) ); else if( OGR_Fld_GetType(hFieldDefn) == OFTInteger64 ) printf( CPL_FRMT_GIB ",", OGR_F_GetFieldAsInteger64( hFeature, iField ) ); else if( OGR_Fld_GetType(hFieldDefn) == OFTReal ) printf( "%.3f,", OGR_F_GetFieldAsDouble( hFeature, iField) ); else if( OGR_Fld_GetType(hFieldDefn) == OFTString ) printf( "%s,", OGR_F_GetFieldAsString( hFeature, iField) ); else printf( "%s,", OGR_F_GetFieldAsString( hFeature, iField) ); }

There are a few more field types than those explicitly handled above, but a reasonable representation of them can be fetched with the OGRFeature::GetFieldAsString() method. In fact we could shorten the above by using OGRFeature::GetFieldAsString() for all the types.

Next we want to extract the geometry from the feature, and write out the point geometry x and y. Geometries are returned as a generic OGRGeometry pointer. We then determine the specific geometry type, and if it is a point, we cast it to point and operate on it. If it is something else we write placeholders.

In C++ :

OGRGeometry *poGeometry; poGeometry = poFeature-> GetGeometryRef(); if( poGeometry != NULL && wkbFlatten(poGeometry-> getGeometryType()) == wkbPoint ) { OGRPoint *poPoint = ( OGRPoint *) poGeometry; printf( "%.3f,%3.f\n", poPoint-> getX(), poPoint-> getY() ); } else { printf( "no point geometry\n" ); }

In C :

OGRGeometryH hGeometry; hGeometry = OGR_F_GetGeometryRef(hFeature); if( hGeometry != NULL && wkbFlatten( OGR_G_GetGeometryType(hGeometry)) == wkbPoint ) { printf( "%.3f,%3.f\n", OGR_G_GetX(hGeometry, 0), OGR_G_GetY(hGeometry, 0) ); } else { printf( "no point geometry\n" ); }

The wkbFlatten() macro is used above to convert the type for a wkbPoint25D (a point with a z coordinate) into the base 2D geometry type code (wkbPoint). For each 2D geometry type there is a corresponding 2.5D type code. The 2D and 2.5D geometry cases are handled by the same C++ class, so our code will handle 2D or 3D cases properly.

Starting with OGR 1.11, several geometry fields can be associated to a feature.

In C++ :

OGRGeometry *poGeometry; int iGeomField; int nGeomFieldCount; nGeomFieldCount = poFeature-> GetGeomFieldCount(); for(iGeomField = 0; iGeomField < nGeomFieldCount; iGeomField ++ ) { poGeometry = poFeature-> GetGeomFieldRef(iGeomField); if( poGeometry != NULL && wkbFlatten(poGeometry-> getGeometryType()) == wkbPoint ) { OGRPoint *poPoint = ( OGRPoint *) poGeometry; printf( "%.3f,%3.f\n", poPoint-> getX(), poPoint-> getY() ); } else { printf( "no point geometry\n" ); } }

In C :

OGRGeometryH hGeometry; int iGeomField; int nGeomFieldCount; nGeomFieldCount = OGR_F_GetGeomFieldCount(hFeature); for(iGeomField = 0; iGeomField < nGeomFieldCount; iGeomField ++ )

相關推薦

GDAL介面演示CC++

OGR API Tutorial This document is intended to document using the OGR C++ classes to read and write data from a fi

介面程式設計webservicepost

package com; import org.apache.axiom.om.OMElement; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis2.addre

SAP移介面實現C#版

 SAP移庫介面C#版本實現程式碼如下: /// <summary> /// 移庫介面(將倉庫中的地址移到線邊倉,線邊倉的地址用模板檔名稱作為引數傳過來) /// </summary> /// <param

TensorFlow訓練的模型引數WB利用np.savez()儲存用cnpyC++讀取

一、背景 大家經常會在Python環境下用TensorFlow訓練自己的深度學習模型。為了日後能夠使用訓練好的模型,在Python環境下,TensorFlow提供了 tf.train.Saver類,用來儲存模型。這個類提供了一整套函式用於方便的儲存和恢復模型! 但實際情

C C++ 的標準分別有自己的 locale 操作方法,C 標準的 locale 設定函式是 setlocale(),而 C++ 標準有 locale 類流物件的 imbue() 方法gcc使用zh_CN.GBK,或者zh_CN.UTF-8,VC++使用Chinese_People&#

轉自:http://zyxhome.org/wp/cc-prog-lang/c-stdlib-setlocale-usage-note/  [在此向原文作者說聲謝謝!若有讀者看到文章轉載時請寫該轉載地址,不要寫我的BLOG地址。尊重他人的勞動成果 ^_^ ] C 和 C++ 的標準庫分別有自己的

GitHub C C++ 開源的清單含示例程式碼

內容包括:標準庫、Web應用框架、人工智慧、資料庫、圖片處理、機器學習、日誌、程式碼分析等。標準庫C++標準庫,包括了STL容器,演算法和函式等。框架C++通用框架和庫ASL :Adobe原始碼庫提供了同行的評審和可移植的C++原始碼庫。Boost :大量通用C++庫的集合。

2014年8月25日,收藏家殺手——面向對象的C++C

creat os x tracking -m end gin 知識 數據 我們 近期事情特別多,睡眠也都非常晚,有點精神和身體混亂的感覺,所以想寫寫技術分析文章。讓兩者的我都調整一下。這篇技術分析文章是一直想寫的,當前僅僅是開篇,有感覺的時候就寫寫,屬於拼湊而成,興

MD5加密javac#

service int print cnblogs return pan Coding pro pre java代碼 public static String md5(String str) { try { MessageDiges

C#操作MySql數據幫助類Dapper,T-Sql

user lex object per ram int rowfilter close tex using System.Text; using MySql.Data.MySqlClient; using System.Data; using Dapper; using

從電梯問題,看cc++之間的區別有點懂了錯覺錯覺

命令 上下 能夠 current 表現 靈活 沒有 c++ 16px 磕磕碰碰的也相繼用c和c++構造了不少的電梯了。雖然對自我的表現不滿意,但是總體來說還是有一定的收獲的,對於c和c++之間的區別感覺也摸到了一點點門道了。。。 用c語言構造電梯的步驟: 第一步: 分析這個

什麽是 C C ++ 標準

lec 內存占用 串處理 函數 背景 成員 流程 出版 contain 簡要介紹編寫C/C ++應用程序的領域,標準庫的作用以及它是如何在各種操作系統中實現的。我已經接觸C++一段時間了,一開始就讓我感到疑惑的是其內部結構:我所使用的內核函數和類從何而來? 誰發明了它們?

最簡單的排序算法CC++實現

最簡單的排序算法(C和C++實現)1、算法思想如下圖:把待排序的數都存在對應的數組的下標中,如果待排序的數有重復的,就用對應的數組加一,最後把數組的下標打印出來即可。2、源碼(C)如下:#include <stdio.h>int main (void){ int a[100], i, j,

C#Winfrom數據讀取日期年月日格式轉換

tables convert col dataset art spa 類型 time winfrom 顯示類型:2018-6-1 //說明:data_time.Text 控件名文本值 ;dataset.Tables[0].Rows[0]["art_time"] 數據集.表

C程式設計,設定兩個外部中斷INT0INT1按鍵,優先順序IP=0x04,即把外部中斷1設定為高階優先順序,外部中斷0為低階優先順序,使外部中斷1巢狀在外部中斷0中。

問題 用C程式設計,設定兩個外部中斷(INT0和INT1)按鍵,優先順序IP=0x04,即把外部中斷1設定為高階優先順序,外部中斷0為低階優先順序,使外部中斷1巢狀在外部中斷0中。 要求:中斷觸發方式為下降沿觸發,按鍵有消抖功能 模擬 程式碼 #include <

Leetcode 929 獨特的電子郵件PythonC++實現

每封電子郵件都由一個本地名稱和一個域名組成,以 @ 符號分隔。 例如,在 [email protected]中, alice 是本地名稱,而 leetcode.com 是域名。 除了小寫字母,這些電子郵件還可能包含 ','

python3使用ctypes在windows中訪問CC++動態連結函式示例

python3使用ctypes在windows中訪問C和C++動態連結庫函式示例 這是我們的第一個示例,我們儘量簡單,不傳參,不返回,不訪問其他的動態連結庫 一 測試環境介紹和準備 測試環境: 作業系統:windows10 Python版本:3.7.0 VS版本:vs2015社群版(免費) 相關

python3使用ctypes在windows中訪問CC++動態鏈接函數示例

mage linu href types windows 文件 function 動態鏈接 就是 python3使用ctypes在windows中訪問C和C++動態鏈接庫函數示例這是我們的第一個示例,我們盡量簡單,不傳參,不返回,不訪問其他的動態鏈接庫一 測試環境介紹和準備

[轉]CC++執行時

轉自csdn原文:https://blog.csdn.net/ithzhang/article/details/20160009 圖片請去原文檢視 在使用VC構建專案時,經常會遇到下面的連結錯誤:   初學者面對這些錯誤常常不知所錯:libcmt.lib是什麼東西?msvcrtd.dll又是幹嗎用的?在

[轉]CC++運行時

alloc 靜態 開發 dialog 遇到的問題 高版本 pri 工作 系統 轉自csdn原文:https://blog.csdn.net/ithzhang/article/details/20160009 圖片請去原文查看 在使用VC構建項目時,經常會遇到下面的鏈接錯誤:

動態陣列vector簡介JavaC++

1.概念描述: vector名為動態陣列 有些時候想開一個數組,但是卻不知道應該開多大長度的數組合適,因為我們需要用到的陣列很可能會根據情況變動。這時候我們就需要用到動態陣列。所謂動態陣列,也就是不定長陣列,陣列的長度可以根據我們需要動態改變。動態陣列的實現也不難,因為在C++和Java