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'} = "");
71 $Dat{'lat'} = num_expand
($Dat{'lat'});
72 $Dat{'lon'} = num_expand
($Dat{'lon'});
73 $Dat{'ele'} = num_expand
($Dat{'ele'});
77 if ($Dat{'what'} eq "tp") {
78 if ($Dat{'format'} eq "gpsml") {
79 $Retval .= gen_gpsml_entry
(%Dat);
80 } elsif($Dat{'format'} eq "gpx") {
81 $Retval .= gen_gpx_entry
(%Dat);
82 } elsif($Dat{'format'} eq "clean") {
83 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
84 } elsif($Dat{'format'} eq "xgraph") {
85 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
86 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
88 } elsif ($Dat{'format'} eq "pgtab") {
89 $Retval .= gen_pgtab_entry
(%Dat);
90 } elsif ($Dat{'format'} eq "gpstrans") {
91 $Retval .= gen_gpstrans_entry
(%Dat);
106 $Dat{'lat'} = num_expand
($Dat{'lat'});
107 $Dat{'lon'} = num_expand
($Dat{'lon'});
108 $Dat{'ele'} = num_expand
($Dat{'ele'});
109 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
110 my $lat_str = length($Dat{'lat'}) ?
" lat=\"$Dat{'lat'}\"" : "";
111 my $lon_str = length($Dat{'lon'}) ?
" lon=\"$Dat{'lon'}\"" : "";
112 my ($estr_begin, $estr_ext, $estr_end) =
114 if (length($err_str)) {
115 $estr_begin = "<!-- ";
116 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
119 if (length("$lat_str$lon_str$Dat{'ele'}")) {
122 "$Spc$Spc$Spc$Spc$Spc$Spc",
124 "<trkpt$lat_str$lon_str>",
127 ?
"<ele>$Dat{'ele'}</ele>$Spc"
131 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
132 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
136 "</trkpt>$estr_end\n"
143 sub gen_gpsml_entry
{
146 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
147 my $Elem = length($err_str) ?
"etp" : "tp";
148 my $Retval = join("",
150 ?
sprintf("<time>%04u-%02u-%02uT" .
151 "%02u:%02u:%02gZ</time> ",
152 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
153 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
156 (length($Dat{'lat'}))
157 ?
"<lat>" . $Dat{'lat'}*1.0 . "</lat> "
159 (length($Dat{'lon'}))
160 ?
"<lon>" . $Dat{'lon'}*1.0 . "</lon> "
162 (length($Dat{'ele'}))
163 ?
"<ele>" . $Dat{'ele'}*1.0 . "</ele> "
165 (length($Dat{'desc'}))
166 ?
sprintf("<desc>%s</desc> ",
171 ($Retval = sprintf("<%s%s> %s</%s>\n",
173 length($err_str) ?
" err=\"$err_str\"" : "",
179 } # gen_gpsml_entry()
181 sub gen_pgtab_entry
{
184 my $Retval = join("\t",
186 ?
"$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
187 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
189 (length($Dat{'lat'}) && length($Dat{'lon'}))
190 ?
"($Dat{'lat'},$Dat{'lon'})"
192 length($Dat{'ele'}) ?
$Dat{'ele'} : '\N', # ele
199 } # gen_pgtab_entry()
201 sub gen_gpstrans_entry
{
205 my ($gpt_lat, $gpt_lon) =
206 (ddd_to_dms
($Dat{'lat'}), ddd_to_dms
($Dat{'lon'}));
207 if ($Dat{'print_time'}) {
208 $Retval = "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
209 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
210 "$gpt_lat\t$gpt_lon\n";
212 $Retval = "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
216 } # gen_gpstrans_entry()
218 sub postgresql_copy_safe
{
221 $Str =~ s/\\/\\\\/gs;
227 } # postgresql_copy_safe()
230 # Convert scientific notation to decimal notation {{{
232 length($Retval) || return("");
233 if ($Retval =~ /^(.*)e([-+]?)(.*)$/) {
234 my ($num, $sign, $exp) = ($1, $2, $3);
235 my $sig = $sign eq '-' ?
"." . ($exp - 1 + length $num) : '';
236 $Retval = sprintf("%${sig}f", $Retval);
239 my $minus = ($Retval =~ s/^-//) ?
"-" : "";
240 if ($Retval =~ /\.\d/) {
243 $Retval =~ s/^0([1-9]+)\./$1./;
248 length($Retval) || ($Retval = 0);
249 $Retval = $Retval ?
"$minus$Retval" : 0;