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