gpsfold: Remove $Id$ at EOF.
[gpstools.git] / branches / gpst.gpxfix / GPST.pm
blobbcc6729579ded77e0a44c260db4600471c08e59b
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 @wpt_elems);
28 %EXPORT_TAGS = ();
30 our @EXPORT_OK;
32 our @wpt_elems = (
33 # Position info
34 "ele", "time", "magvar", "geoidheight",
35 # Description info
36 "name", "cmt", "desc", "src", "link", "sym", "type",
37 # Accuracy info
38 "fix", "sat", "hdop", "vdop", "pdop",
39 "ageofdgpsdata", "dgpsid",
40 # Extensions
41 "extensions"
44 our $Spc = " ";
46 sub trackpoint {
47 # Receive a hash and return a trackpoint as a string {{{
48 my %Dat = @_;
50 defined($Dat{'what'}) || return(undef);
51 defined($Dat{'format'}) || return(undef);
52 defined($Dat{'error'}) || return(undef);
54 defined($Dat{'year'}) || ($Dat{'year'} = 0);
55 defined($Dat{'month'}) || ($Dat{'month'} = 0);
56 defined($Dat{'day'}) || ($Dat{'day'} = 0);
57 defined($Dat{'hour'}) || ($Dat{'hour'} = "");
58 defined($Dat{'min'}) || ($Dat{'min'} = "");
59 defined($Dat{'sec'}) || ($Dat{'sec'} = "");
60 my $print_time = (
61 !$Dat{'year'} ||
62 !$Dat{'month'} ||
63 !$Dat{'day'} ||
64 !length($Dat{'hour'}) ||
65 !length($Dat{'min'}) ||
66 !length($Dat{'sec'})
67 ) ? 0 : 1;
69 if (
70 ("$Dat{'year'}$Dat{'month'}$Dat{'day'}$Dat{'hour'}$Dat{'min'}" =~
71 /[^\d]/) || ($Dat{'sec'} =~ /[^\d\.]/)
72 ) {
73 ($print_time = 0);
75 "$Dat{'lat'}$Dat{'lon'}" =~ /[^\d\.\-\+]/ && return(undef);
77 defined($Dat{'lat'}) || ($Dat{'lat'} = "");
78 defined($Dat{'lon'}) || ($Dat{'lon'} = "");
79 defined($Dat{'ele'}) || ($Dat{'ele'} = "");
80 defined($Dat{'desc'}) || ($Dat{'desc'} = "");
81 defined($Dat{'extensions'}) || ($Dat{'extensions'} = "");
83 my $Retval = "";
85 if ($Dat{'what'} eq "tp") {
86 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
87 if ($Dat{'format'} eq "gpsml") {
88 # {{{
89 my $Elem = length($err_str) ? "etp" : "tp";
90 $Retval .= join("",
91 $print_time
92 ? sprintf("<time>%04u-%02u-%02uT" .
93 "%02u:%02u:%02gZ</time> ",
94 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
95 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
97 : "",
98 (length($Dat{'lat'}))
99 ? "<lat>" . $Dat{'lat'}*1.0 . "</lat> "
100 : "",
101 (length($Dat{'lon'}))
102 ? "<lon>" . $Dat{'lon'}*1.0 . "</lon> "
103 : "",
104 (length($Dat{'ele'}))
105 ? "<ele>" . $Dat{'ele'}*1.0 . "</ele> "
106 : "",
107 (length($Dat{'desc'}))
108 ? sprintf("<desc>%s</desc> ",
109 $Dat{'desc'})
110 : ""
112 length($Retval) &&
113 ($Retval = sprintf("<%s%s> %s</%s>\n",
114 $Elem,
115 length($err_str) ? " err=\"$err_str\"" : "",
116 $Retval,
117 $Elem)
119 # }}}
120 } elsif($Dat{'format'} eq "gpx") {
121 # {{{
122 my $lat_str = length($Dat{'lat'}) ? " lat=\"$Dat{'lat'}\"" : "";
123 my $lon_str = length($Dat{'lon'}) ? " lon=\"$Dat{'lon'}\"" : "";
124 my ($estr_begin, $estr_end) =
125 ( "", "");
126 if (length($err_str)) {
127 $estr_begin = "<!-- ";
128 $Dat{'extensions'} .= "<error>$err_str</error>";
129 $estr_end = " -->";
131 if (length("$lat_str$lon_str$Dat{'ele'}")) {
132 $Retval .=
133 join("",
134 "$Spc$Spc$Spc$Spc$Spc$Spc",
135 $estr_begin,
136 "<trkpt$lat_str$lon_str>",
137 "$Spc",
138 length($Dat{'ele'})
139 ? "<ele>$Dat{'ele'}</ele>$Spc"
140 : "",
141 $print_time
142 ? "<time>" .
143 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
144 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
145 "</time>$Spc"
146 : "",
147 length($Dat{'extensions'})
148 ? "<extensions>" .
149 $Spc .
150 $Dat{'extensions'} .
151 $Spc .
152 "</extensions>$Spc"
153 : "",
154 "</trkpt>$estr_end\n"
157 # }}}
158 } elsif($Dat{'format'} eq "clean") {
159 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
160 } elsif($Dat{'format'} eq "xgraph") {
161 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
162 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
164 } elsif ($Dat{'format'} eq "pgtab") {
165 $Retval .= join("\t",
166 $Dat{'year'}
167 ? "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
168 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
169 : '\N', # date
170 (length($Dat{'lat'}) && length($Dat{'lon'}))
171 ? "($Dat{'lat'},$Dat{'lon'})"
172 : '\N', # coor
173 length($Dat{'ele'}) ? $Dat{'ele'} : '\N', # ele
174 '\N', # name
175 '\N', # dist
176 '\N' # description
177 ) . "\n";
178 } elsif ($Dat{'format'} eq "gpstrans") {
179 # {{{
180 my ($gpt_lat, $gpt_lon) =
181 (ddd_to_dms($Dat{'lat'}), ddd_to_dms($Dat{'lon'}));
182 if ($print_time) {
183 $Retval .= "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
184 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
185 "$gpt_lat\t$gpt_lon\n";
186 } else {
187 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
189 # }}}
190 } else {
191 $Retval = undef;
193 } else {
194 $Retval = undef;
196 return $Retval;
197 # }}}
200 sub postgresql_copy_safe {
201 # {{{
202 my $Str = shift;
203 $Str =~ s/\\/\\\\/gs;
204 $Str =~ s/\n/\\n/gs;
205 $Str =~ s/\r/\\r/gs;
206 $Str =~ s/\t/\\t/gs;
207 return($Str);
208 # }}}