Scribbles and Snaps

Coarse Geotagging with Bash and Google Maps

If all you want is to geotag photos with coordinates of the city where they were taken, a simple Bash shell script is all your need.

#!/usr/bin/env bash

wget -q --spider http://maps.googleapis.com/
if [ $? -ne 0 ]; then
echo "Google Maps is not reachable. Check your Internet connection."
exit 1
fi

if [ -z "$1" ]; then
echo "Please specify a city"
exit 1
fi

lat=$(curl -G -k --data "address=$1&sensor=false" http://maps.googleapis.com/maps/api/geocode/json | jq '.results[0].geometry.location.lat')
if (( $(echo "$lat > 0" |bc -l) )); then
latref="N"
else
latref="S"
fi

lon=$(curl -G -k --data "address=$1&sensor=false" http://maps.googleapis.com/maps/api/geocode/json | jq '.results[0].geometry.location.lng')
if (( $(echo "$lon > 0" |bc -l) )); then
lonref="E"
else
lonref="W"
fi
exiftool -overwrite_original -GPSLatitude=$lat -GPSLatitudeRef=$latref -GPSLongitude=$lon -GPSLongitudeRef=$lonref .

The script  obtains geographical coordinates of the specified city via Google Maps API, and then uses the ExifTool to write the obtained latitude and longitude values into photos in the current directory.

To deploy  the script, install the required packages first. To do this on openSUSE, use the following command:

sudo zypper in exiftool ImageMagick jq bc

Copy then the code, paste it into a new text file, and save the file under the geotag name. Run the commands below to install the script:

sudo cp geophotobash-simple.sh /local/bin/geotag
sudo chown root:root /local/bin/geotag
sudo sudo chmod 755 /local/bin/geotag

To geotag photos, switch to the directory in which they are stored, then run the geotag tokyo command (replace tokyo with the name of the city where the photos were taken). That’s all there is to it.

This is an excerpt from the Linux Photography book. Get your copy from Google Play Store or Gumroad.