Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse Geocoding in Bash using GPS Position from exiftool

I am writing a bash script that renames JPG files based on their EXIF tags. My original files are named like this:

IMG_2110.JPG
IMG_2112.JPG
IMG_2113.JPG
IMG_2114.JPG

I need to rename them like this:

2015-06-07_11-21-38_iPhone6Plus_USA-CA-Los_Angeles_IMG_2110.JPG
2015-06-07_11-22-41_iPhone6Plus_USA-CA-Los_Angeles_IMG_2112.JPG
2015-06-13_19-05-10_iPhone6Plus_Morocco-Fez_IMG_2113.JPG
2015-06-13_19-12-55_iPhone6Plus_Morocco-Fez_IMG_2114.JPG

My bash script uses exiftool to parse the EXIF header and rename the files. For those files that do not contain an EXIF create date, I am using the file modification time.

#!/bin/bash
IFS=$'\n'

for i in *.*; do
    MOD=`stat -f %Sm -t %Y-%m-%d_%H-%m-%S $i`
    model=$( exiftool -f -s3 -"Model" "${i}" )
    datetime=$( exiftool -f -s3 -"DateTimeOriginal" "${i}" )
    stamp=${datetime//:/-}"_"${model// /}
    echo ${stamp// /_}$i
done

I am stuck on the location. I need to determine the country and city using the GPS information from the EXIF tag. exiftool provides a field called "GPS Position." Of all the fields, this seems the most useful to determine location.

GPS Position : 40 deg 44' 49.36" N, 73 deg 56' 28.18" W

Google provides a public API for geolocation, but it requires latitude/longitude coordinates in this format:

40.7470444°, -073.9411611°

The API returns quite a bit of information (click the link to see the results):

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.7470444,-073.9411611

My question is:

  1. How do I format the GPS Position to a latitude/longitude value that will provide acceptable input to a service such as Google geolocation?

  2. How do I parse the JSON results to extract just the country and city, in a way that is consistent with many different kinds of locations? Curl, and then? Ideally, I’d like to handle USA locations one way, and non-USA locations, another. USA locations would be formatted USA-STATE-City, whereas non-USA locations would be formatted COUNTRY-City.

I need to do this all in a bash script. I've looked at pygeocoder and gpsbabel but they do not seem to do the trick. There are a few free web tools available but they don't provide an API (http://www.earthpoint.us/Convert.aspx).

like image 737
utt50 Avatar asked Sep 30 '15 20:09

utt50


2 Answers

Better later than never, right.

So, I just came across the same issue and I've managed to make the conversion using the EXIFTool itself. Try this:

exiftool -n -p '$GPSLatitude,$GPSLongitude' image_name.jpg

The converted coordinates are slightly longer than proposed by Google, but the API accepted it fine.

Cheers.

like image 185
Marco Avatar answered Sep 21 '22 04:09

Marco


For #1, the awk should not be that complicated:

awk '/GPS Position/{
  lat=$4; lat+=strtonum($6)/60; lat+=strtonum($7)/3600; if($8!="N,")lat=-lat;
  lon=$9; lon+=strtonum($11)/60; lon+=strtonum($12)/3600; if($13!="E")lon=-lon;
  printf "%.7f %.7f\n",lat,lon
  }'
like image 26
Jeff Y Avatar answered Sep 21 '22 04:09

Jeff Y