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文件中?
答案 0 :(得分:1)
我找到了此Google Earth KML教程https://developers.google.com/kml/documentation/kml_tut:
Google Earth 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 Claims to always parse the CDATA tag correctly,因此这可能是更高级的链接的一种选择。