如何将超链接放入 SimpleKML?
How do I put a hyperlink into SimpleKML?
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,所以这可能是更高级链接的一个选项。
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,所以这可能是更高级链接的一个选项。