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 &num_expand);
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'} =~ /[^\d\.\-\+]/ &&
60 (warn("$main::progname: Invalid value in latitude value: '$Dat{'lat'}'\n"), return(undef));
61 $Dat{'lon'} =~ /[^\d\.\-\+]/ &&
62 (warn("$main::progname: Invalid value in longitude value: '$Dat{'lon'}'\n"), return(undef));
63 length($Dat{'ele'}) && $Dat{'ele'} =~ /[^\d\.\-\+]/ &&
64 (warn("$main::progname: Invalid value in elevation value: '$Dat{'ele'}'\n"), return(undef));
66 defined($Dat{'lat'}) || ($Dat{'lat'} = "");
67 defined($Dat{'lon'}) || ($Dat{'lon'} = "");
68 defined($Dat{'ele'}) || ($Dat{'ele'} = "");
69 defined($Dat{'desc'}) || ($Dat{'desc'} = "");
73 if ($Dat{'what'} eq "tp") {
74 if ($Dat{'format'} eq "gpsml") {
75 $Retval .= gen_gpsml_entry
(%Dat);
76 } elsif($Dat{'format'} eq "gpx") {
77 $Retval .= gen_gpx_entry
(%Dat);
78 } elsif($Dat{'format'} eq "clean") {
79 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
80 } elsif($Dat{'format'} eq "xgraph") {
81 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
82 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
84 } elsif ($Dat{'format'} eq "pgtab") {
85 $Retval .= gen_pgtab_entry
(%Dat);
86 } elsif ($Dat{'format'} eq "gpstrans") {
87 $Retval .= gen_gpstrans_entry
(%Dat);
102 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
103 my $lat_str = length($Dat{'lat'}) ?
" lat=\"$Dat{'lat'}\"" : "";
104 my $lon_str = length($Dat{'lon'}) ?
" lon=\"$Dat{'lon'}\"" : "";
105 my ($estr_begin, $estr_ext, $estr_end) =
107 if (length($err_str)) {
108 $estr_begin = "<!-- ";
109 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
112 if (length("$lat_str$lon_str$Dat{'ele'}")) {
115 "$Spc$Spc$Spc$Spc$Spc$Spc",
117 "<trkpt$lat_str$lon_str>",
120 ?
"<ele>$Dat{'ele'}</ele>$Spc"
124 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
125 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
129 "</trkpt>$estr_end\n"
136 sub gen_gpsml_entry
{
139 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
140 my $Elem = length($err_str) ?
"etp" : "tp";
141 my $Retval = join("",
143 ?
sprintf("<time>%04u-%02u-%02uT" .
144 "%02u:%02u:%02gZ</time> ",
145 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
146 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
149 (length($Dat{'lat'}))
150 ?
"<lat>" . $Dat{'lat'}*1.0 . "</lat> "
152 (length($Dat{'lon'}))
153 ?
"<lon>" . $Dat{'lon'}*1.0 . "</lon> "
155 (length($Dat{'ele'}))
156 ?
"<ele>" . $Dat{'ele'}*1.0 . "</ele> "
158 (length($Dat{'desc'}))
159 ?
sprintf("<desc>%s</desc> ",
164 ($Retval = sprintf("<%s%s> %s</%s>\n",
166 length($err_str) ?
" err=\"$err_str\"" : "",
172 } # gen_gpsml_entry()
174 sub gen_pgtab_entry
{
177 my $Retval = join("\t",
179 ?
"$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
180 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
182 (length($Dat{'lat'}) && length($Dat{'lon'}))
183 ?
"($Dat{'lat'},$Dat{'lon'})"
185 length($Dat{'ele'}) ?
$Dat{'ele'} : '\N', # ele
192 } # gen_pgtab_entry()
194 sub gen_gpstrans_entry
{
198 my ($gpt_lat, $gpt_lon) =
199 (ddd_to_dms
($Dat{'lat'}), ddd_to_dms
($Dat{'lon'}));
200 if ($Dat{'print_time'}) {
201 $Retval = "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
202 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
203 "$gpt_lat\t$gpt_lon\n";
205 $Retval = "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
209 } # gen_gpstrans_entry()
211 sub postgresql_copy_safe
{
214 $Str =~ s/\\/\\\\/gs;
223 # Convert scientific notation to decimal notation {{{
226 return $n unless $n =~ /^(.*)e([-+]?)(.*)$/;
227 my ($num, $sign, $exp) = ($1, $2, $3);
228 my $sig = $sign eq '-' ?
"." . ($exp - 1 + length $num) : '';
229 $Retval = sprintf("%${sig}f", $n);
230 ($Retval =~ /\.\d/) && ($Retval =~ s/0+$//);