Create KML with Embedded Image

2836
3
Jump to solution
11-16-2017 07:22 AM
MitchHolley1
MVP Regular Contributor

I am trying to recreate/customize a tool (found here) which reads metadata from a JPG and creates a KML from it.  The KML points to the relative path of the JPG and the image is displayed in the KML's pop-up window.  

I cannot get the image to show up and was hoping someone had an idea.  The image below shows what the KML looks like in Google Earth. 

Here is the code that produces the KML. 

for directory, sub, files in os.walk(imageDir):
    for f in files:
        fullpath = os.path.join(directory, f)
        relpath = os.path.relpath(fullpath) #I've tried passing both fullpath and relpath to Text below
        
        data = get_exif_data(fullpath)
        c = get_lat_lon(data)
        coordinates = (c[1],c[0])
        
        pnt = kml.newpoint(name=f, coords = [coordinates])
        
        imgHeight = data['ImageLength']/4
        imgWidth = data['ImageWidth']/4
        project = 'Test'
        date = data['DateTime']
        device = 'Apple iPhone'
        
        Text = '<img src="'+relpath+'" alt="path failed" width="{0}" height="{1}"'+\
                'align="left"/>'.format(imgWidth, imgHeight)+\
                '<br>Project Name: {0}'+\
                '<br>Capture Date & Time: {1}<br>Device: {2}'.format(project, date, device)
        
        pnt.style.iconstyle.icon.href ="http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png"
        
kml.save(outKML)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Any ideas as to why this is happening.  I'm 99% sure the path to the image is incorrect, but I'm out of ideas. 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

I always include the script name in any script, then you can play with relative paths etc.

The easiest is store everything in one folder ie script and image

import sys
script = sys.argv[0]
pth = "/".join(script.split('/')[:-1]) + "/test.png"

pth
'C:/GIS/Tools_scripts/Table_tools/Scripts/test.png'

View solution in original post

3 Replies
DanPatterson_Retired
MVP Esteemed Contributor

I always include the script name in any script, then you can play with relative paths etc.

The easiest is store everything in one folder ie script and image

import sys
script = sys.argv[0]
pth = "/".join(script.split('/')[:-1]) + "/test.png"

pth
'C:/GIS/Tools_scripts/Table_tools/Scripts/test.png'
XanderBakker
Esri Esteemed Contributor
MitchHolley1
MVP Regular Contributor

Holy cow!  Dan, that worked!  Thanks so much!  All I did was move the script to the folder where the images are housed.  You're the man.