D. Cooper Stevenson
September 16, 2010 0

Sattelite Photos with Tagged Image Locations

By in Programming

Have a GPS and a digital camera and would like to have a map depicting where you took your images? No problem.

Here are two scripts I wrote (one in Shell, the other in Ruby) that grabs a satellite photo based on your GPS’s data and places a point that corresponds where you took a picture. This script synchronizes your track log and your images’ header information to pull up a photo showing where you took each image. Here’s a sample:

The scripts require GPS drive and geo-map.

Main Script

#! /bin/bash

#date string as part of base directory
date_string=$(date | awk ‘{ print $2, $3, $6}’ | sed ‘s/ /./g’)

#where all the magic happens
base_directory=/home/cstevens/doc/inf_paper/
gps_dir=${base_directory}/gps/
bin_directory=${base_directory}bin/

#GPS related varibles
interactor_gps_dir=${base_directory}${date_string}/gps/
#user’s track file output
#interactor_gps_tracklog=${interactor_gps_dir}${date_string}.way
interactor_gps_tracklog=/home/cstevens/doc/inf_paper/08.09.06/gps/08.09.06.gpx
interactor_gps_file=${interactor_gps_dir}gps_photo.txt
interactor_image_directory=${base_directory}${date_string}/images/
#directory for gpsphoto pearl script
sys_gis_dir=${base_directory}gps/
gpsphoto_dir=${sys_gis_dir}gpsphoto/
gpsphoto_script=${gpsphoto_dir}gpsPhoto.pl
gps_dev=/dev/ttyS0
gps_input_format=garmin
gps_track_ouptut_file=${interactor_gps_dir}${gps_track_file}
sys_gis_dir=${base_directory}gps/
exif_credit_string=”Cooper Stevenson”
#A bit of hackery here. This script and the others’ base destination directory
#are synch’d by abbrv. month name ‘dot’ day ‘dot’ year Example: Aug.10.2006
#all subdirs rely on this base

image_mount_dir=${base_directory}08.09.06/images/

#GPS/Image syncronized coordinate file
syncd_coord_file=${interactor_gps_directory}gps_coordinates.txt

#Lay down base directories and header
/usr/bin/ruby ${bin_directory}process_inf_data.rb

#copy the camera images and movies
cp ${image_mount_dir}*.jpg ${interactor_image_directory}
cp ${image_mount_dir}*.mov ${interactor_image_directory}

#Get gps track log
gpsbabel -t -i ${gps_input_format} -f ${gps_dev} -o gpx -F ${interactor_gps_tracklog}

# geotag files–this updates the camera image’s EXIF data. This may be useful
# someday but mostly we use it to link up the track data from gps with the
# cooresponding image. The output is used to build the gpx file
# Note that the gpsphoto app is modified slightly to render single line output.
# line 434 of gpsphoto.pl should read as follows:
# print “$file, $createDate, timediff=$mintimediff, “;

#build gpx header file
echo ‘<?xml version=”1.0″?>’ > ${interactor_gps_file}
echo ‘<gpx version=”1.0″>’ >> ${interactor_gps_file}

# run gpsphoto and parse the output to synchronized coordinate/image file

/usr/bin/perl ${gpsphoto_script} –dir ${interactor_image_directory} –maxtimediff 60 –gpsfile ${interactor_gps_tracklog} –credit ${exif_credit_string} | grep timediff | awk -F’,’ ‘{print $1, $2, $4, $5, $6}’ | while read image_filename date time lat lon elev
do
echo “${image_filename}”, “${time}”, “${lat}”, “${lon}”, “${elev}”
echo “<time>${time}</time>” >> ${interactor_gps_file}
echo “<wpt lat=\”${lat}\” lon=\”${lon}\”> ” >> ${interactor_gps_file}
echo “<cmt>Label</cmt>” >> ${interactor_gps_file}
echo ‘label?’
label=read input
echo “<cmt>${label}</cmt>” >> ${interactor_gps_file}
echo “<sym>Waypoint</sym>” >> ${interactor_gps_file}
echo “</wpt>” >> ${interactor_gps_file}
done
echo “</gpx>” >> ${interactor_gps_file}
run geomap to go out to server, pull map, and bring home image…
${sys_gis_dir}./geo-map -a 4 -s0 -W 800 -H 600 -t ${interactor_gps_file}

Image Header Processing Script

cat process_inf_data.txt
#! /usr/bin/ruby -w

# base directory
base_directory = “/home/cstevens/doc/inf_paper/”
#Header source file name
header_file = “header.txt”

#image directory
image_directory_string = “images/”

gps_directory_string = “gps/”

#current date
time = Time.now
date_string = time.strftime(“%b”) + ‘.’ + time.strftime(“%d”) + ‘.’ + time.strftime(“%Y”)

def create_directories(base_directory, date_string, image_directory_string, header_file, gps_directory_string)

#create a directory for today
Dir.mkdir(base_directory + date_string)

#create images directory
Dir.mkdir(base_directory + date_string + “/” + image_directory_string)

#create a gps directory
Dir.mkdir(base_directory + date_string + “/” + gps_directory_string)
end

def print_header(base_directory, date_string, header_file)
#open the header file
lines = header_file_open = File.open(base_directory + ‘inform’ + ‘/’ + header_file)

# create a file to append data found from the input file
inf_file = File.open(base_directory + date_string + “/” + “test.txt”, “w+”)
#print the entire line
lines.each { | line |
print line
inf_file.write(line)
}
end

#create base directory structure
create_directories(base_directory, date_string, image_directory_string, header_file, gps_directory_string)

#lay down System header template
print_header(base_directory, date_string, header_file)

Leave a Reply