8

I have a bunch of .jpg photos containing GPS information. What is the easiest way to extract the GPS information and save it into a .gpx file (or .kml, for that matter)?

I have seen this answer pointing to exiftool, which gives a readable output, but I would like to import the track into other applications (e.g. Oruxmaps or Google Maps). Answers to this question on gis.stackexchange.com point to Windows programs, but I am looking for a usable alternative on Mac OSX.

2
  • Would running a Windows tool in a wrapper for OS X be ok or are you looking solely for a native tool?
    – grg
    Aug 27, 2014 at 15:39
  • @GeorgeGarside Sure, a Windows tool in a wrapper would do. If it's easier then hand-crafting a script that uses exiftool and produces xml output.. Aug 27, 2014 at 15:58

2 Answers 2

3

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>"""
2
  • Thanks a lot for this script! I wonder why apparently no app developer jumped into that niche yet, to develop a nice iSomething GUI to extract tracks from photo albums etc... :) Aug 28, 2014 at 11:31
  • Yes, this kind of "join x data to y format" ends up being quite useful. I was able to merge pieces of other existing scripts to make this. Haven't checked the date field of the placemarks, so you might want to double check, if that is important to you. If you want to make a tour, you could probably even add the ability for the images show up as thumbnails along a route, etc.
    – beroe
    Aug 28, 2014 at 17:39
9

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
2
  • I actually use exiftool occasionally, and did not know it had this capability - amazing! Personally, I feel the true value of this is to provide datapoints that may be integrated with an existing GPX trackfile. I've been searching for a replacement for the abusive AllTrails app, and this seems to be a reasonable way to do that.
    – Seamus
    Aug 16, 2020 at 15:01
  • If you are extracting data from an iPhone, you should use this GPX format file instead. gist.github.com/reedriley/6051055
    – jseawright
    Sep 4, 2022 at 19:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .