作者:朱金灿
来源:http://blog.csdn.net/clever101
最近要读取kml文件,具体就是把kml文件当作一个矢量文件来读取。我发现gdal是支持集成kml库的。不过集成这个kml库的地方有点复杂。下面作一个详细的说明。
首先要libkml和expat这两个库,具体是修改gdal源码根目录下的nmake.opt,将EXPAT_DIR放在LIBKML_DIR的前面(为了确保EXPAT_LIB能被LIBKML_LIBS识别),具体修改如下:
# Uncomment for Expat support (required for KML, GPX and GeoRSS read support). EXPAT_DIR = $(THIRD_PARTY)/expat_2_1_0_vc9 EXPAT_INCLUDE = -I$(EXPAT_DIR)/include !IFDEF DEBUG EXPAT_LIB = $(THIRD_PARTY)/expat_2_1_0_vc9/lib/libexpatD.lib !ELSE EXPAT_LIB = $(THIRD_PARTY)/expat_2_1_0_vc9/lib/libexpat.lib !ENDIF
LIBKML_DIR的设置修改如下:
# Uncomment out the following lines to enable LibKML support. LIBKML_DIR = $(THIRD_PARTY)/libkml_1_3_0_vc9 LIBKML_INCLUDE = -I$(LIBKML_DIR)/include -I$(THIRD_PARTY)/boost_1_42_vc9/include LIBKML_LIBRARY = $(LIBKML_DIR)/lib LIBKML_LIBS = $(LIBKML_LIBRARY)/libkmlbase.lib \ $(LIBKML_LIBRARY)/libkmlconvenience.lib \ $(LIBKML_LIBRARY)/libkmldom.lib \ $(LIBKML_LIBRARY)/libkmlengine.lib \ $(LIBKML_LIBRARY)/libkmlregionator.lib \ $(LIBKML_LIBRARY)/libkmlxsd.lib \ $(THIRD_PARTY)/minizip_vc9/lib/minizip_static.lib \ $(EXPAT_LIB) \ $(THIRD_PARTY)/uriparser_0_7_5_vc9/lib/uriparser.lib \ # $(LIBKML_DIR)/third_party\zlib-1.2.3.win32/lib/minizip.lib \ $(THIRD_PARTY)/zlib_1_2_3_vc9/lib/libzlib_vc9_1_2_3.lib
可以除了kml本身的库外,还要添加minizip库、expat库、boost库和zip库。
这样就能将kml库集成进来。还有一点需要注意的是,我发现kml并不支持传统的shp文件的矢量类型,因此要重载kml图层类的GetGeomType接口(将kml类型转换为传统的shp的矢量类型),这个接口的实现代码如下:
OGRwkbGeometryType OGRLIBKMLLayer::GetGeomType() { OGRwkbGeometryType eType = wkbUnknown; OGRFeature* pFeature = NULL; ResetReading(); pFeature = GetNextFeature(); if (pFeature != NULL) { OGRGeometry* pGeometry = pFeature->GetGeometryRef(); if (pGeometry != NULL) { eType = pGeometry->getGeometryType(); } } m_poOgrFeatureDefn->SetGeomType(eType); return eType; }