gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15945
  • QQ554730525
  • 铜币25337枚
  • 威望15352点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
阅读:3396回复:1

[转帖]开源栅格/矢量空间数据转换库-GDAL/OGR

楼主#
更多 发布于:2008-11-08 14:13
<P ><FONT size=3>GDAL(Geospatial Data Abstraction Library)是一个在X/MIT许可协议下的开源栅格空间数据转换库。它利用抽象数据模型来表达所支持的各种文件格式。它还有一系列命令行工具来进行数据转换和处理。</FONT></P>
<P ><FONT size=3>OGR是GDAL项目的一个分支,功能与GDAL类似,只不过它提供对矢量数据的支持。</FONT></P>
<P ><FONT size=3>有很多著名的GIS类产品都使用了GDAL/OGR库,包括ESRI的ArgGIS 9.2,Google Earth和跨平台的GRASS GIS系统。</FONT></P>
<P ><FONT size=3>利用GDAL/OGR库,可以使基于Linux的地理空间数据管理系统提供对矢量和栅格文件数据的支持。</FONT></P>
<P ><FONT size=3><ST1:CHSDATE isrocdate="False" islunardate="False" day="30" month="12" year="1899">1 . </ST1:CHSDATE>GDAL</FONT></P>
<P ><FONT size=3>GDAL提供对多种栅格数据的支持,包括Arc/Info ASCII Grid(asc),GeoTiff (tiff),Erdas Imagine Images(img),ASCII DEM(dem) 等格式。</FONT></P>
<P ><FONT size=3>GDAL使用抽象数据模型(abstract data model)来解析它所支持的数据格式,抽象数据模型包括数据集(dataset),坐标系统,仿射地理坐标转换(Affine Geo Transform), 大地控制点(GCPs), 元数据(Metadata),栅格波段(Raster Band),颜色表(Color Table),子数据集域(Subdatasets Domain),图像结构域(Image_Structure Domain),XML域(XML:Domains)。</FONT></P>
<P ><FONT size=3>GDAL的核心类结构设计如图所示:</FONT></P>
<DIV align=center forimg="1"><FONT size=3><IMG src="http://hiphotos.baidu.com/zhujin2006/pic/item/ee5edb330665a947ac4b5f8a.jpg" border=0></FONT></DIV><FONT size=3><BR></FONT>
<P ></P>
<br>
<P ><FONT size=3></FONT></P>
<P ><FONT size=3>   其中的类说明如下:</FONT></P>
<P ><FONT size=3>    GDALMajorObject类:带有元数据的对象。</FONT></P>
<P ><FONT size=3>    GDALDdataset类:通常是从一个栅格文件中提取的相关联的栅格波段集合和这些波段的元数据;GDALDdataset也负责所有栅格波段的地理坐标转换(georeferencing transform)和坐标系定义。</FONT></P>
<P ><FONT size=3>    GDALDriver类:文件格式驱动类,GDAL会为每一个所支持的文件格式创建一个该类的实体,来管理该文件格式。</FONT></P>
<P ><FONT size=3>    GDALDriverManager类:文件格式驱动管理类,用来管理GDALDriver类。</FONT></P>
<P ><FONT size=3><ST1:CHSDATE isrocdate="False" islunardate="False" day="30" month="12" year="1899">2. </ST1:CHSDATE>OGR</FONT></P>
<P ><FONT size=3>OGR提供对矢量数据格式的读写支持,它所支持的文件格式包括:ESRI Shapefiles, S-57, SDTS, PostGIS,Oracle Spatial, Mapinfo mid/mif , Mapinfo TAB。</FONT></P>
<P ><FONT size=3>       1)OGR体系结构</FONT></P>
<P ><FONT size=3>OGR包括如下几部分:</FONT></P>
<P ><FONT size=3>Geometry:类Geometry (包括OGRGeometry等类)封装了OpenGIS的矢量数据模型,并提供了一些几何操作,WKB(Well Knows Binary)和WKT(Well Known Text)格式之间的相互转换,以及空间参考系统(投影)。</FONT></P>
<P ><FONT size=3>Spatial Reference:类OGRSpatialReference封装了投影和基准面的定义。</FONT></P>
<P ><FONT size=3>Feature:类OGRFeature封装了一个完整feature的定义,一个完整的feature包括一个geometry和geometry的一系列属性。</FONT></P>
<P ><FONT size=3>Feature Definition:类OGRFeatureDefn里面封装了feature的属性,类型、名称及其默认的空间参考系统等。一个OGRFeatureDefn对象通常与一个层(layer)对应。</FONT></P>
<P ><FONT size=3>Layer:类OGRLayer是一个抽象基类,表示数据源类OGRDataSource里面的一层要素(feature)。</FONT></P>
<P ><FONT size=3>Data Source:类OGRDataSource是一个抽象基类,表示含有OGRLayer对象的一个文件或一个数据库。</FONT></P>
<P ><FONT size=3>Drivers:类OGRSFDriver对应于每一个所支持的矢量文件格式。类OGRSFDriver由类OGRSFDriverRegistrar来注册和管理。</FONT></P>
<P ><FONT size=3>      2) OGR的Geometry模型</FONT></P>
<P ><FONT size=3>OGR的Geometry模型是建立在OpenGIS的简单要素数据模型之上的。如下图所示:</FONT></P>
<P ><FONT size=3></FONT></P>
<P ><FONT size=3></P>
<DIV align=center forimg="1"><IMG src="http://hiphotos.baidu.com/zhujin2006/pic/item/f656a60f9d0a83e9aa64578b.jpg" border=0></DIV><BR></FONT>
<p>
<P  align=center><FONT size=3>                    图-OGR的Geometry模型关系图</FONT></P>
<P ><FONT size=3>OpenGIS的简单要素数据模型,其关系图如下所示:</FONT></P>
<P ><FONT size=3></P>
<DIV align=center forimg="1"><IMG src="http://hiphotos.baidu.com/zhujin2006/pic/item/7cef0b2468efef2fd5074291.jpg" border=0></DIV><BR></FONT>
<p>
<P  align=center><FONT size=3>             图-OpenGIS的简单要素数据模型</FONT></P>
<P ><FONT size=3>由上面两图的对比,可以清楚的看到,OGR的Geometry模型是严格遵循OpenGIS的简单要素数据规范的。OGR的Geometry模型不仅在继承体系上与OpenGIS的简单要素数据模型一致,在函数接口上也向其靠拢,从基本的获取Geometry对象信息的方法如Dimension ( )、GeometryType ( )、SRID ( )、Envelope( )、AsText( )、Boundary( )等到判定空间未知关系的方法如Equals(anotherGeometry:Geometry)、Disjoint(anotherGeometry:Geometry)、</FONT></P>
<P ><FONT size=3>Intersects(anotherGeometry:Geometry)、Touches(anotherGeometry:Geometry)等都是符合其标准的。</FONT></P>
<P ><FONT size=3>      3)OGR的API使用范例</FONT></P>
<P ><FONT size=3>以下C++示例代码展示了使用OGR的API来读取矢量数据。</FONT></P><PRE><FONT face=宋体 color=#000099 size=3>#include "ogrsf_frmts.h"<BR></FONT><FONT face=宋体 color=#ff6633 size=3><BR>int</FONT><FONT face=宋体 color=#990000 size=3> main</FONT><FONT face=宋体 size=3><STRONG><FONT color=#663300>()<BR>{</FONT></STRONG><EM><FONT color=#999999><BR>    //注册所有的文件格式驱动<BR></FONT></EM>    OGRRegisterAll<STRONG><FONT color=#663300>();</FONT></STRONG><EM><FONT color=#999999><BR><BR>    //打开point.shp文件<BR></FONT></EM>    OGRDataSource<STRONG><FONT color=#663300> *</FONT></STRONG>poDS<STRONG><FONT color=#663300> =</FONT></STRONG> OGRSFDriverRegistrar<STRONG><FONT color=#663300>::</FONT></STRONG>Open<STRONG><FONT color=#663300>(</FONT></STRONG></FONT><FONT face=宋体 color=#009900 size=3> "point.shp"</FONT><FONT face=宋体 size=3><STRONG><FONT color=#663300>,</FONT></STRONG> FALSE<STRONG><FONT color=#663300> );</FONT></STRONG><EM><FONT color=#999999><BR><BR>    //获取点层<BR></FONT></EM>    OGRLayer<STRONG><FONT color=#663300> *</FONT></STRONG>poLayer<STRONG><FONT color=#663300> =</FONT></STRONG> poDS<STRONG><FONT color=#663300>-></FONT></STRONG>GetLayerByName<STRONG><FONT color=#663300>(</FONT></STRONG></FONT><FONT face=宋体 color=#009900 size=3> "point"</FONT><FONT face=宋体 size=3><STRONG><FONT color=#663300> );</FONT></STRONG><BR><BR>    OGRFeature<STRONG><FONT color=#663300> *</FONT></STRONG>poFeature<STRONG><FONT color=#663300>;</FONT></STRONG><EM><FONT color=#999999><BR><BR>    //重置该层,确保从层的开始读取数据<BR></FONT></EM>    poLayer<STRONG><FONT color=#663300>-></FONT></STRONG>ResetReading<STRONG><FONT color=#663300>();</FONT></STRONG></FONT><FONT face=宋体 color=#ff0000 size=3><BR><BR>    while</FONT><FONT face=宋体 size=3><STRONG><FONT color=#663300> ( (</FONT></STRONG>poFeature<STRONG><FONT color=#663300> =</FONT></STRONG> poLayer<STRONG><FONT color=#663300>-></FONT></STRONG>GetNextFeature<STRONG><FONT color=#663300>()) !=</FONT></STRONG> NULL<STRONG><FONT color=#663300> )<BR>    {</FONT></STRONG><EM><FONT color=#999999><BR>        //获取该层的属性信息<BR></FONT></EM>        OGRFeatureDefn<STRONG><FONT color=#663300> *</FONT></STRONG>poFDefn<STRONG><FONT color=#663300> =</FONT></STRONG> poLayer<STRONG><FONT color=#663300>-></FONT></STRONG>GetLayerDefn<STRONG><FONT color=#663300>();</FONT></STRONG></FONT><FONT face=宋体 color=#ff6633 size=3><BR>        int</FONT><FONT face=宋体 size=3> iField<STRONG><FONT color=#663300>;</FONT></STRONG></FONT><FONT face=宋体 color=#ff0000 size=3><BR>        for</FONT><FONT face=宋体 size=3><STRONG><FONT color=#663300> (</FONT></STRONG> iField<STRONG><FONT color=#663300> =</FONT></STRONG></FONT><FONT face=宋体 color=#999900 size=3> 0</FONT><FONT face=宋体 size=3><STRONG><FONT color=#663300>;</FONT></STRONG> iField<STRONG><FONT color=#663300> <</FONT></STRONG> poFDefn<STRONG><FONT color=#663300>-></FONT></STRONG>GetFieldCount<STRONG><FONT color=#663300>();</FONT></STRONG> iField<STRONG><FONT color=#663300>++ ) {</FONT></STRONG><EM><FONT color=#999999><BR>     //获取某一个字段的信息<BR></FONT></EM>            OGRFieldDefn<STRONG><FONT color=#663300> *</FONT></STRONG>poFieldDefn<STRONG><FONT color=#663300> =</FONT></STRONG> poFDefn<STRONG><FONT color=#663300>-></FONT></STRONG>GetFieldDefn<STRONG><FONT color=#663300>(</FONT></STRONG> iField<STRONG><FONT color=#663300> );</FONT></STRONG></FONT><FONT face=宋体 color=#ff0000 size=3><BR>            if</FONT><FONT face=宋体 size=3><STRONG><FONT color=#663300>(</FONT></STRONG> poFieldDefn<STRONG><FONT color=#663300>-></FONT></STRONG>GetType<STRONG><FONT color=#663300>() ==</FONT></STRONG> OFTInteger<STRONG><FONT color=#663300> )</FONT></STRONG><BR>                printf<STRONG><FONT color=#663300>(</FONT></STRONG></FONT><FONT face=宋体 color=#009900 size=3> "%d,"</FONT><FONT face=宋体 size=3><STRONG><FONT color=#663300>,</FONT></STRONG> poFeature<STRONG><FONT color=#663300>-></FONT></STRONG>GetFieldAsInteger<STRONG><FONT color=#663300>(</FONT></STRONG> iField<STRONG><FONT color=#663300> ) );<BR>        }</FONT></STRONG><BR>        OGRGeometry<STRONG><FONT color=#663300> *</FONT></STRONG>poGeometry<STRONG><FONT color=#663300>;</FONT></STRONG><EM><FONT color=#999999><BR><BR>        //获取feature<BR></FONT></EM>        poGeometry<STRONG><FONT color=#663300> =</FONT></STRONG> poFeature<STRONG><FONT color=#663300>-></FONT></STRONG>GetGeometryRef<STRONG><FONT color=#663300>();</FONT></STRONG><EM><FONT color=#999999><BR> //用wkbFlatten宏把wkbPoint25D类型转换为wkbPoint类型<BR></FONT></EM></FONT><FONT face=宋体 color=#ff0000 size=3>        if</FONT><FONT face=宋体 size=3><STRONG><FONT color=#663300>(</FONT></STRONG> poGeometry<STRONG><FONT color=#663300> !=</FONT></STRONG> NULL<STRONG><FONT color=#663300> <BR>            ;;</FONT></STRONG> wkbFlatten<STRONG><FONT color=#663300>(</FONT></STRONG>poGeometry<STRONG><FONT color=#663300>-></FONT></STRONG>getGeometryType<STRONG><FONT color=#663300>()) ==</FONT></STRONG> wkbPoint<STRONG><FONT color=#663300> )   {</FONT></STRONG><BR>            OGRPoint<STRONG><FONT color=#663300> *</FONT></STRONG>poPoint<STRONG><FONT color=#663300> = (</FONT></STRONG>OGRPoint<STRONG><FONT color=#663300> *)</FONT></STRONG> poGeometry<STRONG><FONT color=#663300>;</FONT></STRONG><BR>            printf<STRONG><FONT color=#663300>(</FONT></STRONG></FONT><FONT face=宋体 color=#009900 size=3> "%.3f,%3.f\n"</FONT><FONT face=宋体 size=3><STRONG><FONT color=#663300>,</FONT></STRONG> poPoint<STRONG><FONT color=#663300>-></FONT></STRONG>getX<STRONG><FONT color=#663300>(),</FONT></STRONG> poPoint<STRONG><FONT color=#663300>-></FONT></STRONG>getY<STRONG><FONT color=#663300>() );<BR>        }</FONT></STRONG></FONT><FONT face=宋体 color=#ff0000 size=3><BR>        else</FONT><FONT face=宋体 size=3><BR>            printf<STRONG><FONT color=#663300>(</FONT></STRONG></FONT><FONT face=宋体 color=#009900 size=3> "no point geometry\n"</FONT><FONT face=宋体 size=3><STRONG><FONT color=#663300> );</FONT></STRONG><EM><FONT color=#999999><BR>        //销毁feature<BR></FONT></EM>        OGRFeature<STRONG><FONT color=#663300>::</FONT></STRONG>DestroyFeature<STRONG><FONT color=#663300>(</FONT></STRONG> poFeature<STRONG><FONT color=#663300> );<BR>    }</FONT></STRONG><EM><FONT color=#999999><BR>    //销毁数据源,以便关闭矢量文件<BR></FONT></EM>    OGRDataSource<STRONG><FONT color=#663300>::</FONT></STRONG>DestroyDataSource<STRONG><FONT color=#663300>(</FONT></STRONG> poDS<STRONG><FONT color=#663300> );<BR>}</FONT></STRONG></FONT><FONT size=3> </FONT></PRE>
喜欢0 评分0
cafecat
路人甲
路人甲
  • 注册日期2003-07-29
  • 发帖数375
  • QQ
  • 铜币894枚
  • 威望0点
  • 贡献值0点
  • 银元0个
1楼#
发布于:2008-12-15 19:54
这个开源库确实是好东西
http://3s2go.blogspot.com/
举报 回复(0) 喜欢(0)     评分
游客

返回顶部