pythonhyperlinksimplekml

How do I put a hyperlink into SimpleKML?


The simplekml package gives this intro example:

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

I would like to extend it as follows, to get a hyperlink into the description:

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")

However, when I look at the resulting KML file, the hyperlink has been converted into text:

<?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>

According to this page, I should be something that looks more like this (hyperlink wrapped in CDATA):

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

What do I need to do in simplekml to get my hyperlink in the .KML file correctly?


Solution

  • I found this Google Earth KML tutorial 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.

    So it looks like you should be able to get the desired behavior by just passing the hyperlink without the <a> tag, like this:

    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 also has a parsetext() function which allows you to turn off the behavior that escapes html characters. So you could use your original code like so:

    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")
    

    The CDATA tag also has special behavior that tells GE not to escape HTML chars. You can read more about it here: https://developers.google.com/kml/documentation/kml_tut

    simplekml Claims to always parse the CDATA tag correctly, so this could be an option for more advanced linking.