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 (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
23 @EXPORT = qw(&trackpoint &postgresql_copy_safe);
31 # Receive a hash and return a trackpoint as a string {{{
34 defined($Dat{'what'}) || return(undef);
35 defined($Dat{'format'}) || return(undef);
36 defined($Dat{'error'}) || return(undef);
38 defined($Dat{'year'}) || ($Dat{'year'} = 0);
39 defined($Dat{'month'}) || ($Dat{'month'} = 0);
40 defined($Dat{'day'}) || ($Dat{'day'} = 0);
41 defined($Dat{'hour'}) || ($Dat{'hour'} = "");
42 defined($Dat{'min'}) || ($Dat{'min'} = "");
43 defined($Dat{'sec'}) || ($Dat{'sec'} = "");
44 $Dat{'print_time'} = (
48 !length($Dat{'hour'}) ||
49 !length($Dat{'min'}) ||
54 ("$Dat{'year'}$Dat{'month'}$Dat{'day'}$Dat{'hour'}$Dat{'min'}" =~
55 /[^\d]/) || ($Dat{'sec'} =~ /[^\d\.]/)
57 ($Dat{'print_time'} = 0);
59 "$Dat{'lat'}$Dat{'lon'}" =~ /[^\d\.\-\+]/ && return(undef);
61 defined($Dat{'lat'}) || ($Dat{'lat'} = "");
62 defined($Dat{'lon'}) || ($Dat{'lon'} = "");
63 defined($Dat{'ele'}) || ($Dat{'ele'} = "");
64 defined($Dat{'desc'}) || ($Dat{'desc'} = "");
68 if ($Dat{'what'} eq "tp") {
69 if ($Dat{'format'} eq "gpsml") {
70 $Retval .= gen_gpsml_entry
(%Dat);
71 } elsif($Dat{'format'} eq "gpx") {
72 $Retval .= gen_gpx_entry
(%Dat);
73 } elsif($Dat{'format'} eq "clean") {
74 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
75 } elsif($Dat{'format'} eq "xgraph") {
76 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
77 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
79 } elsif ($Dat{'format'} eq "pgtab") {
82 ?
"$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
83 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
85 (length($Dat{'lat'}) && length($Dat{'lon'}))
86 ?
"($Dat{'lat'},$Dat{'lon'})"
88 length($Dat{'ele'}) ?
$Dat{'ele'} : '\N', # ele
93 } elsif ($Dat{'format'} eq "gpstrans") {
95 my ($gpt_lat, $gpt_lon) =
96 (ddd_to_dms
($Dat{'lat'}), ddd_to_dms
($Dat{'lon'}));
97 if ($Dat{'print_time'}) {
98 $Retval .= "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
99 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
100 "$gpt_lat\t$gpt_lon\n";
102 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
119 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
120 my $lat_str = length($Dat{'lat'}) ?
" lat=\"$Dat{'lat'}\"" : "";
121 my $lon_str = length($Dat{'lon'}) ?
" lon=\"$Dat{'lon'}\"" : "";
122 my ($estr_begin, $estr_ext, $estr_end) =
124 if (length($err_str)) {
125 $estr_begin = "<!-- ";
126 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
129 if (length("$lat_str$lon_str$Dat{'ele'}")) {
132 "$Spc$Spc$Spc$Spc$Spc$Spc",
134 "<trkpt$lat_str$lon_str>",
137 ?
"<ele>$Dat{'ele'}</ele>$Spc"
141 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
142 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
146 "</trkpt>$estr_end\n"
153 sub gen_gpsml_entry
{
156 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
157 my $Elem = length($err_str) ?
"etp" : "tp";
158 my $Retval = join("",
160 ?
sprintf("<time>%04u-%02u-%02uT" .
161 "%02u:%02u:%02gZ</time> ",
162 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
163 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
166 (length($Dat{'lat'}))
167 ?
"<lat>" . $Dat{'lat'}*1.0 . "</lat> "
169 (length($Dat{'lon'}))
170 ?
"<lon>" . $Dat{'lon'}*1.0 . "</lon> "
172 (length($Dat{'ele'}))
173 ?
"<ele>" . $Dat{'ele'}*1.0 . "</ele> "
175 (length($Dat{'desc'}))
176 ?
sprintf("<desc>%s</desc> ",
181 ($Retval = sprintf("<%s%s> %s</%s>\n",
183 length($err_str) ?
" err=\"$err_str\"" : "",
189 } # gen_gpsml_entry()
191 sub postgresql_copy_safe
{
194 $Str =~ s/\\/\\\\/gs;