Apple - Extract a GPS track (as .gpx) from a series of jpg photos

exiftool itself can now also generate GPX files. You can download the gpx.fmt template from the exiftool website, then run

exiftool -p ./gpx.fmt *JPG > output.gpx

Some useful options:

  1. -if makes exiftool only process files with a gps tag,
  2. -fileOrder allows processing in a forced order,
  3. -d allows formatting the datatime string.

For example:

exiftool -if '$gpsdatetime' -fileOrder gpsdatetime -p ./gpx.fmt -d %Y-%m-%dT%H:%M:%SZ *JPG > output.gpx

Exiftool is going to be the easiest way to do this.

Here is a script that generates KML output for a list of images. You can modify this if you want a KML path, etc...

#! /usr/bin/env python
# -*- coding: utf-8 -*-

""" 
Create a KML file based on exif data

Requires exiftool to have been installed    
Usage: exif2kml.py *.jpg > output.kml

"""

import os
import sys
import re
import time

def decimalat(DegString):
    # This function requires that the re module is loaded
    # Take a string in the format "34 56.78 N" and return decimal degrees
    SearchStr=r''' *(\d+) deg (\d+)' ([\d\.]+)" (\w)'''
    Result = re.search(SearchStr, DegString)

    # Get the (captured) character groups from the search
    Degrees = float(Result.group(1))
    Minutes = float(Result.group(2))
    Seconds = float(Result.group(3))
    Compass = Result.group(4).upper() # make sure it is capital too

    # Calculate the decimal degrees
    DecimalDegree = Degrees + Minutes/60 + Seconds/(60*60)
    if Compass == 'S' or Compass == 'W':
        DecimalDegree = -DecimalDegree  
    return DecimalDegree

def writePlace(filename,lat,lon,date):
    PlacemarkString = '''
    <Placemark>
     <name>{0}</name>
     <Point>
      <altitudeMode>absolute</altitudeMode>
      <coordinates>{1}, {2}</coordinates>
      <TimeStamp>
        <when>{3}</when>
      </TimeStamp>
     </Point>
    </Placemark>'''.format(filename,lat,lon,date)
    return PlacemarkString 

HeadString='''<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<kml xmlns=\"http://earth.google.com/kml/2.2\">
<Document>'''

if len(sys.argv)<2:
    print >> sys.stderr, __doc__

else:
    placestring = ''
    FList = sys.argv[1:]
    for F in FList:
        ExifData=os.popen('exiftool "'+ F +'" -DateTimeOriginal -GPSLatitude -GPSLongitude').read()
        if "Longitude" in ExifData:
            print >> sys.stderr, F,"\n",ExifData.rstrip()
            Fields = ExifData.split("\n")
            for Items in Fields:
                if len(Items)> 10:
                    K,V = Items.split(" : ")
                    if "Latitude" in K:
                        lat = decimalat(V)
                    elif "Longitude" in K:
                        lon = decimalat(V)
                    elif "Date" in K:
                        date = time.strptime(V.strip(),"%Y:%m:%d %H:%M:%S")  # time format
            if lat:
                TimeFmt = "%Y-%m-%dT%H:%M:%S"
                placestring += writePlace(F,lon,lat,time.strftime(TimeFmt,date))
                lat = ''
    # Generate the output file...
    # This just prints to screen -- use > to capture to file...
    print HeadString
    print placestring
    print """</Document>\n</kml>"""