Geotagging with Exif Metadata

Recently I was coding a prototype using the Google Maps API to geotag objects and then search by location. Geospatial metadata would be imported automatically when users uploaded files. Alternately, items could be manually geotagged by resolving an address or place name (using the Geocoding API) and then adjusting the resulting coordinates on the map.

For my demo I wanted to use photographs with geospatial metadata baked in. Since I’m still a few months away from owning a (GPS enabled) smartphone, I had to find a way to manually set the latitude and longitude after taking some photos. First, to get the coordinates, I used a javascript bookmarklet to alert the current centred latitude and longitude in Google Maps. I aliased this as “geo“:

javascript:var c=window.gApplication.getMap().getCenter();alert(c.lat()+' '+c.lng())

A quick glance at the man page of exiv2 showed that Exif uses triplets of rational numbers for degrees, minutes, and seconds along with cardinal directions instead of simple decimal numbers. So for example, “43.54861810, -80.26079178″ becomes 43/1 32/1 55/1 N and 80/1 15/1 39/1 W (denominator is always 1). I wrote a python script “geo.py” to do the conversion and invoke exiv2:

#!/usr/bin/python
import sys, os
lat = float(sys.argv[1])
lng = float(sys.argv[2])
path = sys.argv[3]

def format(dec):
  dec = abs(dec)
  d = int(dec)
  m = int(dec*60%60)
  s = int(dec*3600%60)
  return "%02d/1 %02d/1 %02d/1" % (d, m, s)

def lat_cardinal(dec):
  return 'N' if dec >= 0 else 'S'

def lng_cardinal(dec):
  return 'E' if dec >= 0 else 'W'

os.system( "exiv2 \
  -M\"set Exif.GPSInfo.GPSLatitude %s\" \
  -M\"set Exif.GPSInfo.GPSLatitudeRef %s\" \
  -M\"set Exif.GPSInfo.GPSLongitude %s\" \
  -M\"set Exif.GPSInfo.GPSLongitudeRef %s\" \
  %s" % (format(lat), lat_cardinal(lat), format(lng), lng_cardinal(lng), path)
)

Now all I have to do is find the location in Google Maps, type ‘geo‘ to get the coordinates, and run geo.py latitude longitude file.jpg!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>