Tech Tip: Add Latitude/Longitude Information to Photos
I wanted to store geolocation information in the photos I take with my digital camera. That way I wouldn't have to specify the photo locations manually when I upload them to the Picasa webpage. Since my camera doesn't have built in GPS support, I wrote this script to add the location information to the pictures when they are on the computer already.
The script can be used together with Dave Taylor's whereis.sh script, described in this artcle. Dave's script takes an address and returns the latitude/longitude values using the Yahoo Maps service. My script - I call it jpgloc.sh - can be piped the output from whereis.sh, for example:
sh whereis.sh 2001 Blake Street, Denver, CO | ./jpgloc.sh
# OR
sh whereis.sh 2001 Blake Street, Denver, CO | sh jpgloc.sh
It takes the two values from the standard input. Converts them to Degrees/Minutes format and also figures out whether it is a North/South latitude and East/West longitude. After that it loops through the JPEGg images in the current directory and sets the GPS Latitude/Longitude values to the provided location. It uses the exiv2 utility for the last step. This program should be available for recent distributions through the package management system. For example on Ubuntu Linux it can be installed with this command:
sudo apt-get install exiv2
If the Latitude/Longitude data is available in some other format, the script can be easily modified/extended to take the same information from command-line arguments. The source for the script follows:
#!/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 # Also try *.JPG
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
| Attachment | Size |
|---|---|
| jpgloc.sh_.txt | 1.19 KB |
Trending Topics
| OpenLDAP Everywhere Reloaded, Part I | May 23, 2012 |
| Chemistry the Gromacs Way | May 21, 2012 |
| Make TV Awesome with Bluecop | May 16, 2012 |
| Hack and / - Password Cracking with GPUs, Part I: the Setup | May 15, 2012 |
| An Introduction to Application Development with Catalyst and Perl | May 14, 2012 |
| Cryptocurrency: Your Total Cost Is 01001010010 | May 09, 2012 |
- OpenLDAP Everywhere Reloaded, Part I
- Strip DRM from WMV File
- Validate an E-Mail Address with PHP, the Right Way
- Boot with GRUB
- Why Python?
- A Statistical Approach to the Spam Problem
- Chapter 16: Ubuntu and Your iPod
- Why Hulu Plus Sucks, and Why You Should Use It Anyway
- Building an Ultra-Low-Power File Server with the Trim-Slice
- Science the GNU Way, Part I
- Editorial Standards?
4 hours 5 min ago - Great one
5 hours 40 min ago - Common form in many
6 hours 1 min ago - Awsome
11 hours 4 min ago - Euro 2012 Coupon Codes - Get 20% Off Pavtube TiVo Converter
3 days 9 hours ago - Euro 2012 Big Sale: 20% Off Instant Savings on TiVo Converter
3 days 9 hours ago - MakeMKV works as well, though
3 days 9 hours ago - Euro 2012 Big Sale: 20% Off Instant Savings on TiVo Converter
3 days 10 hours ago - Awesome
4 days 8 hours ago - Who worries approx the
4 days 10 hours ago





Comments
Great Tip/Timesaver
Thank you for this excellent little tip. I'm currently traveling around the world and have been taking a lot of pictures. Since my camera isn't GPS enabled and I do not have a GPS tracker, I've been wanting to batch geocode them based on my general location. Your script did the trick. I'm a complete n00b running Ubuntu (Easy Peasy) on a slow Asus EeePC 900, so being able to do this in the command line has been a huge time saver.
Note to Flickr users: Make sure to enable the import location info in the privacy section of your account settings, that way the pics will automatically show up on your map.
Typo
Couldn get it to work until I typed:
./whereis.sh 2001 Blake Street, Denver, CO | ./jpgloc.shquite useless
Dave Taylor's script is totally useless to obtain location of anything which does not have an address, like mountain road.
it might be much better idea to save gps track, extract the snapshot time from exif and match this time to the gps track to get exact location. also this saves plenty of time writing the address of the place you took your pictures and typing it back to the computer.
Accuracy of Locations using Yahoo or Google Maps
Using Google Maps or Yahoo Maps may be accurate in some places, but in SW Montana, they place my house about a mile and a half off. Not even close by nuclear weapon standards.
Nice idea
Excellent! Sometimes using a GUI to set locations like digiKam http://www.digikam.org/ supports is too much trouble. A shell interface is exactly what the DR ordered. Nice.
Of course, using gpsPhoto.pl http://sourceforge.net/projects/gps2photo/ and gpsbabel http://sourceforge.net/projects/gpsbabel/ is also an option for matching GPS track data into your photos if you saved the GPS track.