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
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Linux-Based X Terminals with XDMCP
- 100% disappointed with the decision to go all digital.
- Readers' Choice Awards 2011
- Parallel Programming with NVIDIA CUDA
- You Need A Budget
- Validate an E-Mail Address with PHP, the Right Way
- The Linux powered LAN Gaming House
- The Linux RAID-1, 4, 5 Code
- Python for Android
- Gnome3 is such a POS. No one
5 hours 19 min ago - Gnome 3 is the biggest POS
5 hours 30 min ago - I didn't knew this thing by
11 hours 34 min ago - Author's reply
14 hours 59 min ago - Link to modlys
16 hours 5 min ago - I use YNAB because of the
16 hours 17 min ago - Search
21 hours 20 min ago - Question
21 hours 43 min ago - for the record
21 hours 45 min ago - That's disappointing. Thanks
1 day 9 min 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.