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);
35 # Receive a hash and return a trackpoint as a string {{{
38 defined($Dat{'what'}) || return(undef);
39 defined($Dat{'format'}) || return(undef);
40 defined($Dat{'error'}) || return(undef);
42 defined($Dat{'year'}) || ($Dat{'year'} = 0);
43 defined($Dat{'month'}) || ($Dat{'month'} = 0);
44 defined($Dat{'day'}) || ($Dat{'day'} = 0);
45 defined($Dat{'hour'}) || ($Dat{'hour'} = "");
46 defined($Dat{'min'}) || ($Dat{'min'} = "");
47 defined($Dat{'sec'}) || ($Dat{'sec'} = "");
48 $Dat{'print_time'} = (
52 !length($Dat{'hour'}) ||
53 !length($Dat{'min'}) ||
58 ("$Dat{'year'}$Dat{'month'}$Dat{'day'}$Dat{'hour'}$Dat{'min'}" =~
59 /[^\d]/) || ($Dat{'sec'} =~ /[^\d\.]/)
61 ($Dat{'print_time'} = 0);
63 "$Dat{'lat'}$Dat{'lon'}" =~ /[^\d\.\-\+]/ && return(undef);
65 defined($Dat{'lat'}) || ($Dat{'lat'} = "");
66 defined($Dat{'lon'}) || ($Dat{'lon'} = "");
67 defined($Dat{'ele'}) || ($Dat{'ele'} = "");
68 defined($Dat{'desc'}) || ($Dat{'desc'} = "");
72 if ($Dat{'what'} eq "tp") {
73 if ($Dat{'format'} eq "gpsml") {
74 $Retval .= gen_gpsml_entry
(%Dat);
75 } elsif($Dat{'format'} eq "gpx") {
76 $Retval .= gen_gpx_entry
(%Dat);
77 } elsif($Dat{'format'} eq "clean") {
78 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
79 } elsif($Dat{'format'} eq "xgraph") {
80 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
81 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
83 } elsif ($Dat{'format'} eq "pgtab") {
86 ?
"$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
87 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
89 (length($Dat{'lat'}) && length($Dat{'lon'}))
90 ?
"($Dat{'lat'},$Dat{'lon'})"
92 length($Dat{'ele'}) ?
$Dat{'ele'} : '\N', # ele
97 } elsif ($Dat{'format'} eq "gpstrans") {
99 my ($gpt_lat, $gpt_lon) =
100 (ddd_to_dms
($Dat{'lat'}), ddd_to_dms
($Dat{'lon'}));
101 if ($Dat{'print_time'}) {
102 $Retval .= "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
103 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
104 "$gpt_lat\t$gpt_lon\n";
106 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
123 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
124 my $lat_str = length($Dat{'lat'}) ?
" lat=\"$Dat{'lat'}\"" : "";
125 my $lon_str = length($Dat{'lon'}) ?
" lon=\"$Dat{'lon'}\"" : "";
126 my ($estr_begin, $estr_ext, $estr_end) =
128 if (length($err_str)) {
129 $estr_begin = "<!-- ";
130 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
133 if (length("$lat_str$lon_str$Dat{'ele'}")) {
136 "$Spc$Spc$Spc$Spc$Spc$Spc",
138 "<trkpt$lat_str$lon_str>",
141 ?
"<ele>$Dat{'ele'}</ele>$Spc"
145 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
146 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
150 "</trkpt>$estr_end\n"
157 sub gen_gpsml_entry
{
160 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
161 my $Elem = length($err_str) ?
"etp" : "tp";
162 my $Retval = join("",
164 ?
sprintf("<time>%04u-%02u-%02uT" .
165 "%02u:%02u:%02gZ</time> ",
166 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
167 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
170 (length($Dat{'lat'}))
171 ?
"<lat>" . $Dat{'lat'}*1.0 . "</lat> "
173 (length($Dat{'lon'}))
174 ?
"<lon>" . $Dat{'lon'}*1.0 . "</lon> "
176 (length($Dat{'ele'}))
177 ?
"<ele>" . $Dat{'ele'}*1.0 . "</ele> "
179 (length($Dat{'desc'}))
180 ?
sprintf("<desc>%s</desc> ",
185 ($Retval = sprintf("<%s%s> %s</%s>\n",
187 length($err_str) ?
" err=\"$err_str\"" : "",
193 } # gen_gpsml_entry()
195 sub postgresql_copy_safe
{
198 $Str =~ s/\\/\\\\/gs;