# Album Plugin: captions/exif/gps
# Author      : Andreas K Huettel 

# Description : Adds artifical exif tags for use in exif_caption, e.g. 
#             : gpscaption_gmaps_url or gpscaption_nice.

# Use         : Just use the additional keywords as exif caption tags.
#             : Defined right now:
#             : gpscaption_lat_hem, gpscaption_lat_deg, gpscaption_lat_min
#             : gpscaption_lat_sec, gpscaption_lon_hem, gpscaption_lon_deg
#             : gpscaption_lon_min, gpscaption_lon_sec, gpscaption_nice
#             : gpscaption_gmaps_url
#             : Example:
#             : exif_image "<br><a href='%gpscaption_gmaps_url%'>%gpscaption_nice%</a>"

# License     : This code is licensed under the terms of the LGPL.  If a copy of the license
#             : is not available with this source, it can be viewed at
#             : http://www.gnu.org/licenses/lgpl.html

# $Id: gpscaption.alp 181 2008-10-18 12:32:14Z huettel $


use strict;


sub start_plugin {
	my ( $opt, $plugin, $path ) = @_;

	my $ret = {
		author      => 'Andreas K Huettel',
		href        => 'http://www.akhuettel.de/',
		version     => '1.0',
		description => "Enables ouput of formatted GPS data in captions, e.g. google maps links.

Options: none
",
	};

	album::hook( $opt, 'got_exif_info', \&got_exif_info );

	$ret;
}


my $origlat;
my $origlon;


sub get_lat_deg()
{
  my $deg = $origlat;
  $deg =~ s/^[NS ]*//;
  $deg =~ s/d[0-9ms \.]*//;
  return $deg;
};

sub get_lat_hem()
{
  my $hemi = $origlat;
  $hemi =~ s/[ 0-9dms.]*//g;
  return $hemi;
};

sub get_lon_deg()
{
  my $deg = $origlon;
  $deg =~ s/^[EW ]*//;
  $deg =~ s/d[0-9ms \.]*$//;
  return $deg;
};

sub get_lon_hem()
{
  my $hemi = $origlon;
  $hemi =~ s/[ 0-9dms.]*$//g;
  return $hemi;
};

sub get_lat_min()
{
  my $min = $origlat;
  $min =~ s/^[NS 0-9]*d *//;
  $min =~ s/m[0-9s \.]*$//;
  return $min;
};

sub get_lat_sec()
{
  my $sec = $origlat;
  $sec =~ s/^[NS 0-9d]*m *//;
  $sec =~ s/s$//;
  return $sec;
};

sub get_lon_min()
{
  my $min = $origlon;
  $min =~ s/^[EW 0-9]*d *//;
  $min =~ s/m[0-9s \.]*$//;
  return $min;
};

sub get_lon_sec()
{
  my $sec = $origlon;
  $sec =~ s/^[EW 0-9d]*m *//;
  $sec =~ s/s$//;
  return $sec;
};





sub get_lat_number()
{
 my $number=abs(get_lat_deg())+get_lat_min()/60+get_lat_sec/60/60;
 if (get_lat_hem() eq "S") {$number=-$number; };
 return $number;
};


sub get_lon_number()
{
 my $number=abs(get_lon_deg())+get_lon_min()/60+get_lon_sec/60/60;
 if (get_lon_hem() eq "W") {$number=-$number; };
 return $number;
};



sub get_gmaps_url()
{
  return "http://maps.google.com/maps?q=".get_lat_number().",+".get_lon_number();
};



sub get_nicepos()
{
  my $val = get_lat_deg()."&deg; ".get_lat_min()."' ".get_lat_sec()."\" ".get_lat_hem().", ";
  $val = $val.get_lon_deg()."&deg; ".get_lon_min()."' ".get_lon_sec()."\" ".get_lon_hem();
  return $val;
}






sub got_exif_info {

	my ($opt,$data,$hookname,$pic,$exif) = @_;

	# isolate latitude and logitude from the hash
	$origlat=$exif->{"GPS Latitude"};
	$origlon=$exif->{"GPS Longitude"};

	if (! $origlat) { return $exif; };
	  
	my %gpsexif = %{$exif};

	$gpsexif{"gpscaption_lat_hem"}=get_lat_hem();
	$gpsexif{"gpscaption_lat_deg"}=get_lat_deg();
	$gpsexif{"gpscaption_lat_min"}=get_lat_min();
	$gpsexif{"gpscaption_lat_sec"}=get_lat_sec();
	$gpsexif{"gpscaption_lon_hem"}=get_lon_hem();
	$gpsexif{"gpscaption_lon_deg"}=get_lon_deg();
	$gpsexif{"gpscaption_lon_min"}=get_lon_min();
	$gpsexif{"gpscaption_lon_sec"}=get_lon_sec();
	$gpsexif{"gpscaption_nice"}=get_nicepos();
	$gpsexif{"gpscaption_gmaps_url"}=get_gmaps_url();
	$gpsexif{"gpscaption_version"}='$Id: gpscaption.alp 181 2008-10-18 12:32:14Z huettel $';

	return \%gpsexif;

}





# Plugins always end with:
1;
