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><a href="https://en.wikipedia.org/wiki/Kirstenbosch_National_Botanical_Garden">Please go here</a></description>
<Point id="geom_3">
<coordinates>18.432314,-33.988862,0.0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
根据 this page ,我应该看起来更像这样(包含在 CDATA 中的超链接):
<description><![CDATA[
<A href="http://stlab.adobe.com/wiki/images/d/d3/Test.pdf">test link</A>]]></description>
我需要在 simplekml 中做什么才能正确获取 .KML 文件中的超链接?
请您参考如下方法:
我找到了这个 Google 地球 KML 教程 https://developers.google.com/kml/documentation/kml_tut :
Google Earth 4.0 has an auto-markup feature that automatically converts text such as www.google.com into active hyperlinks that the user can click. Text inside the tag, the tag, and the element of are all automatically transformed into standard HTTP hyperlinks. You don't need to add the tags yourself.
所以看起来您应该能够通过不带 <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 Claims to always parse the CDATA tag correctly ,因此这可能是更高级链接的一个选项。
