How to Add Geocode Lat/Lon Information to your Photos

Use this quick script to quickly geo tag or geocode all images in a folder with Latitude and Longitude coordinates.


If you would like to add or edit Geocode information in existing images there are a few programs out there to do this task exactly- but seldom are they cross platform (Windows/Mac/Unix) and most have a cost as well. I have many images shot with an older Canon Digital Camera without GPS (and continue to create new images) as well as older images without any EXIF GPS data. There are some tools out which can do this at the command line via simple scripting for free. Of course, this is no issue for those using smart phones, but for those of us who have older stuff (and images) this can help make the images more easily explored.

So how do you go about adding this Geocode tagging? Well to do it we need two things- first, we need a set of GPS Coordinates in Lat/Lon format, and second, we need to update or add the coordinates to the images’ EXIF data attributes. Before we start, this article is based on an article by Zsolt Hajdo published in Linux Journal, Dec 2009. I have made one minor change to the script and also had to recreate the article (and script) he linked to (in my own format using the MapQuest API instead of the yahoo maps API – which no longer exists) and upon which this script depends to work. Whew. Here is a link to the article.

Here is a link to my article on a Simple Script to get Latitude and Longitude from an address. The script is called geocode.sh and you simply call it with an address and pipe the output to jpegexif.sh – which is what I’ve called the script which adds the geocode tags.

How to Add Geocode Lat/Lon Information to your Photos

Learn how to use these simple scripts to quickly geo tag or geocode all images in a folder with Latitude and Longitude coordinates. Scripts work on Windows, Linux, MacOS. Grab the scripts at: https://www.webgears.com/blog/how-to-add-geocode-lat/lon-information-to-your-photos

Posted by Webgears on Tuesday, June 19, 2018

If you’re on Windows as I am, you need Cygwin installed, additionally you need to be sure you have bc installed (found in the installer under the Math category) and most important you need exiv2 (http://www.exiv2.org) installed as it is the actual tool which does the bulk of the work. You should find it in Cygwin installer too. You’ll also need curl installed. Once you have that sorted, you can type sh geocode.sh 1600 Pennsylvania Ave, Washington DC | sh jpegexif.sh while in the folder which contains the jpgs you wish to tag.

#!/bin/sh
# Grabs latitude and longitude values from stdin and stores # them in all JPGs in current directory.
# The two values have to be separated by a comma.
# IFS=","
read lat long
# If sign is negative, we have to change reference
latRef="N";
latSign=$(echo $lat | cut -c 1)
if [ "X$latSign" = "X-" ]
then # Delete minus from beginning
lat=$(echo $lat | sed s/^-//)
latRef="S"
fi
lonRef="E";
lonSign=$(echo $long | cut -c 1)
if [ "X$lonSign" = "X-" ]
then
long=$(echo $long | sed s/^-//)
lonRef="W"
fi
# Calculate latitude/longitude degrees and minutes
latDeg=$( echo "scale=0; $lat/1" | bc )
latMin=$( echo "scale=2; m=($lat-$latDeg) *100/ 0.016666;scale=0;m/1" | bc )
lonDeg=$( echo "scale=0; $long/1" | bc )
lonMin=$( echo "scale=2; m=($long-$lonDeg)*100/0.016666;scale=0;m/1" | bc )
echo Picture location will be: $latDeg $latMin\' $latRef , $lonDeg $lonMin\' $lonRef
for fil in {*.jpg,*.JPG} # case
do
echo Updating $fil ....
exiv2 -M"set Exif.GPSInfo.GPSLatitude $latDeg/1 $latMin/100 0/1" \
-M"set Exif.GPSInfo.GPSLatitudeRef $latRef" \
-M"set Exif.GPSInfo.GPSLongitude $lonDeg/1 $lonMin/100 0/1" \
-M"set Exif.GPSInfo.GPSLongitudeRef $lonRef" \
$fil
done

and that should be it. Simply drop the files you want to tag into a folder (if they are not already) and run the geocode.sh script with a pipe (|) to jpegexif.sh script – when you are done, you should be able to right click from the file manager and see the lat/lon attributes added to each file.


Comments (0)


Add a Comment

This thread has been closed from taking new comments.