gpsfold: Remove $Id$ at EOF.
[gpstools.git] / branches / gpst.near / GPST.pm
blob3ede9062ee4e3defa6cf126983aa523215c5333a
1 package GPST;
3 #=======================================================================
4 # $Id$
6 # Character set: UTF-8
7 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
8 # License: GNU General Public License, see end of file for legal stuff.
9 #=======================================================================
11 use strict;
12 use warnings;
14 use GPSTdebug;
16 BEGIN {
17 use Exporter ();
18 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
20 my $rcs_id = '$Id$';
21 push(@main::version_array, $rcs_id);
22 $VERSION = ($rcs_id =~ / (\d+) /, $1);
24 @ISA = qw(Exporter);
25 @EXPORT = qw(&trackpoint);
26 %EXPORT_TAGS = ();
28 our @EXPORT_OK;
30 our $Spc = " ";
32 sub trackpoint {
33 # Receive a hash and return a trackpoint as a string {{{
34 my %Dat = @_;
36 defined($Dat{'type'}) || return(undef);
37 defined($Dat{'format'}) || return(undef);
38 defined($Dat{'error'}) || return(undef);
40 defined($Dat{'year'}) || ($Dat{'year'} = 0);
41 defined($Dat{'month'}) || ($Dat{'month'} = 0);
42 defined($Dat{'day'}) || ($Dat{'day'} = 0);
43 defined($Dat{'hour'}) || ($Dat{'hour'} = "");
44 defined($Dat{'min'}) || ($Dat{'min'} = "");
45 defined($Dat{'sec'}) || ($Dat{'sec'} = "");
46 my $print_time = (
47 !$Dat{'year'} ||
48 !$Dat{'month'} ||
49 !$Dat{'day'} ||
50 !length($Dat{'hour'}) ||
51 !length($Dat{'min'}) ||
52 !length($Dat{'sec'})
53 ) ? 0 : 1;
55 if (
56 ("$Dat{'year'}$Dat{'month'}$Dat{'day'}$Dat{'hour'}$Dat{'min'}" =~
57 /[^\d]/) || ($Dat{'sec'} =~ /[^\d\.]/)
58 ) {
59 ($print_time = 0);
61 "$Dat{'lat'}$Dat{'lon'}" =~ /[^\d\.\-\+]/ && return(undef);
63 defined($Dat{'lat'}) || ($Dat{'lat'} = "");
64 defined($Dat{'lon'}) || ($Dat{'lon'} = "");
65 defined($Dat{'ele'}) || ($Dat{'ele'} = "");
66 defined($Dat{'desc'}) || ($Dat{'desc'} = "");
67 defined($Dat{'extensions'}) || ($Dat{'extensions'} = "");
69 my $Retval = "";
71 if ($Dat{'type'} eq "tp") {
72 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
73 if ($Dat{'format'} eq "gpsml") {
74 # {{{
75 my $Elem = length($err_str) ? "etp" : "tp";
76 $Retval .= join("",
77 $print_time
78 ? sprintf("<time>%04u-%02u-%02uT" .
79 "%02u:%02u:%02gZ</time> ",
80 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
81 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
83 : "",
84 (length($Dat{'lat'}))
85 ? "<lat>" . $Dat{'lat'}*1.0 . "</lat> "
86 : "",
87 (length($Dat{'lon'}))
88 ? "<lon>" . $Dat{'lon'}*1.0 . "</lon> "
89 : "",
90 (length($Dat{'ele'}))
91 ? "<ele>" . $Dat{'ele'}*1.0 . "</ele> "
92 : "",
93 (length($Dat{'desc'}))
94 ? sprintf("<desc>%s</desc> ",
95 $Dat{'desc'})
96 : ""
98 length($Retval) &&
99 ($Retval = sprintf("<%s%s> %s</%s>\n",
100 $Elem,
101 length($err_str) ? " err=\"$err_str\"" : "",
102 $Retval,
103 $Elem)
105 # }}}
106 } elsif($Dat{'format'} eq "gpx") {
107 # {{{
108 my $lat_str = length($Dat{'lat'}) ? " lat=\"$Dat{'lat'}\"" : "";
109 my $lon_str = length($Dat{'lon'}) ? " lon=\"$Dat{'lon'}\"" : "";
110 if (length("$lat_str$lon_str$Dat{'ele'}")) {
111 $Retval .=
112 join("",
113 "$Spc$Spc$Spc$Spc$Spc$Spc",
114 "<trkpt$lat_str$lon_str>",
115 "$Spc",
116 length($Dat{'ele'})
117 ? "<ele>$Dat{'ele'}</ele>$Spc"
118 : "",
119 $print_time
120 ? "<time>" .
121 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
122 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
123 "</time>$Spc"
124 : "",
125 length($Dat{'extensions'})
126 ? "<extensions>" .
127 $Dat{'extensions'} .
128 "</extensions>"
129 : "",
130 "</trkpt>\n"
133 # }}}
134 } else {
135 $Retval = undef;
137 } else {
138 $Retval = undef;
140 return $Retval;
141 # }}}