2

simplekml包提供这个介绍示例:

import simplekml
kml = simplekml.Kml()
kml.newpoint(name="Kirstenbosch", coords=[(18.432314,-33.988862)])  # lon, lat, optional height
kml.save("botanicalgarden.kml")

我想将其扩展如下,以获得描述中的超链接:

import simplekml
kml = simplekml.Kml()
pnt = kml.newpoint(name="Kirstenbosch",
  coords=[(18.432314,-33.988862)],
  description='<a href="https://en.wikipedia.org/wiki/Kirstenbosch_National_Botanical_Garden">Please go here</a>')
kml.save("botanicalgarden.kml")

但是,当我查看生成的 KML 文件时,超链接已转换为文本:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document id="feat_7">
        <Placemark id="feat_8">
            <name>Kirstenbosch</name>
            <description>&lt;a href=&quot;https://en.wikipedia.org/wiki/Kirstenbosch_National_Botanical_Garden&quot;&gt;Please go here&lt;/a&gt;</description>
            <Point id="geom_3">
                <coordinates>18.432314,-33.988862,0.0</coordinates>
            </Point>
        </Placemark>
    </Document>
</kml>

根据这个页面,我应该看起来更像这样(用 CDATA 包裹的超链接):

  <description><![CDATA[
    <A href="http://stlab.adobe.com/wiki/images/d/d3/Test.pdf">test link</A>]]></description>

我需要在 simplekml 中做什么才能正确获取 .KML 文件中的超链接?

4

1 回答 1

1

我找到了这个 Google 地球 KML 教程https://developers.google.com/kml/documentation/kml_tut

Google 地球 4.0 具有自动标记功能,可自动将诸如 www.google.com 之类的文本转换为用户可以单击的活动超链接。标记内的文本、标记和元素都自动转换为标准的 HTTP 超链接。您不需要自己添加标签。

所以看起来你应该能够通过只传递不带<a>标签的超链接来获得所需的行为,如下所示:

import simplekml
kml = simplekml.Kml()
pnt = kml.newpoint(name="Kirstenbosch",
  coords=[(18.432314,-33.988862)],
  description='https://en.wikipedia.org/wiki/Kirstenbosch_National_Botanical_Garden')
kml.save("botanicalgarden.kml")

simplekml 还有一个parsetext()函数,它允许您关闭转义 html 字符的行为。所以你可以像这样使用你的原始代码:

import simplekml
kml = simplekml.Kml()
kml.parsetext(parse=False)
pnt = kml.newpoint(name="Kirstenbosch",
  coords=[(18.432314,-33.988862)],
  description='<a href="https://en.wikipedia.org/wiki/Kirstenbosch_National_Botanical_Garden">Please go here</a>')
kml.save("botanicalgarden.kml")

CDATA标签还具有告诉 GE 不要转义 HTML 字符的特殊行为。您可以在此处阅读更多相关信息:https ://developers.google.com/kml/documentation/kml_tut

simplekml声称始终正确解析 CDATA 标记,因此这可能是更高级链接的一个选项。

于 2018-10-19T17:04:47.793 回答