Merge branch 'fixrounding'
[gpstools.git] / GPST.pm
bloba05613a89c200def043fcd8ab9e837f45639ef05
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\.\-\+]/ &&
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 my $Retval = "";
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);
88 } else {
89 $Retval = undef;
91 } else {
92 $Retval = undef;
94 return $Retval;
95 # }}}
96 } # trackpoint()
98 sub gen_gpx_entry {
99 # {{{
100 my %Dat = @_;
101 my $Retval = "";
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) =
106 ( "", "", "");
107 if (length($err_str)) {
108 $estr_begin = "<!-- ";
109 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
110 $estr_end = " -->";
112 if (length("$lat_str$lon_str$Dat{'ele'}")) {
113 $Retval .=
114 join("",
115 "$Spc$Spc$Spc$Spc$Spc$Spc",
116 $estr_begin,
117 "<trkpt$lat_str$lon_str>",
118 "$Spc",
119 length($Dat{'ele'})
120 ? "<ele>$Dat{'ele'}</ele>$Spc"
121 : "",
122 $Dat{'print_time'}
123 ? "<time>" .
124 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
125 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
126 "</time>$Spc"
127 : "",
128 $estr_ext,
129 "</trkpt>$estr_end\n"
132 return($Retval);
133 # }}}
134 } # gen_gpx_entry()
136 sub gen_gpsml_entry {
137 # {{{
138 my %Dat = @_;
139 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
140 my $Elem = length($err_str) ? "etp" : "tp";
141 my $Retval = join("",
142 $Dat{'print_time'}
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
148 : "",
149 (length($Dat{'lat'}))
150 ? "<lat>" . $Dat{'lat'}*1.0 . "</lat> "
151 : "",
152 (length($Dat{'lon'}))
153 ? "<lon>" . $Dat{'lon'}*1.0 . "</lon> "
154 : "",
155 (length($Dat{'ele'}))
156 ? "<ele>" . $Dat{'ele'}*1.0 . "</ele> "
157 : "",
158 (length($Dat{'desc'}))
159 ? sprintf("<desc>%s</desc> ",
160 $Dat{'desc'})
161 : ""
163 length($Retval) &&
164 ($Retval = sprintf("<%s%s> %s</%s>\n",
165 $Elem,
166 length($err_str) ? " err=\"$err_str\"" : "",
167 $Retval,
168 $Elem)
170 return($Retval);
171 # }}}
172 } # gen_gpsml_entry()
174 sub gen_pgtab_entry {
175 # {{{
176 my %Dat = @_;
177 my $Retval = join("\t",
178 $Dat{'year'}
179 ? "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
180 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
181 : '\N', # date
182 (length($Dat{'lat'}) && length($Dat{'lon'}))
183 ? "($Dat{'lat'},$Dat{'lon'})"
184 : '\N', # coor
185 length($Dat{'ele'}) ? $Dat{'ele'} : '\N', # ele
186 '\N', # name
187 '\N', # dist
188 '\N' # description
189 ) . "\n";
190 return($Retval);
191 # }}}
192 } # gen_pgtab_entry()
194 sub gen_gpstrans_entry {
195 # {{{
196 my %Dat = @_;
197 my $Retval;
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";
204 } else {
205 $Retval = "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
207 return($Retval);
208 # }}}
209 } # gen_gpstrans_entry()
211 sub postgresql_copy_safe {
212 # {{{
213 my $Str = shift;
214 $Str =~ s/\\/\\\\/gs;
215 $Str =~ s/\n/\\n/gs;
216 $Str =~ s/\r/\\r/gs;
217 $Str =~ s/\t/\\t/gs;
218 return($Str);
219 # }}}
222 sub num_expand {
223 # Convert scientific notation to decimal notation {{{
224 my $n = shift;
225 my $Retval;
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+$//);
231 return($Retval);
232 # }}}