如何将超链接放入SimpleKML?

2024-07-08 10:43:21 发布

您现在位置:Python中文网/ 问答频道 /正文

simplekml包给出了这个介绍示例:

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

我想将其扩展如下,以便在描述中添加一个超链接:

^{pr2}$

但是,当我查看生成的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>

根据this page,我应该更像这样(超链接包装在CDATA中):

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

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


Tags: 文件namecomidhttpwwwdescriptionkml
1条回答
网友
1楼 · 发布于 2024-07-08 10:43:21

我找到了这个Google Earth 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字符的行为。所以你可以像这样使用你的原始代码:

^{pr2}$

CDATA标记还有一个特殊行为,它告诉GE不要转义HTML字符。你可以在这里阅读更多信息:https://developers.google.com/kml/documentation/kml_tut

simplekmlClaims to always parse the CDATA tag correctly,因此这可能是更高级链接的一个选项。在

相关问题 更多 >

    热门问题