【GDAL】聊聊GDAL的数据模型
GDAL是個非常優秀的GIS數據操作庫,最近在和實習生介紹GDAL的簡單使用,順手寫下記錄
本篇記錄柵格數據,代碼環境為C#
在GDAL中,柵格數據大致是以一個Dataset對應一個柵格數據文件(.Tif/GeoTiff格式),而這個柵格中的各種信息被包含在Dataset的對象中作為屬性。
基本上一個柵格數據在GDAL的數據模型中存儲是基于波段的方式,一般一個單波段數據在GDAL中讀取后,所得到的Dataset中僅包含一個Band對象,而BandCount屬性也為1.多波段數據類似,即是說在GDAL里的Dataset對象與在ArcGIS里所談的柵格數據集是類似的概念。
這里以官方文檔為準搬運相關的概念和說明。
Dataset
A dataset (represented by the GDALDataset class) is an assembly of related raster bands and some information common to them all. In particular the dataset has a concept of the raster size (in pixels and lines) that applies to all the bands. The dataset is also responsible for the georeferencing transform and coordinate system definition of all bands. The dataset itself can also have associated metadata, a list of name/value pairs in string form.
Note that the GDAL dataset, and raster band data model is loosely based on the OpenGIS Grid Coverages specification.
其中標紅的部分是在具體的柵格數據處理中我們應該關注的內容,包括數據的大小(圖像的長寬),地理參考和坐標系定義, 數據的元數據等。這些項目在Dataset對象中定義,對所有這個Dataset下的Band有效。
接下來首先討論Dataset中包含的基本內容。
非常重要的是數據的投影和地理參考。
數據的坐標系【Coordinate System】
GDAL的坐標系定義采用OpenGIS的投影字符串規范表示,所以當你使用 Dataset.GetProjection()方法時會發現返回值為一個字符串而不是什么Projection對象。這保證在大部分情況下數據的投影文件(信息)能夠被GDAL讀取并正確解釋。這個坐標系定義中包含以下內容:
An overall coordinate system name.
A geographic coordinate system name.
A datum identifier.
An ellipsoid name, semi-major axis, and inverse flattening.
A prime meridian name and offset from Greenwich.
A projection method type (i.e. Transverse Mercator).
A list of projection parameters (i.e. central_meridian).
A units name, and conversion factor to meters or radians.
Names and ordering for the axes.
Codes for most of the above in terms of predefined coordinate systems from authorities such as EPSG.
這里感覺需要提一下的就是在調用一些GDAL的投影轉換方法時,要求的參數可能寫作“WKT”,熟悉OpenGIS的會知道這是OpenGIS WKT coordiante System Definitions,也就是這里的投影字符串。
個人感覺稍微有用一些的Tips是最近同事提醒的,原來用于判定兩個數據是否同一個坐標系統我是直接采用Dataset.GetProjection()對得到的字符串做Equals判斷,這樣并不嚴謹。原因自然是某些軟件讀取了數據之后會將其WKT坐標系定義(此處存疑)修改為其他標準的坐標系定義,所以更建議使用GDAL中的OGR庫的SpatialReference對象進行判定。
代碼示例如下(暫時沒學會怎么插入代碼段,先截圖了)
因為GDAL是C++的庫,所以習慣各方面保持C++的風格,比如條件判斷基本是以01方式做,需要習慣下。
轉換參數【GeoTransform】
這個參數一般可以被叫做6參數,因為其對象是個double[6]數組。這個參數用于標定數據的地理位置等信息,相關的方法是 Dataset.GetGeoTransform(out double[] args)、 Dataset.SetGeoTransform(double[] args)
GDAL datasets have two ways of describing the relationship between raster positions (in pixel/line coordinates) and georeferenced coordinates. The first, and most commonly used is the affine transform (the other is GCPs).
關于六參數的具體解釋將在另外的文章中解釋。
一個真實地理坐標和影像數據行列的轉換關系如下:
Xgeo = GT(0) + Xpixel*GT(1) + Yline*GT(2)
Ygeo = GT(3) + Xpixel*GT(4) + Yline*GT(5)
【GCPs】
關于GCPs了解不多,這里暫時搬運官方解釋
A dataset can have a set of control points relating one or more positions on the raster to georeferenced coordinates. All GCPs share a georeferencing coordinate system (returned by GDALDataset::GetGCPProjection()). Each GCP (represented as the GDAL_GCP class) contains the following:
typedef struct
{
char *pszId;
char *pszInfo;
double dfGCPPixel;
double dfGCPLine;
double dfGCPX;
double dfGCPY;
double dfGCPZ;
} GDAL_GCP;
元數據【Metadata】
這個部分請參閱前一篇博客,關于GDAL的Metadata
柵格波段 【Raster Band】
波段對象(Raster Band)是GDAL中的重要對象。一個Band對象表示一個波段/通道/圖層,因此一個RGB數據在GDAL的模型中實際上是一個包含3個波段的Dataset,其中波段與Red/Green/Blue分別對應。
關于波段的內容同樣將在另一篇博客中詳細解釋。
【Color Table】
這個幾乎沒用過,直接搬運了。
先看結構定義:
A color table consists of zero or more color entries described in C by the following structure:
1 typedef struct
2 {
3 /- gray, red, cyan or hue -/
4 short c1;
5 /- green, magenta, or lightness -/
6 short c2;
7 /- blue, yellow, or saturation -/
8 short c3;
9 /- alpha or black band -/
10 short c4;
11 } GDALColorEntry;
View Code
The color table also has a palette interpretation value (GDALPaletteInterp) which is one of the following values, and indicates how the c1/c2/c3/c4 values of a color entry should be interpreted.
GPI_Gray: Use c1 as gray scale value.
GPI_RGB: Use c1 as red, c2 as green, c3 as blue and c4 as alpha.
GPI_CMYK: Use c1 as cyan, c2 as magenta, c3 as yellow and c4 as black.
GPI_HLS: Use c1 as hue, c2 as lightness, and c3 as saturation.
To associate a color with a raster pixel, the pixel value is used as a subscript into the color table. That means that the colors are always applied starting at zero and ascending. There is no provision for indicating a pre-scaling mechanism before looking up in the color table.
【Overviews】
根據官方說明,這個是波段的縮略圖。
A band may have zero or more overviews. Each overview is represented as a "free standing" GDALRasterBand. The size (in pixels and lines) of the overview will be different than the underlying raster, but the geographic region covered by overviews is the same as the full resolution band.
The overviews are used to display reduced resolution overviews more quickly than could be done by reading all the full resolution data and downsampling.
Bands also have a HasArbitraryOverviews property which is TRUE if the raster can be read at any resolution efficiently but with no distinct overview levels. This applies to some FFT encoded images, or images pulled through gateways (like OGDI) where downsampling can be done efficiently at the remote point.
關于最后兩個對象,后期研究一下再來補充。
感謝觀看
總結
以上是生活随笔為你收集整理的【GDAL】聊聊GDAL的数据模型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STL源码剖析 序列式容器 vector
- 下一篇: python异常处理_Python入门