3 #=======================================================================
5 # File ID: 5e0437a0-fafa-11dd-abd7-000475e441b9
8 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
9 # License: GNU General Public License, see end of file for legal stuff.
10 #=======================================================================
20 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
23 push(@main::version_array
, $rcs_id);
24 $VERSION = ($rcs_id =~ / (\d+) /, $1);
27 @EXPORT = qw(&trackpoint &postgresql_copy_safe @wpt_elems);
34 "ele", "time", "magvar", "geoidheight",
36 "name", "cmt", "desc", "src", "link", "sym", "type",
38 "fix", "sat", "hdop", "vdop", "pdop",
39 "ageofdgpsdata", "dgpsid",
47 # Receive a hash and return a trackpoint as a string {{{
50 defined($Dat{'what'}) || return(undef);
51 defined($Dat{'format'}) || return(undef);
52 defined($Dat{'error'}) || return(undef);
54 defined($Dat{'year'}) || ($Dat{'year'} = 0);
55 defined($Dat{'month'}) || ($Dat{'month'} = 0);
56 defined($Dat{'day'}) || ($Dat{'day'} = 0);
57 defined($Dat{'hour'}) || ($Dat{'hour'} = "");
58 defined($Dat{'min'}) || ($Dat{'min'} = "");
59 defined($Dat{'sec'}) || ($Dat{'sec'} = "");
64 !length($Dat{'hour'}) ||
65 !length($Dat{'min'}) ||
70 ("$Dat{'year'}$Dat{'month'}$Dat{'day'}$Dat{'hour'}$Dat{'min'}" =~
71 /[^\d]/) || ($Dat{'sec'} =~ /[^\d\.]/)
75 "$Dat{'lat'}$Dat{'lon'}" =~ /[^\d\.\-\+]/ && return(undef);
77 defined($Dat{'lat'}) || ($Dat{'lat'} = "");
78 defined($Dat{'lon'}) || ($Dat{'lon'} = "");
79 defined($Dat{'ele'}) || ($Dat{'ele'} = "");
80 defined($Dat{'desc'}) || ($Dat{'desc'} = "");
81 defined($Dat{'extensions'}) || ($Dat{'extensions'} = "");
85 if ($Dat{'what'} eq "tp") {
86 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
87 if ($Dat{'format'} eq "gpsml") {
89 my $Elem = length($err_str) ? "etp" : "tp";
92 ? sprintf("<time>%04u-%02u-%02uT" .
93 "%02u:%02u:%02gZ</time> ",
94 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
95 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
99 ? "<lat>" . $Dat{'lat'}*1.0 . "</lat> "
101 (length($Dat{'lon'}))
102 ? "<lon>" . $Dat{'lon'}*1.0 . "</lon> "
104 (length($Dat{'ele'}))
105 ? "<ele>" . $Dat{'ele'}*1.0 . "</ele> "
107 (length($Dat{'desc'}))
108 ? sprintf("<desc>%s</desc> ",
113 ($Retval = sprintf("<%s%s> %s</%s>\n",
115 length($err_str) ? " err=\"$err_str\"" : "",
120 } elsif($Dat{'format'} eq "gpx") {
122 my $lat_str = length($Dat{'lat'}) ? " lat=\"$Dat{'lat'}\"" : "";
123 my $lon_str = length($Dat{'lon'}) ? " lon=\"$Dat{'lon'}\"" : "";
124 my ($estr_begin, $estr_end) =
126 if (length($err_str)) {
127 $estr_begin = "<!-- ";
128 $Dat{'extensions'} .= "<error>$err_str</error>";
131 if (length("$lat_str$lon_str$Dat{'ele'}")) {
134 "$Spc$Spc$Spc$Spc$Spc$Spc",
136 "<trkpt$lat_str$lon_str>",
139 ? "<ele>$Dat{'ele'}</ele>$Spc"
143 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
144 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
147 length($Dat{'extensions'})
154 "</trkpt>$estr_end\n"
158 } elsif($Dat{'format'} eq "clean") {
159 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
160 } elsif($Dat{'format'} eq "xgraph") {
161 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
162 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
164 } elsif ($Dat{'format'} eq "pgtab") {
165 $Retval .= join("\t",
167 ? "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
168 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
170 (length($Dat{'lat'}) && length($Dat{'lon'}))
171 ? "($Dat{'lat'},$Dat{'lon'})"
173 length($Dat{'ele'}) ? $Dat{'ele'} : '\N', # ele
178 } elsif ($Dat{'format'} eq "gpstrans") {
180 my ($gpt_lat, $gpt_lon) =
181 (ddd_to_dms($Dat{'lat'}), ddd_to_dms($Dat{'lon'}));
183 $Retval .= "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
184 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
185 "$gpt_lat\t$gpt_lon\n";
187 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
200 sub postgresql_copy_safe {
203 $Str =~ s/\\/\\\\/gs;