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.

Tech writer covering Linux and open source software

Tagged with: , , , ,
Posted in Open Source, Photography, Software
10 comments on “Coarse Geotagging with Bash and Google Maps
  1. Thanks a lot for this nice little snippet! However you should add installing the tool “jq” to the instructions. I’m currently adopting your script to a KDE-Dolphin Service Menu so that its possible to select some images in the dolphin file manager and set the geotags with rightclick->set geotag.

    • Dmitri Popov says:

      You are absolutely right! I’ve updated the article. Thank you for your feedback, and feel free to share the tweaked version of the script. :-)

      • I created a gist with my version of your snippet here: https://gist.github.com/tuxflo/929e6e1ff2c66a6b3c76d1c0ad256141
        Put the geotag.sh script in /usr/local/bin and the .desktop file at /usr/share/kservice/ServiceMenus/geotag.desktop. Restart Plasma or execute kbuildsycoca5, then select some Images with dolphin, rightclick and select “Set geotag”. A kdialog window will ask for the city. After applying another kdialog should tell you which files were updated.

        Currently the “Cancel” Button is not working and I’m looking for a way to create “bookmarks” for the locations I use most.

      • Dmitri Popov says:

        Great job! Thank you so much for sharing this!

  2. Bill Crooke says:

    Very cool, thank you!

    I would add -sS to the curl command to eliminate the progress output

  3. Bill Crooke says:

    Also if you want a city with a space in the name use %20.

    Like this “hood%20river,or”

  4. tuxflo says:

    I wrote an (german) tutorial about your script and the KDE ServiceMenu at my blog: https://blog.webdnd.de/geotagging-mit-dolphin/ and also included the curl -sS option and the “Cancel” function.

Comments are closed.

Recipes for automated and streamlined photographic workflow on Linux

Practical advice for Tokyo night photography

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

%d bloggers like this: