GPST.pm: Move pgtab stuff out of trackpoint() into the new gen_pgtab_entry()
[gpstools.git] / GPST.pm
blobf61aa1ebde47f5300e56a2bd3d6b687ce7de2e96
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);
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'}$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'} = "");
66 my $Retval = "";
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") {
80 $Retval .= gen_pgtab_entry(%Dat);
81 } elsif ($Dat{'format'} eq "gpstrans") {
82 # {{{
83 my ($gpt_lat, $gpt_lon) =
84 (ddd_to_dms($Dat{'lat'}), ddd_to_dms($Dat{'lon'}));
85 if ($Dat{'print_time'}) {
86 $Retval .= "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
87 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
88 "$gpt_lat\t$gpt_lon\n";
89 } else {
90 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
92 # }}}
93 } else {
94 $Retval = undef;
96 } else {
97 $Retval = undef;
99 return $Retval;
100 # }}}
101 } # trackpoint()
103 sub gen_gpx_entry {
104 # {{{
105 my %Dat = @_;
106 my $Retval = "";
107 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
108 my $lat_str = length($Dat{'lat'}) ? " lat=\"$Dat{'lat'}\"" : "";
109 my $lon_str = length($Dat{'lon'}) ? " lon=\"$Dat{'lon'}\"" : "";
110 my ($estr_begin, $estr_ext, $estr_end) =
111 ( "", "", "");
112 if (length($err_str)) {
113 $estr_begin = "<!-- ";
114 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
115 $estr_end = " -->";
117 if (length("$lat_str$lon_str$Dat{'ele'}")) {
118 $Retval .=
119 join("",
120 "$Spc$Spc$Spc$Spc$Spc$Spc",
121 $estr_begin,
122 "<trkpt$lat_str$lon_str>",
123 "$Spc",
124 length($Dat{'ele'})
125 ? "<ele>$Dat{'ele'}</ele>$Spc"
126 : "",
127 $Dat{'print_time'}
128 ? "<time>" .
129 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
130 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
131 "</time>$Spc"
132 : "",
133 $estr_ext,
134 "</trkpt>$estr_end\n"
137 return($Retval);
138 # }}}
139 } # gen_gpx_entry()
141 sub gen_gpsml_entry {
142 # {{{
143 my %Dat = @_;
144 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
145 my $Elem = length($err_str) ? "etp" : "tp";
146 my $Retval = join("",
147 $Dat{'print_time'}
148 ? sprintf("<time>%04u-%02u-%02uT" .
149 "%02u:%02u:%02gZ</time> ",
150 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
151 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
153 : "",
154 (length($Dat{'lat'}))
155 ? "<lat>" . $Dat{'lat'}*1.0 . "</lat> "
156 : "",
157 (length($Dat{'lon'}))
158 ? "<lon>" . $Dat{'lon'}*1.0 . "</lon> "
159 : "",
160 (length($Dat{'ele'}))
161 ? "<ele>" . $Dat{'ele'}*1.0 . "</ele> "
162 : "",
163 (length($Dat{'desc'}))
164 ? sprintf("<desc>%s</desc> ",
165 $Dat{'desc'})
166 : ""
168 length($Retval) &&
169 ($Retval = sprintf("<%s%s> %s</%s>\n",
170 $Elem,
171 length($err_str) ? " err=\"$err_str\"" : "",
172 $Retval,
173 $Elem)
175 return($Retval);
176 # }}}
177 } # gen_gpsml_entry()
179 sub gen_pgtab_entry {
180 # {{{
181 my %Dat = @_;
182 my $Retval = join("\t",
183 $Dat{'year'}
184 ? "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
185 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
186 : '\N', # date
187 (length($Dat{'lat'}) && length($Dat{'lon'}))
188 ? "($Dat{'lat'},$Dat{'lon'})"
189 : '\N', # coor
190 length($Dat{'ele'}) ? $Dat{'ele'} : '\N', # ele
191 '\N', # name
192 '\N', # dist
193 '\N' # description
194 ) . "\n";
195 return($Retval);
196 # }}}
197 } # gen_pgtab_entry()
199 sub postgresql_copy_safe {
200 # {{{
201 my $Str = shift;
202 $Str =~ s/\\/\\\\/gs;
203 $Str =~ s/\n/\\n/gs;
204 $Str =~ s/\r/\\r/gs;
205 $Str =~ s/\t/\\t/gs;
206 return($Str);
207 # }}}