gpst: print_gpx() truncates values instead of round in pgwtab and pgwupd
[gpstools.git] / GPST.pm
blobd33f39c4aacadfde9b54c65a19cb9134a2be56e9
1 package GPST;
3 #=======================================================================
4 # GPST.pm
5 # File ID: 5e0437a0-fafa-11dd-abd7-000475e441b9
7 # Character set: UTF-8
8 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
9 # License: GNU General Public License, see end of file for legal stuff.
10 #=======================================================================
12 use strict;
13 use warnings;
15 use GPSTdebug;
16 use GPSTgeo;
18 BEGIN {
19 use Exporter ();
20 our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
22 @ISA = qw(Exporter);
23 @EXPORT = qw(&trackpoint &postgresql_copy_safe &num_expand);
24 %EXPORT_TAGS = ();
26 our @EXPORT_OK;
28 our $Spc = " ";
30 sub trackpoint {
31 # Receive a hash and return a trackpoint as a string {{{
32 my %Dat = @_;
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'} = (
45 !$Dat{'year'} ||
46 !$Dat{'month'} ||
47 !$Dat{'day'} ||
48 !length($Dat{'hour'}) ||
49 !length($Dat{'min'}) ||
50 !length($Dat{'sec'})
51 ) ? 0 : 1;
53 if (
54 ("$Dat{'year'}$Dat{'month'}$Dat{'day'}$Dat{'hour'}$Dat{'min'}" =~
55 /[^\d]/) || ($Dat{'sec'} =~ /[^\d\.]/)
56 ) {
57 ($Dat{'print_time'} = 0);
59 $Dat{'lat'} =~ /[^\d\.\-\+]/ && return(undef);
60 $Dat{'lon'} =~ /[^\d\.\-\+]/ && return(undef);
61 length($Dat{'ele'}) && $Dat{'ele'} =~ /[^\d\.\-\+]/ && return(undef);
63 defined($Dat{'lat'}) || ($Dat{'lat'} = "");
64 defined($Dat{'lon'}) || ($Dat{'lon'} = "");
65 defined($Dat{'ele'}) || ($Dat{'ele'} = "");
66 defined($Dat{'desc'}) || ($Dat{'desc'} = "");
68 $Dat{'lat'} = num_expand($Dat{'lat'});
69 $Dat{'lon'} = num_expand($Dat{'lon'});
70 $Dat{'ele'} = num_expand($Dat{'ele'});
72 my $Retval = "";
74 if ($Dat{'what'} eq "tp") {
75 if ($Dat{'format'} eq "gpsml") {
76 $Retval .= gen_gpsml_entry(%Dat);
77 } elsif($Dat{'format'} eq "gpx") {
78 $Retval .= gen_gpx_entry(%Dat);
79 } elsif($Dat{'format'} eq "clean") {
80 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
81 } elsif($Dat{'format'} eq "xgraph") {
82 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
83 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
85 } elsif ($Dat{'format'} eq "pgtab") {
86 $Retval .= gen_pgtab_entry(%Dat);
87 } elsif ($Dat{'format'} eq "gpstrans") {
88 $Retval .= gen_gpstrans_entry(%Dat);
89 } else {
90 $Retval = undef;
92 } else {
93 $Retval = undef;
95 return $Retval;
96 # }}}
97 } # trackpoint()
99 sub gen_gpx_entry {
100 # {{{
101 my %Dat = @_;
102 my $Retval = "";
103 $Dat{'lat'} = num_expand($Dat{'lat'});
104 $Dat{'lon'} = num_expand($Dat{'lon'});
105 $Dat{'ele'} = num_expand($Dat{'ele'});
106 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
107 my $lat_str = length($Dat{'lat'}) ? " lat=\"$Dat{'lat'}\"" : "";
108 my $lon_str = length($Dat{'lon'}) ? " lon=\"$Dat{'lon'}\"" : "";
109 my ($estr_begin, $estr_ext, $estr_end) =
110 ( "", "", "");
111 if (length($err_str)) {
112 $estr_begin = "<!-- ";
113 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
114 $estr_end = " -->";
116 if (length("$lat_str$lon_str$Dat{'ele'}")) {
117 $Retval .=
118 join("",
119 "$Spc$Spc$Spc$Spc$Spc$Spc",
120 $estr_begin,
121 "<trkpt$lat_str$lon_str>",
122 "$Spc",
123 length($Dat{'ele'})
124 ? "<ele>$Dat{'ele'}</ele>$Spc"
125 : "",
126 $Dat{'print_time'}
127 ? "<time>" .
128 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
129 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
130 "</time>$Spc"
131 : "",
132 $estr_ext,
133 "</trkpt>$estr_end\n"
136 return($Retval);
137 # }}}
138 } # gen_gpx_entry()
140 sub gen_gpsml_entry {
141 # {{{
142 my %Dat = @_;
143 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
144 my $Elem = length($err_str) ? "etp" : "tp";
145 my $Retval = join("",
146 $Dat{'print_time'}
147 ? sprintf("<time>%04u-%02u-%02uT" .
148 "%02u:%02u:%02gZ</time> ",
149 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
150 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
152 : "",
153 (length($Dat{'lat'}))
154 ? "<lat>" . $Dat{'lat'}*1.0 . "</lat> "
155 : "",
156 (length($Dat{'lon'}))
157 ? "<lon>" . $Dat{'lon'}*1.0 . "</lon> "
158 : "",
159 (length($Dat{'ele'}))
160 ? "<ele>" . $Dat{'ele'}*1.0 . "</ele> "
161 : "",
162 (length($Dat{'desc'}))
163 ? sprintf("<desc>%s</desc> ",
164 $Dat{'desc'})
165 : ""
167 length($Retval) &&
168 ($Retval = sprintf("<%s%s> %s</%s>\n",
169 $Elem,
170 length($err_str) ? " err=\"$err_str\"" : "",
171 $Retval,
172 $Elem)
174 return($Retval);
175 # }}}
176 } # gen_gpsml_entry()
178 sub gen_pgtab_entry {
179 # {{{
180 my %Dat = @_;
181 my $Retval = join("\t",
182 $Dat{'year'}
183 ? "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
184 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
185 : '\N', # date
186 (length($Dat{'lat'}) && length($Dat{'lon'}))
187 ? "($Dat{'lat'},$Dat{'lon'})"
188 : '\N', # coor
189 length($Dat{'ele'}) ? $Dat{'ele'} : '\N', # ele
190 '\N', # name
191 '\N', # dist
192 '\N' # description
193 ) . "\n";
194 return($Retval);
195 # }}}
196 } # gen_pgtab_entry()
198 sub gen_gpstrans_entry {
199 # {{{
200 my %Dat = @_;
201 my $Retval;
202 my ($gpt_lat, $gpt_lon) =
203 (ddd_to_dms($Dat{'lat'}), ddd_to_dms($Dat{'lon'}));
204 if ($Dat{'print_time'}) {
205 $Retval = "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
206 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
207 "$gpt_lat\t$gpt_lon\n";
208 } else {
209 $Retval = "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
211 return($Retval);
212 # }}}
213 } # gen_gpstrans_entry()
215 sub postgresql_copy_safe {
216 # {{{
217 my $Str = shift;
218 $Str =~ s/\\/\\\\/gs;
219 $Str =~ s/\n/\\n/gs;
220 $Str =~ s/\r/\\r/gs;
221 $Str =~ s/\t/\\t/gs;
222 return($Str);
223 # }}}
224 } # postgresql_copy_safe()
226 sub num_expand {
227 # Convert scientific notation to decimal notation {{{
228 my $Retval = shift;
229 length($Retval) || return("");
230 if ($Retval =~ /^(.*)e([-+]?)(.*)$/) {
231 my ($num, $sign, $exp) = ($1, $2, $3);
232 my $sig = $sign eq '-' ? "." . ($exp - 1 + length $num) : '';
233 $Retval = sprintf("%${sig}f", $Retval);
235 $Retval =~ s/^\+//;
236 my $minus = ($Retval =~ s/^-//) ? "-" : "";
237 if ($Retval =~ /\.\d/) {
238 $Retval =~ s/0+$//;
239 $Retval =~ s/^0+/0/;
240 $Retval =~ s/^0([1-9]+)\./$1./;
241 $Retval =~ s/\.$//;
242 } else {
243 $Retval =~ s/^0+//;
245 length($Retval) || ($Retval = 0);
246 $Retval = $Retval ? "$minus$Retval" : 0;
247 return($Retval);
248 # }}}
249 } # num_expand()