* /trunk/src/gpstools/GPST.pm
[gpstools.git] / GPST.pm
blob62d08bd31c8e70d9389ab5867d6ea1cb92b45f60
1 package GPST;
3 #=======================================================================
4 # $Id$
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 ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
22 my $rcs_id = '$Id$';
23 push(@main::version_array, $rcs_id);
24 $VERSION = ($rcs_id =~ / (\d+) /, $1);
26 @ISA = qw(Exporter);
27 @EXPORT = qw(&trackpoint &postgresql_copy_safe);
28 %EXPORT_TAGS = ();
30 our @EXPORT_OK;
32 our $Spc = " ";
34 sub trackpoint {
35 # Receive a hash and return a trackpoint as a string {{{
36 my %Dat = @_;
38 defined($Dat{'what'}) || return(undef);
39 defined($Dat{'format'}) || return(undef);
40 defined($Dat{'error'}) || return(undef);
42 defined($Dat{'year'}) || ($Dat{'year'} = 0);
43 defined($Dat{'month'}) || ($Dat{'month'} = 0);
44 defined($Dat{'day'}) || ($Dat{'day'} = 0);
45 defined($Dat{'hour'}) || ($Dat{'hour'} = "");
46 defined($Dat{'min'}) || ($Dat{'min'} = "");
47 defined($Dat{'sec'}) || ($Dat{'sec'} = "");
48 $Dat{'print_time'} = (
49 !$Dat{'year'} ||
50 !$Dat{'month'} ||
51 !$Dat{'day'} ||
52 !length($Dat{'hour'}) ||
53 !length($Dat{'min'}) ||
54 !length($Dat{'sec'})
55 ) ? 0 : 1;
57 if (
58 ("$Dat{'year'}$Dat{'month'}$Dat{'day'}$Dat{'hour'}$Dat{'min'}" =~
59 /[^\d]/) || ($Dat{'sec'} =~ /[^\d\.]/)
60 ) {
61 ($Dat{'print_time'} = 0);
63 "$Dat{'lat'}$Dat{'lon'}" =~ /[^\d\.\-\+]/ && return(undef);
65 defined($Dat{'lat'}) || ($Dat{'lat'} = "");
66 defined($Dat{'lon'}) || ($Dat{'lon'} = "");
67 defined($Dat{'ele'}) || ($Dat{'ele'} = "");
68 defined($Dat{'desc'}) || ($Dat{'desc'} = "");
70 my $Retval = "";
72 if ($Dat{'what'} eq "tp") {
73 if ($Dat{'format'} eq "gpsml") {
74 $Retval .= gen_gpsml_entry(%Dat);
75 } elsif($Dat{'format'} eq "gpx") {
76 $Retval .= gen_gpx_entry(%Dat);
77 } elsif($Dat{'format'} eq "clean") {
78 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
79 } elsif($Dat{'format'} eq "xgraph") {
80 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
81 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
83 } elsif ($Dat{'format'} eq "pgtab") {
84 $Retval .= join("\t",
85 $Dat{'year'}
86 ? "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
87 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
88 : '\N', # date
89 (length($Dat{'lat'}) && length($Dat{'lon'}))
90 ? "($Dat{'lat'},$Dat{'lon'})"
91 : '\N', # coor
92 length($Dat{'ele'}) ? $Dat{'ele'} : '\N', # ele
93 '\N', # name
94 '\N', # dist
95 '\N' # description
96 ) . "\n";
97 } elsif ($Dat{'format'} eq "gpstrans") {
98 # {{{
99 my ($gpt_lat, $gpt_lon) =
100 (ddd_to_dms($Dat{'lat'}), ddd_to_dms($Dat{'lon'}));
101 if ($Dat{'print_time'}) {
102 $Retval .= "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
103 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
104 "$gpt_lat\t$gpt_lon\n";
105 } else {
106 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
108 # }}}
109 } else {
110 $Retval = undef;
112 } else {
113 $Retval = undef;
115 return $Retval;
116 # }}}
117 } # trackpoint()
119 sub gen_gpx_entry {
120 # {{{
121 my %Dat = @_;
122 my $Retval = "";
123 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
124 my $lat_str = length($Dat{'lat'}) ? " lat=\"$Dat{'lat'}\"" : "";
125 my $lon_str = length($Dat{'lon'}) ? " lon=\"$Dat{'lon'}\"" : "";
126 my ($estr_begin, $estr_ext, $estr_end) =
127 ( "", "", "");
128 if (length($err_str)) {
129 $estr_begin = "<!-- ";
130 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
131 $estr_end = " -->";
133 if (length("$lat_str$lon_str$Dat{'ele'}")) {
134 $Retval .=
135 join("",
136 "$Spc$Spc$Spc$Spc$Spc$Spc",
137 $estr_begin,
138 "<trkpt$lat_str$lon_str>",
139 "$Spc",
140 length($Dat{'ele'})
141 ? "<ele>$Dat{'ele'}</ele>$Spc"
142 : "",
143 $Dat{'print_time'}
144 ? "<time>" .
145 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
146 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
147 "</time>$Spc"
148 : "",
149 $estr_ext,
150 "</trkpt>$estr_end\n"
153 return($Retval);
154 # }}}
155 } # gen_gpx_entry()
157 sub gen_gpsml_entry {
158 # {{{
159 my %Dat = @_;
160 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
161 my $Elem = length($err_str) ? "etp" : "tp";
162 my $Retval = join("",
163 $Dat{'print_time'}
164 ? sprintf("<time>%04u-%02u-%02uT" .
165 "%02u:%02u:%02gZ</time> ",
166 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
167 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
169 : "",
170 (length($Dat{'lat'}))
171 ? "<lat>" . $Dat{'lat'}*1.0 . "</lat> "
172 : "",
173 (length($Dat{'lon'}))
174 ? "<lon>" . $Dat{'lon'}*1.0 . "</lon> "
175 : "",
176 (length($Dat{'ele'}))
177 ? "<ele>" . $Dat{'ele'}*1.0 . "</ele> "
178 : "",
179 (length($Dat{'desc'}))
180 ? sprintf("<desc>%s</desc> ",
181 $Dat{'desc'})
182 : ""
184 length($Retval) &&
185 ($Retval = sprintf("<%s%s> %s</%s>\n",
186 $Elem,
187 length($err_str) ? " err=\"$err_str\"" : "",
188 $Retval,
189 $Elem)
191 return($Retval);
192 # }}}
193 } # gen_gpsml_entry()
195 sub postgresql_copy_safe {
196 # {{{
197 my $Str = shift;
198 $Str =~ s/\\/\\\\/gs;
199 $Str =~ s/\n/\\n/gs;
200 $Str =~ s/\r/\\r/gs;
201 $Str =~ s/\t/\\t/gs;
202 return($Str);
203 # }}}