<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#------------------------------------------------------------------------------
# File:         GPS.pm
#
# Description:  EXIF GPS meta information tags
#
# Revisions:    12/09/2003  - P. Harvey Created
#------------------------------------------------------------------------------

package Image::ExifTool::GPS;

use strict;
use vars qw($VERSION);
use Image::ExifTool::Exif;

$VERSION = '1.34';

my %coordConv = (
    ValueConv    =&gt; 'Image::ExifTool::GPS::ToDegrees($val)',
    ValueConvInv =&gt; 'Image::ExifTool::GPS::ToDMS($self, $val)',
    PrintConv    =&gt; 'Image::ExifTool::GPS::ToDMS($self, $val, 1)',
    PrintConvInv =&gt; 'Image::ExifTool::GPS::ToDegrees($val)',
);

%Image::ExifTool::GPS::Main = (
    GROUPS =&gt; { 0 =&gt; 'EXIF', 1 =&gt; 'GPS', 2 =&gt; 'Location' },
    WRITE_PROC =&gt; \&amp;Image::ExifTool::Exif::WriteExif,
    CHECK_PROC =&gt; \&amp;Image::ExifTool::Exif::CheckExif,
    WRITABLE =&gt; 1,
    WRITE_GROUP =&gt; 'GPS',
    0x0000 =&gt; {
        Name =&gt; 'GPSVersionID',
        Writable =&gt; 'int8u',
        Mandatory =&gt; 1,
        Count =&gt; 4,
        PrintConv =&gt; '$val =~ tr/ /./; $val',
        PrintConvInv =&gt; '$val =~ tr/./ /; $val',
    },
    0x0001 =&gt; {
        Name =&gt; 'GPSLatitudeRef',
        Writable =&gt; 'string',
        Count =&gt; 2,
        PrintConv =&gt; {
            # extract N/S if written from Composite:GPSLatitude
            # (also allow writing from a signed number)
            OTHER =&gt; sub {
                my ($val, $inv) = @_;
                return undef unless $inv;
                return uc $1 if $val =~ /\b([NS])$/i;
                return $1 eq '-' ? 'S' : 'N' if $val =~ /^([-+]?)\d+(\.\d*)?$/;
                return undef;
            },
            N =&gt; 'North',
            S =&gt; 'South',
        },
    },
    0x0002 =&gt; {
        Name =&gt; 'GPSLatitude',
        Writable =&gt; 'rational64u',
        Count =&gt; 3,
        %coordConv,
    },
    0x0003 =&gt; {
        Name =&gt; 'GPSLongitudeRef',
        Writable =&gt; 'string',
        Count =&gt; 2,
        PrintConv =&gt; {
            # extract E/W if written from Composite:GPSLongitude
            # (also allow writing from a signed number)
            OTHER =&gt; sub {
                my ($val, $inv) = @_;
                return undef unless $inv;
                return uc $1 if $val =~ /\b([EW])$/i;
                return $1 eq '-' ? 'W' : 'E' if $val =~ /^([-+]?)\d+(\.\d*)?$/;
                return undef;
            },
            E =&gt; 'East',
            W =&gt; 'West',
        },
    },
    0x0004 =&gt; {
        Name =&gt; 'GPSLongitude',
        Writable =&gt; 'rational64u',
        Count =&gt; 3,
        %coordConv,
    },
    0x0005 =&gt; {
        Name =&gt; 'GPSAltitudeRef',
        Writable =&gt; 'int8u',
        PrintConv =&gt; {
            0 =&gt; 'Above Sea Level',
            1 =&gt; 'Below Sea Level',
        },
    },
    0x0006 =&gt; {
        Name =&gt; 'GPSAltitude',
        Writable =&gt; 'rational64u',
        # extricate unsigned decimal number from string
        ValueConvInv =&gt; '$val=~/((?=\d|\.\d)\d*(?:\.\d*)?)/ ? $1 : undef',
        PrintConv =&gt; '$val =~ /^(inf|undef)$/ ? $val : "$val m"',
        PrintConvInv =&gt; '$val=~s/\s*m$//;$val',
    },
    0x0007 =&gt; {
        Name =&gt; 'GPSTimeStamp',
        Groups =&gt; { 2 =&gt; 'Time' },
        Writable =&gt; 'rational64u',
        Count =&gt; 3,
        Shift =&gt; 'Time',
        Notes =&gt; q{
            when writing, date is stripped off if present, and time is adjusted to UTC
            if it includes a timezone
        },
        ValueConv =&gt; 'Image::ExifTool::GPS::ConvertTimeStamp($val)',
        ValueConvInv =&gt; '$val=~tr/:/ /;$val',
        # pull time out of any format date/time string
        # (converting to UTC if a timezone is given)
        PrintConvInv =&gt; sub {
            my $v = shift;
            my @tz;
            if ($v =~ s/([-+])(.*)//s) {    # remove timezone
                my $s = $1 eq '-' ? 1 : -1; # opposite sign to convert back to UTC
                my $t = $2;
                @tz = ($s*$1, $s*$2) if $t =~ /^(\d{2}):?(\d{2})\s*$/;
            }
            my @a = ($v =~ /((?=\d|\.\d)\d*(?:\.\d*)?)/g);
            push @a, '00' while @a &lt; 3;
            if (@tz) {
                # adjust to UTC
                $a[-2] += $tz[1];
                $a[-3] += $tz[0];
                while ($a[-2] &gt;= 60) { $a[-2] -= 60; ++$a[-3] }
                while ($a[-2] &lt; 0)   { $a[-2] += 60; --$a[-3] }
                $a[-3] = ($a[-3] + 24) % 24;
            }
            return "$a[-3]:$a[-2]:$a[-1]";
        },
    },
    0x0008 =&gt; {
        Name =&gt; 'GPSSatellites',
        Writable =&gt; 'string',
    },
    0x0009 =&gt; {
        Name =&gt; 'GPSStatus',
        Writable =&gt; 'string',
        Count =&gt; 2,
        PrintConv =&gt; {
            A =&gt; 'Measurement Active', # Exif2.2 "Measurement in progress"
            V =&gt; 'Measurement Void',   # Exif2.2 "Measurement Interoperability" (WTF?)
            # (meaning for 'V' taken from status code in NMEA GLL and RMC sentences)
        },
    },
    0x000a =&gt; {
        Name =&gt; 'GPSMeasureMode',
        Writable =&gt; 'string',
        Count =&gt; 2,
        PrintConv =&gt; {
            2 =&gt; '2-Dimensional Measurement',
            3 =&gt; '3-Dimensional Measurement',
        },
    },
    0x000b =&gt; {
        Name =&gt; 'GPSDOP',
        Description =&gt; 'GPS Dilution Of Precision',
        Writable =&gt; 'rational64u',
    },
    0x000c =&gt; {
        Name =&gt; 'GPSSpeedRef',
        Writable =&gt; 'string',
        Count =&gt; 2,
        PrintConv =&gt; {
            K =&gt; 'km/h',
            M =&gt; 'mph',
            N =&gt; 'knots',
        },
    },
    0x000d =&gt; {
        Name =&gt; 'GPSSpeed',
        Writable =&gt; 'rational64u',
    },
    0x000e =&gt; {
        Name =&gt; 'GPSTrackRef',
        Writable =&gt; 'string',
        Count =&gt; 2,
        PrintConv =&gt; {
            M =&gt; 'Magnetic North',
            T =&gt; 'True North',
        },
    },
    0x000f =&gt; {
        Name =&gt; 'GPSTrack',
        Writable =&gt; 'rational64u',
    },
    0x0010 =&gt; {
        Name =&gt; 'GPSImgDirectionRef',
        Writable =&gt; 'string',
        Count =&gt; 2,
        PrintConv =&gt; {
            M =&gt; 'Magnetic North',
            T =&gt; 'True North',
        },
    },
    0x0011 =&gt; {
        Name =&gt; 'GPSImgDirection',
        Writable =&gt; 'rational64u',
    },
    0x0012 =&gt; {
        Name =&gt; 'GPSMapDatum',
        Writable =&gt; 'string',
    },
    0x0013 =&gt; {
        Name =&gt; 'GPSDestLatitudeRef',
        Writable =&gt; 'string',
        Count =&gt; 2,
        PrintConv =&gt; {
            N =&gt; 'North',
            S =&gt; 'South',
        },
    },
    0x0014 =&gt; {
        Name =&gt; 'GPSDestLatitude',
        Writable =&gt; 'rational64u',
        Count =&gt; 3,
        %coordConv,
    },
    0x0015 =&gt; {
        Name =&gt; 'GPSDestLongitudeRef',
        Writable =&gt; 'string',
        Count =&gt; 2,
        PrintConv =&gt; {
            E =&gt; 'East',
            W =&gt; 'West',
        },
    },
    0x0016 =&gt; {
        Name =&gt; 'GPSDestLongitude',
        Writable =&gt; 'rational64u',
        Count =&gt; 3,
        %coordConv,
    },
    0x0017 =&gt; {
        Name =&gt; 'GPSDestBearingRef',
        Writable =&gt; 'string',
        Count =&gt; 2,
        PrintConv =&gt; {
            M =&gt; 'Magnetic North',
            T =&gt; 'True North',
        },
    },
    0x0018 =&gt; {
        Name =&gt; 'GPSDestBearing',
        Writable =&gt; 'rational64u',
    },
    0x0019 =&gt; {
        Name =&gt; 'GPSDestDistanceRef',
        Writable =&gt; 'string',
        Count =&gt; 2,
        PrintConv =&gt; {
            K =&gt; 'Kilometers',
            M =&gt; 'Miles',
            N =&gt; 'Nautical Miles',
        },
    },
    0x001a =&gt; {
        Name =&gt; 'GPSDestDistance',
        Writable =&gt; 'rational64u',
    },
    0x001b =&gt; {
        Name =&gt; 'GPSProcessingMethod',
        Writable =&gt; 'undef',
        Notes =&gt; 'values of "GPS", "CELLID", "WLAN" or "MANUAL" by the EXIF spec.',
        RawConv =&gt; 'Image::ExifTool::Exif::ConvertExifText($self,$val)',
        RawConvInv =&gt; 'Image::ExifTool::Exif::EncodeExifText($self,$val)',
    },
    0x001c =&gt; {
        Name =&gt; 'GPSAreaInformation',
        Writable =&gt; 'undef',
        RawConv =&gt; 'Image::ExifTool::Exif::ConvertExifText($self,$val)',
        RawConvInv =&gt; 'Image::ExifTool::Exif::EncodeExifText($self,$val)',
    },
    0x001d =&gt; {
        Name =&gt; 'GPSDateStamp',
        Groups =&gt; { 2 =&gt; 'Time' },
        Writable =&gt; 'string',
        Notes =&gt; 'YYYY:mm:dd',
        Count =&gt; 11,
        Shift =&gt; 'Time',
        Notes =&gt; q{
            when writing, time is stripped off if present, after adjusting date/time to
            UTC if time includes a timezone
        },
        ValueConv =&gt; 'Image::ExifTool::Exif::ExifDate($val)',
        ValueConvInv =&gt; '$val',
        # pull date out of any format date/time string
        # (and adjust to UTC if this is a full date/time/timezone value)
        PrintConvInv =&gt; q{
            my $secs;
            if ($val =~ /[-+]/ and ($secs = Image::ExifTool::GetUnixTime($val, 1))) {
                $val = Image::ExifTool::ConvertUnixTime($secs);
            }
            return $val =~ /(\d{4}).*?(\d{2}).*?(\d{2})/ ? "$1:$2:$3" : undef;
        },
    },
    0x001e =&gt; {
        Name =&gt; 'GPSDifferential',
        Writable =&gt; 'int16u',
        PrintConv =&gt; {
            0 =&gt; 'No Correction',
            1 =&gt; 'Differential Corrected',
        },
    },
    0x001f =&gt; {
        Name =&gt; 'GPSHPositioningError',
        Description =&gt; 'GPS Horizontal Positioning Error',
        PrintConv =&gt; '"$val m"',
        PrintConvInv =&gt; '$val=~s/\s*m$//; $val',
        Writable =&gt; 'rational64u',
    },
);

# Composite GPS tags
%Image::ExifTool::GPS::Composite = (
    GROUPS =&gt; { 2 =&gt; 'Location' },
    GPSDateTime =&gt; {
        Description =&gt; 'GPS Date/Time',
        Groups =&gt; { 2 =&gt; 'Time' },
        SubDoc =&gt; 1,    # generate for all sub-documents
        Require =&gt; {
            0 =&gt; 'GPS:GPSDateStamp',
            1 =&gt; 'GPS:GPSTimeStamp',
        },
        ValueConv =&gt; '"$val[0] $val[1]Z"',
        PrintConv =&gt; '$self-&gt;ConvertDateTime($val)',
    },
    # Note: The following tags are used by other modules
    # which must therefore require this module as necessary
    GPSLatitude =&gt; {
        SubDoc =&gt; 1,    # generate for all sub-documents
        Require =&gt; {
            0 =&gt; 'GPS:GPSLatitude',
            1 =&gt; 'GPS:GPSLatitudeRef',
        },
        ValueConv =&gt; '$val[1] =~ /^S/i ? -$val[0] : $val[0]',
        PrintConv =&gt; 'Image::ExifTool::GPS::ToDMS($self, $val, 1, "N")',
    },
    GPSLongitude =&gt; {
        SubDoc =&gt; 1,    # generate for all sub-documents
        Require =&gt; {
            0 =&gt; 'GPS:GPSLongitude',
            1 =&gt; 'GPS:GPSLongitudeRef',
        },
        ValueConv =&gt; '$val[1] =~ /^W/i ? -$val[0] : $val[0]',
        PrintConv =&gt; 'Image::ExifTool::GPS::ToDMS($self, $val, 1, "E")',
    },
    GPSAltitude =&gt; {
        SubDoc =&gt; 1,    # generate for all sub-documents
        Desire =&gt; {
            0 =&gt; 'GPS:GPSAltitude',
            1 =&gt; 'GPS:GPSAltitudeRef',
            2 =&gt; 'XMP:GPSAltitude',
            3 =&gt; 'XMP:GPSAltitudeRef',
        },
        # Require either GPS:GPSAltitudeRef or XMP:GPSAltitudeRef
        RawConv =&gt; '(defined $val[1] or defined $val[3]) ? $val : undef',
        ValueConv =&gt; q{
            my $alt = $val[0];
            $alt = $val[2] unless defined $alt;
            return undef unless defined $alt;
            return ($val[1] || $val[3]) ? -$alt : $alt;
        },
        PrintConv =&gt; q{
            $val = int($val * 10) / 10;
            return ($val =~ s/^-// ? "$val m Below" : "$val m Above") . " Sea Level";
        },
    },
);

# add our composite tags
Image::ExifTool::AddCompositeTags('Image::ExifTool::GPS');

#------------------------------------------------------------------------------
# Convert GPS timestamp value
# Inputs: 0) raw timestamp value string
# Returns: EXIF-formatted time string
sub ConvertTimeStamp($)
{
    my $val = shift;
    my ($h,$m,$s) = split ' ', $val;
    my $f = (($h || 0) * 60 + ($m || 0)) * 60 + ($s || 0);
    $h = int($f / 3600); $f -= $h * 3600;
    $m = int($f / 60);   $f -= $m * 60;
    $s = int($f);        $f -= $s;
    $f = int($f * 1000000 + 0.5);
    if ($f) {
        ($f = sprintf(".%.6d", $f)) =~ s/0+$//;
    } else {
        $f = ''
    }
    return sprintf("%.2d:%.2d:%.2d$f",$h,$m,$s);
}

#------------------------------------------------------------------------------
# Convert degrees to DMS, or whatever the current settings are
# Inputs: 0) ExifTool reference, 1) Value in degrees,
#         2) format code (0=no format, 1=CoordFormat, 2=XMP format)
#         3) 'N' or 'E' if sign is significant and N/S/E/W should be added
# Returns: DMS string
sub ToDMS($$;$$)
{
    my ($exifTool, $val, $doPrintConv, $ref) = @_;
    my ($fmt, $num);

    if ($ref) {
        if ($val &lt; 0) {
            $val = -$val;
            $ref = {N =&gt; 'S', E =&gt; 'W'}-&gt;{$ref};
        }
        $ref = " $ref" unless $doPrintConv and $doPrintConv eq '2';
    } else {
        $val = abs($val);
        $ref = '';
    }
    if ($doPrintConv) {
        if ($doPrintConv eq '1') {
            $fmt = ($exifTool-&gt;Options('CoordFormat') || q{%d deg %d' %.2f"}) . $ref;
        } else {
            $fmt = "%d,%.6f$ref";   # use XMP standard format
        }
        # count the number of format specifiers
        $num = ($fmt =~ tr/%/%/);
    } else {
        $num = 3;
    }
    my ($d, $m, $s);
    $d = $val;
    if ($num &gt; 1) {
        $d = int($d);
        $m = ($val - $d) * 60;
        if ($num &gt; 2) {
            $m = int($m);
            $s = ($val - $d - $m / 60) * 3600;
        }
    }
    return $doPrintConv ? sprintf($fmt, $d, $m, $s) : "$d $m $s$ref";
}

#------------------------------------------------------------------------------
# Convert to decimal degrees
# Inputs: 0) a string containing 1-3 decimal numbers and any amount of other garbage
#         1) true if value should be negative if coordinate ends in 'S' or 'W'
# Returns: Coordinate in degrees
sub ToDegrees($;$)
{
    my ($val, $doSign) = @_;
    # extract decimal or floating point values out of any other garbage
    my ($d, $m, $s) = ($val =~ /((?:[+-]?)(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee][+-]\d+)?)/g);
    my $deg = ($d || 0) + (($m || 0) + ($s || 0)/60) / 60;
    # make negative if S or W coordinate
    $deg = -$deg if $doSign ? $val =~ /[^A-Z](S|W)$/i : $deg &lt; 0;
    return $deg;
}


1;  #end

__END__

=head1 NAME

Image::ExifTool::GPS - EXIF GPS meta information tags

=head1 SYNOPSIS

This module is loaded automatically by Image::ExifTool when required.

=head1 DESCRIPTION

This module contains definitions required by Image::ExifTool to interpret
GPS (Global Positioning System) meta information in EXIF data.

=head1 AUTHOR

Copyright 2003-2011, Phil Harvey (phil at owl.phy.queensu.ca)

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=head1 REFERENCES

=over 4

=item L&lt;Image::Info|Image::Info&gt;

=back

=head1 SEE ALSO

L&lt;Image::ExifTool::TagNames/GPS Tags&gt;,
L&lt;Image::ExifTool(3pm)|Image::ExifTool&gt;,
L&lt;Image::Info(3pm)|Image::Info&gt;

=cut
</pre></body></html>