Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying local image in simplekml

I'm trying to use simplekml to put a bunch of geotagged photographs into a KML file (well, a KMZ file actually) to view in Google Earth. I've gotten the locations to display, however when I try to put the image in the "description" so when I click on the locations the image comes up, it doesn't work. There is just a blank image. I'm trying to do it by using the addfile() command shown here. My code looks like this:

import os, simplekml

path = r'C:\Users\as\Desktop\testpics'                     
    
kml = simplekml.Kml()

for (dirpath, dirnames, filenames) in os.walk(path):
    for filename in filenames:
        fullpath = os.path.join(dirpath, filename)
        try:
            Lat, Long, Alt = GetLatLong(fullpath) #Didn't include this function, but it appears to work
        except:
            Lat, Long, Alt = (None, None, None)
        if Lat: #Only adds to kml if it has Lat value.
            x, y = (FormatLatLong(Lat), FormatLatLong(Long)) #puts into decimal coords
            point = kml.newpoint(name = filename , coords = [(y,x)])
            picpath = kml.addfile(fullpath)
            point.description = '<img src="' + picpath +'" alt="picture" width="400" height="300" align="left" />'

            

kml.savekmz("kmltest2.kmz", format = False)

As you can see, I've pretty much cut and paste the instructions for using "addfile" from the instructions on the page above. The point.description line seems to be where things are going wrong.

The pictures are getting added to the kmz archive, but they're not showing up in the location bubbles. I thought it might be because I'm doing this on Windows 7 and the slashes are backwards, but I tried manually changing the files\image.jpg to files/image.jpg and it didn't fix it. The produced KMZ doc.kml file looks like this:

    <kml xmlns="http://www.opengis.net/kml/2.2"xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document id="feat_1">
    <Placemark id="feat_2">
    <name>DSC00001.JPG</name>
    <description>&lt;img src="files/DSC00001.JPG" alt="picture" width="400" height="300" align="left" /&gt;</description>
    <Point id="geom_0"><coordinates>18.9431816667,9.44355222222,0.0</coordinates>
    </Point></Document></kml>

(I've removed all but one of the points) Thanks a lot, Alex

like image 412
Alex S Avatar asked Nov 03 '22 02:11

Alex S


1 Answers

May be it's because of unclosed placemark tag in the kml file which you had written. So close the placemark tag after point tag closing.

<kml xmlns="http://www.opengis.net/kml/2.2"xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document id="feat_1">
    <Placemark id="feat_2">
    <name>DSC00001.JPG</name>
    <description>&lt;img src="files/DSC00001.JPG" alt="picture" width="400" height="300" align="left" /&gt;</description>
    <Point id="geom_0"><coordinates>18.9431816667,9.44355222222,0.0</coordinates>
    </Point></Placemark></Document></kml>

If the above code after placing place mark tag doesn't work then try with Balloon Style instead of description tag try with below code

<kml xmlns="http://www.opengis.net/kml/2.2"
     xmlns:gx="http://www.google.com/kml/ext/2.2" 
     xmlns:kml="http://www.opengis.net/kml/2.2" 
     xmlns:atom="http://www.w3.org/2005/Atom"
>
<Document id="feat_1">
<Placemark id="feat_2">
<name>DSC00001.JPG</name>
<Style>
<BalloonStyle>
<text><![CDATA[
 <table width=100% cellpadding=0 cellspacing=0>
  <tr><td><img width=100% src='files/DSC00001.jpg' /></td></tr></table>]]>
</text>
</BalloonStyle>
</Style> 
<Point id="geom_0">
<coordinates>18.9431816667,9.44355222222</coordinates>
</Point>
</Placemark>
</Document>
</kml>
like image 116
565 Avatar answered Nov 13 '22 18:11

565