LIBKML驱动程序(.kml.kmz)

司机简称

LIBKML

生成依赖项

libkml语言

LIBKML驱动程序是 Libkml ,的参考实现 KML 阅读和写作,以跨平台C++的形式出现。必须生成并安装Libkml才能使用此OGR驱动程序。注意:您需要构建libkml 1.3或master。

注意,如果您构建并包含这个LIBKML驱动程序,它将成为ogr的KML的默认读取器,覆盖前面的 KML driver . 仍然可以通过命令行指定KML或LIBKML作为输出驱动程序

来自Google的Libkml为任何有效的KML文件提供读取服务。但是,请注意,一些KML设施并没有映射到OGR用作其内部结构的简单功能规范中。因此,驱动程序将尽最大努力理解libkml读入ogr的KML文件的内容,但是您的里程数可能会有所不同。请尝试一些KML文件作为示例,以了解理解的内容。特别是,多个深度的特征集嵌套将被展平以支持ogr的内部格式。

驱动程序功能

Supports Create()

This driver supports the GDALDriver::Create() operation

Supports Georeferencing

This driver supports georeferencing

Supports VirtualIO

This driver supports virtual I/O operations (/vsimem/, etc.)

数据来源

可以将数据源指定为kml文件 somefile.kml ,一个目录 somedir/ ,或kmz文件 somefile.kmz .

By default on directory and kmz datasources, an index file of all the layers will be read from or written to doc.kml. It contains a <NetworkLink> to each layer file in the datasource. This feature can be turned off by setting the configuration option LIBKML_USE_DOC.KML to "no".

StyleTable

数据源样式表被写入 <Document> 在.kml、kmz文件中的style/style.kml或目录中的style.kml中,作为一个或多个 <Style> 元素。不是全部 特征样式规范 可以转换成KML。

数据源创建选项

以下数据源创建选项可用于生成 <atom:Author> 顶级文档级别的元素。

  • AUTHOR_NAME

  • AUTHOR_URI

  • AUTHOR_EMAIL

一个 <atom:link> 顶级文档级别的元素可以用 LINK 创建选项。

这个 <phoneNumber> 顶级文档级别的元素可以用 PHONENUMBER 创建选项。该值必须遵循以下语法 IETF RFC 3966 .

从GDAL 2.2开始,DOCUMENT_ID datasource creation选项可用于指定根<DOCUMENT>节点的ID。默认值为根文档。

容器属性

以下数据集创建选项可用于设置容器选项:

列表样式

以下数据集创建选项可用于通过 <ListStyle> 元素:

  • LISTSTYLE_TYPE :可以是“check”、“radioFolder”、“checkOffOnly”或“checkHideChildren”之一。设置 <listItemType> 元素。

  • LISTSTYLE_ICON_HREF :要为主文件夹显示的图标的URL。设置 <ItemIcon> 元素。

气球样式

如果一种风格 foo 如果定义了,则可以添加 <BalloonStyle> 元素,通过指定 foo_BALLOONSTYLE_BGCOLOR 和/或 foo_BALLOONSTYLE_TEXT 元素。

NetworkLinkControl

A <NetworkLinkControl> 如果至少指定了以下数据集创建选项之一,则可以定义元素:

更新文档

定义数据集创建选项时 UPDATE_TARGETHREF ,NetworkLinkControl KML文件 <Update> 将生成元素。见 tutorial about update .

层上的CreateFeature()操作将转换为 <Create> 元素。

层上的SetFeature()操作将转换为 <Change> 元素。

层上的DeleteFeature()操作将转换为 <Delete> 元素。

OGRLayer 映射到kml文件作为 <Document><Folder> ,并在kmz文件或目录中作为单独的kml文件。

风格

不能从作为 <Folder> ,否则它们在 <Document> 这就是层。

图式

读写 <Schema> .kml文件、.kmz文件和目录支持。

图层创建选项

以下图层创建选项可用于生成 <LookAt> 层级别的元素。

  • LOOKAT_LONGITUDE (需要)

  • LOOKAT_LATITUDE (需要)

  • LOOKAT_RANGE (需要)

  • LOOKAT_HEADING

  • LOOKAT_TILT

  • LOOKAT_ALTITUDE

  • LOOKAT_ALTITUDEMODE

或者,a <Camera> 可以生成元素。

  • CAMERA_LONGITUDE (需要)

  • CAMERA_LATITUDE (需要)

  • CAMERA_ALTITUDE (需要)

  • CAMERA_ALTITUDEMODE (需要)

  • CAMERA_HEADING

  • CAMERA_TILT

  • CAMERA_ROLL

A <Region> 可以生成元素来控制层的对象是否可见。如果REGION_XMIN、REGION_YMIN、REGION_XMAX和REGION_YMAX,则区域坐标由写入层中的特征的空间范围确定。

  • ADD_REGION=YES/NO :默认为否

  • REGION_XMIN (可选):定义区域的西坐标。

  • REGION_YMIN (可选):定义区域的南坐标。

  • REGION_XMAX (可选):定义区域的东坐标。

  • REGION_YMAX (可选):定义区域的北坐标。

  • REGION_MIN_LOD_PIXELS (可选):显示区域的最小像素大小。默认为256。

  • REGION_MAX_LOD_PIXELS (可选):显示区域的最大像素大小。默认为-1(无限)。

  • REGION_MIN_FADE_EXTENT (可选):几何体淡出的距离,从完全不透明到完全透明。默认为0。

  • REGION_MAX_FADE_EXTENT (可选):几何体淡出的距离,从完全透明到完全不透明。默认为0。

A <ScreenOverlay> 元素可以添加以显示徽标、图例等。。。

  • SO_HREF (必需):要显示的图像的URL。

  • SO_NAME (可选)

  • SO_DESCRIPTION (可选)

  • SO_OVERLAY_X (可选)

  • SO_OVERLAY_Y (可选)

  • SO_OVERLAY_XUNITS (可选)

  • SO_OVERLAY_YUNITS (可选)

  • SO_SCREEN_X (可选)。默认为0.05

  • SO_SCREEN_Y (可选)。默认为0.05

  • SO_SCREEN_XUNITS (可选)。默认为分数

  • SO_SCREEN_YUNITS (可选)。默认为分数

  • SO_SIZE_X (可选)

  • SO_SIZE_Y (可选)

  • SO_SIZE_XUNITS (可选)

  • SO_SIZE_YUNITS (可选)

默认情况下,层写入为 <Document> 元素。通过设置 FOLDER 图层创建选项为“是”,也可以将其写为 <Folder> 元素(仅在.kml文件中)。

以下图层创建选项可用于设置容器选项:

以下图层创建选项可用于通过 <ListStyle> 元素:

  • LISTSTYLE_TYPE :可以是“check”、“radioFolder”、“checkOffOnly”或“checkHideChildren”之一。设置 <listItemType> 元素。

  • LISTSTYLE_ICON_HREF :要为层文件夹显示的图标的URL。设置 <ItemIcon> 元素。

特征

OGRFeature 通常将kml翻译为 <Placemark> ,反之亦然。

如果定义了模型字段,则 <Model> 将生成Placemark中的对象。

如果定义了networklink字段,则 <NetworkLink> 将生成。其他networklink字段是可选的。

如果定义了photooverlay字段,则 <PhotoOverlay> 将生成(前提是还设置了摄像机经度、摄像机纬度、摄像机高度、摄像机高度模式、头部和/或倾斜和/或滚动、左视野、右视野、底部视野、顶部视野、近场)。形状字段是可选的。

如果PhotoOverlay是一个大图像,强烈建议平铺它并生成概述级别,如 PhotoOverlay tutorial . 在这种情况下,URL应该包含“$ [水平] ", "$ [x] “和”$ [y] “应设置photooverlay字段中的子字符串,以及imagepyramid_tilesize、imagepyramid_maxwidth、imagepyramid_maxheight和imagepyramid_gridorigin字段。

如果定义了相机经度、相机纬度、相机高度、相机高度模式、头部和/或倾斜和/或滚动字段,则Placemark、Model、NetworkLink和PhotoOverlay对象可以具有关联的相机。

KML <GroundOverlay> elements are supported for reading (unless the LIBKML_READ_GROUND_OVERLAY configuration option is set to FALSE). For such elements, there are icon and drawOrder fields.

风格

要素级别的样式字符串映射到KML,作为 <Style><StyleUrl> 在每个 <Placemark> .

When reading a kml feature and the configuration option LIBKML_RESOLVE_STYLE is set to yes, styleurls are looked up in the style tables and the features style string is set to the style from the table. This is to allow reading of shared styles by applications, like mapserver, that do not read style tables.

When reading a kml feature and the configuration option LIBKML_EXTERNAL_STYLE is set to yes, a styleurl that is external to the datasource is read from disk or fetched from the server and parsed into the datasource style table. If the style kml can not be read or LIBKML_EXTERNAL_STYLE is set to no then the styleurl is copied to the style string.

When reading a kml StyleMap the default mapping is set to normal. If you wish to use the highlighted styles set the configuration option LIBKML_STYLEMAP_KEY to "highlight"

在编写kml时,如果存在“astylename_normal”和“astylename_highlight”两种样式(astylename是任何字符串),那么将从这两种样式创建一个名为“astylename”的StyleMap对象。

领域

OGR字段(特征属性)映射到kml <Schema> ;和 <SimpleData> ,但某些特殊字段除外,如下所述。

Note: it is also possible to export fields as <Data> elements if the LIBKML_USE_SCHEMADATA configuration option is set to NO.

A rich set of configuration options are available to define how fields in input and output, map to a KML <Placemark>. For example, if you want a field called 'Cities' to map to the <name>; tag in KML, you can set a configuration option.

  • name: string field that maps to the kml tag <name>. The name of the ogr field can be changed with the configuration option LIBKML_NAME_FIELD .

  • description: string field that maps to the kml tag <description>. The name of the ogr field can be changed with the configuration option LIBKML_DESCRIPTION_FIELD .

  • timestamp: string or datetime or date and/or time field that maps to the kml tag <timestamp>. The name of the ogr field can be changed with the configuration option LIBKML_TIMESTAMP_FIELD .

  • begin: string or datetime or date and/or time field that maps to the kml tag <begin>. The name of the ogr field can be changed with the configuration option LIBKML_BEGIN_FIELD .

  • end: string or datetime or date and/or time field that maps to the kml tag <end>. The name of the ogr field can be changed with the configuration option LIBKML_END_FIELD .

  • altitudeMode: string field that maps to the kml tag <altitudeMode> or <gx:altitudeMode>. The name of the ogr field can be changed with the configuration option LIBKML_ALTITUDEMODE_FIELD .

  • tessellate: integer field that maps to the kml tag <tessellate>. The name of the ogr field can be changed with the configuration option LIBKML_TESSELLATE_FIELD .

  • extrude: integer field that maps to the kml tag <extrude>. The name of the ogr field can be changed with the configuration option LIBKML_EXTRUDE_FIELD .

  • visibility: integer field that maps to the kml tag <visibility>. The name of the ogr field can be changed with the configuration option LIBKML_VISIBILITY_FIELD .

  • icon: string field that maps to the kml tag <icon>. The name of the ogr field can be changed with the configuration option LIBKML_ICON_FIELD .

  • drawOrder: integer field that maps to the kml tag <drawOrder>. The name of the ogr field can be changed with the configuration option LIBKML_DRAWORDER_FIELD .

  • snippet: integer field that maps to the kml tag <snippet>. The name of the ogr field can be changed with the configuration option LIBKML_SNIPPET_FIELD .

  • heading: real field that maps to the kml tag <heading>. The name of the ogr field can be changed with the configuration option LIBKML_HEADING_FIELD. When reading, this field is present only if a Placemark has a Camera with a heading element.

  • tilt: real field that maps to the kml tag <tilt>. The name of the ogr field can be changed with the configuration option LIBKML_TILT_FIELD. When reading, this field is present only if a Placemark has a Camera with a tilt element.

  • roll: real field that maps to the kml tag <roll>. The name of the ogr field can be changed with the configuration option LIBKML_ROLL_FIELD. When reading, this field is present only if a Placemark has a Camera with a roll element.

  • model: string field that can be used to define the URL of a 3D <model>. The name of the ogr field can be changed with the configuration option LIBKML_MODEL_FIELD.

  • scale_x: real field that maps to the x element of the kml tag <scale> for a 3D model. The name of the ogr field can be changed with the configuration option LIBKML_SCALE_X_FIELD.

  • scale_y: real field that maps to the y element of the kml tag <scale>for a 3D model. The name of the ogr field can be changed with the configuration option LIBKML_SCALE_Y_FIELD.

  • scale_z: real field that maps to the z element of the kml tag <scale>for a 3D model. The name of the ogr field can be changed with the configuration option LIBKML_SCALE_Z_FIELD.

  • networklink: string field that maps to the href element of the kml tag <href> of a NetworkLink. The name of the ogr field can be changed with the configuration option LIBKML_NETWORKLINK_FIELD.

  • networklink_refreshvisibility: integer field that maps to kml tag <refreshVisibility> of a NetworkLink. The name of the ogr field can be changed with the configuration option LIBKML_NETWORKLINK_REFRESHVISIBILITY_FIELD.

  • networklink_flytoview: integer field that maps to kml tag <flyToView> of a NetworkLink. The name of the ogr field can be changed with the configuration option LIBKML_NETWORKLINK_FLYTOVIEW_FIELD.

  • networklink_refreshmode: string field that maps to kml tag <refreshMode> of a NetworkLink. The name of the ogr field can be changed with the configuration option LIBKML_NETWORKLINK_REFRESHMODE_FIELD.

  • networklink_refreshinterval: real field that maps to kml tag <refreshInterval> of a NetworkLink. The name of the ogr field can be changed with the configuration option LIBKML_NETWORKLINK_REFRESHINTERVAL_FIELD.

  • networklink_viewrefreshmode: string field that maps to kml tag <viewRefreshMode> of a NetworkLink. The name of the ogr field can be changed with the configuration option LIBKML_NETWORKLINK_VIEWREFRESHMODE_FIELD.

  • networklink_viewrefreshtime: real field that maps to kml tag <viewRefreshTime> of a NetworkLink. The name of the ogr field can be changed with the configuration option LIBKML_NETWORKLINK_VIEWREFRESHTIME_FIELD.

  • networklink_viewboundscale: real field that maps to kml tag <viewBoundScale> of a NetworkLink. The name of the ogr field can be changed with the configuration option LIBKML_NETWORKLINK_VIEWBOUNDSCALE_FIELD.

  • networklink_viewformat: string field that maps to kml tag <viewFormat> of a NetworkLink. The name of the ogr field can be changed with the configuration option LIBKML_NETWORKLINK_VIEWFORMAT_FIELD.

  • networklink_httpquery: string field that maps to kml tag <httpQuery> of a NetworkLink. The name of the ogr field can be changed with the configuration option LIBKML_NETWORKLINK_HTTPQUERY_FIELD.

  • camera_longitude: real field that maps to kml tag <longitude> of a <Camera>. The name of the ogr field can be changed with the configuration option LIBKML_CAMERA_LONGITUDE_FIELD.

  • camera_latitude: real field that maps to kml tag <latitude> of a <Camera>. The name of the ogr field can be changed with the configuration option LIBKML_CAMERA_LATITUDE_FIELD.

  • camera_altitude: real field that maps to kml tag <altitude> of a <Camera>. The name of the ogr field can be changed with the configuration option LIBKML_CAMERA_ALTITUDE_FIELD.

  • camera_altitudemode: real field that maps to kml tag <altitudeMode> of a <Camera>. The name of the ogr field can be changed with the configuration option LIBKML_CAMERA_ALTITUDEMODE_FIELD.

  • photooverlay: string field that maps to the href element of the kml tag <href> of a <PhotoOverlay>. The name of the ogr field can be changed with the configuration option LIBKML_PHOTOOVERLAY_FIELD.

  • leftfov: real field that maps to to kml tag <LeftFov> of a <PhotoOverlay>. The name of the ogr field can be changed with the configuration option LIBKML_LEFTFOV_FIELD.

  • rightfov: real field that maps to to kml tag <RightFov> of a <PhotoOverlay>. The name of the ogr field can be changed with the configuration option LIBKML_RIGHTFOV_FIELD.

  • bottomfov: real field that maps to to kml tag <BottomFov> of a <PhotoOverlay>. The name of the ogr field can be changed with the configuration option LIBKML_BOTTOMTFOV_FIELD.

  • topfov: real field that maps to to kml tag <TopFov> of a <PhotoOverlay>. The name of the ogr field can be changed with the configuration option LIBKML_TOPFOV_FIELD.

  • near: real field that maps to to kml tag <Near> of a <PhotoOverlay>. The name of the ogr field can be changed with the configuration option LIBKML_NEAR_FIELD.

  • shape: string field that maps to to kml tag <shape> of a <PhotoOverlay>. The name of the ogr field can be changed with the configuration option LIBKML_SHAPE_FIELD.

  • imagepyramid_tilesize: integer field that maps to to kml tag <tileSize> of a <ImagePyramid>. The name of the ogr field can be changed with the configuration option LIBKML_IMAGEPYRAMID_TILESIZE.

  • imagepyramid_maxwidth: integer field that maps to to kml tag <maxWidth> of a <ImagePyramid>. The name of the ogr field can be changed with the configuration option LIBKML_IMAGEPYRAMID_MAXWIDTH.

  • imagepyramid_maxheight: integer field that maps to to kml tag <maxHeight> of a <ImagePyramid>. The name of the ogr field can be changed with the configuration option LIBKML_IMAGEPYRAMID_MAXHEIGHT.

  • imagepyramid_gridorigin: string field that maps to to kml tag <gridOrigin> of a <ImagePyramid>. The name of the ogr field can be changed with the configuration option LIBKML_IMAGEPYRAMID_GRIDORIGIN.

  • OGR_STYLE: string field that maps to a features style string, OGR reads this field if there is no style string set on the feature.

几何图形

翻译 OGRGeometry 对于KML来说,几何结构是非常狭窄的,只有几个例外。指向 <Point> (除非找到航向和/或倾斜和/或滚转字段名称,在这种情况下 Camera 对象将被生成),LineString到 <LineString> ,线性连接到 <LinearRing> ,和多边形到 <Polygon> . 在OGR中,多边形包含一个线性阵列,第一个是外圈。KML有标签 <outerBoundaryIs><innerBoundaryIs> 以区别两者。OGR有多种几何类型:GeometryCollection、MultiPolygon、MultiPoint和MultiLineString。如果可能,OGR将尝试映射 <MultiGeometry> 更精确的OGR几何类型(多点、多行或多多边形),在混合内容的情况下默认为GeometryCollection。

Sometimes kml geometry will span the dateline, In applications like qgis or mapserver this will create horizontal lines all the way around the globe. Setting the configuration option LIBKML_WRAPDATELINE to "yes" will cause the libkml driver to split the geometry at the dateline when read.

VSI虚拟文件系统API支持

驱动程序支持读取和写入由VSI虚拟文件系统API管理的文件,这些文件包括“常规”文件以及/vsizip/(读写)、/vsigzip/(读写)、/vsicurl/(只读)域中的文件。

还支持写入/dev/stdout或/vsistdout/。

例子

下面的bash脚本将构建 csv 文件和A vrt 文件,然后使用 ogr2ogr 放入带有时间戳和样式的.kml文件中。

#!/bin/bash
# Copyright (c) 2010, Brian Case
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.


icon="http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png"
rgba33="#FF9900"
rgba70="#FFFF00"
rgba150="#00FF00"
rgba300="#0000FF"
rgba500="#9900FF"
rgba800="#FF0000"

function docsv {

    IFS=','

    while read Date Time Lat Lon Mag Dep
    do
        ts=$(echo $Date | sed 's:/:-:g')T${Time%%.*}Z
        rgba=""

        if [[ $rgba == "" ]] && [[ $Dep -lt 33 ]]
        then
            rgba=$rgba33
        fi

        if [[ $rgba == "" ]] && [[ $Dep -lt 70 ]]
        then
            rgba=$rgba70
        fi

        if [[ $rgba == "" ]] && [[ $Dep -lt 150 ]]
        then
            rgba=$rgba150
        fi

        if [[ $rgba == "" ]] && [[ $Dep -lt 300 ]]
        then
            rgba=$rgba300
        fi

        if [[ $rgba == "" ]] && [[ $Dep -lt 500 ]]
        then
            rgba=$rgba500
        fi

        if [[ $rgba == "" ]]
        then
            rgba=$rgba800
        fi



        style="\"SYMBOL(s:$Mag,id:\"\"$icon\"\",c:$rgba)\""

        echo $Date,$Time,$Lat,$Lon,$Mag,$Dep,$ts,"$style"
    done

}


wget http://neic.usgs.gov/neis/gis/qed.asc -O /dev/stdout |\
 tail -n +2 > qed.asc

echo Date,TimeUTC,Latitude,Longitude,Magnitude,Depth,timestamp,OGR_STYLE > qed.csv

docsv < qed.asc >> qed.csv

cat > qed.vrt << EOF
<OGRVRTDataSource>
    <OGRVRTLayer name="qed">
        <SrcDataSource>qed.csv</SrcDataSource>
        <GeometryType>wkbPoint</GeometryType>
        <LayerSRS>WGS84</LayerSRS>
        <GeometryField encoding="PointFromColumns" x="Longitude" y="Latitude"/>
    </OGRVRTLayer>
</OGRVRTDataSource>

EOF

ogr2ogr -f libkml qed.kml qed.vrt