Posted on 2007-02-14 15:02:30-08 by exiftool
Generating a Google Earth KML file
Here are some simple steps which use ExifTool to generate a KML file for Google Earth from a set of images with embedded GPS information:

First you need a set of 3 files to make up the framework of the KML file. These files are included at the bottom of this post:

1. kml-start.fmt - the header section of the KML file
2. kml-placemark.fmt - the section that is repeated for each image
3. kml-end.fmt - the end section of the KML file

The second of these files contains the necessary ExifTool formatting codes to insert the file name, size and GPS coordinates into the KML file.

The steps to generating a complete KML file are:

a) Copy kml-start.fmt to your output file.

b) Concatenate the output of "exiftool -n -r -q -p kml-placemark.fmt DIR" to the file, where "DIR" is the full directory specification for the images.

c) Concatenate kml-end.fmt to the output file.

d) You can now open the output KML file in Google Earth.

In Windows, this can be automated with a simple batch file to give a drag-and-drop utility to peform these steps:

kml.bat:
@echo off type c:\windows\kml-start.fmt > c:\out.kml exiftool -n -r -q -p c:\windows\kml-placemark.fmt %* >> c:\out.kml type c:\windows\kml-end.fmt >> c:\out.kml echo done. pause

The above commands assume that the KML format files are in the c:\windows directory and that exiftool is somewhere in your PATH (ie. also in c:\windows).

You should be able to drag and drop geocoded images and folders containing images onto this batch file to generate the output file c:\out.kml, which can be opened in Google Earth.

In Unix (and Mac OS X), the commands are slightly different:

cat kml-start.fmt > out.kml exiftool -n -r -q -p kml-placemark.fmt DIR >> out.kml cat kml-end.fmt >> out.kml

(assuming the KML format files are in the current directory)

Here are the 3 KML format files:

1. kml-start.fmt:
<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.0"> <Document> <name>My Photos</name> <open>1</open> <Style id="Photo"> <geomScale>.75</geomScale> <IconStyle> <color>ffffffff</color> <Icon> <href>root://icons/palette-4.png</href> <x>192</x> <y>96</y> <w>32</w> <h>32</h> </Icon> </IconStyle> </Style> <Style id="View"> <IconStyle> <color>ffffffff</color> <Icon> <href>root://icons/palette-3.png</href> <x>160</x> <y>128</y> <w>32</w> <h>32</h> </Icon> </IconStyle> </Style> <Folder> <name>Waypoints</name> <open>0</open>

2. kml-placemark.fmt:
<Placemark> <description><![CDATA[<br/><table><tr><td> <img src='$directory\$filename' width='$imagewidth' height='$imageheight'> </td></tr></table>]]></description> <Snippet/> <name>$filename</name> <styleUrl>#Photo</styleUrl> <Point> <altitudeMode>clampedToGround</altitudeMode> <coordinates>$gpslongitude,$gpslatitude,0</coordinates> </Point> </Placemark>

3. kml-end.fmt:
</Folder> </Document> </kml>

- Phil
Direct Responses: 11154 | Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.