Remove forgotten $rcs_id variable after the svn → git conversion
[gpstools.git] / branches / gpst-breakfix / GPST.pm
blob6cdc1a4bba057496cdc640c24fe7ade4aeefcba1
1 package GPST;
3 #=======================================================================
4 # $Id$
6 # Character set: UTF-8
7 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
8 # License: GNU General Public License, see end of file for legal stuff.
9 #=======================================================================
11 use strict;
12 use warnings;
14 use GPSTdebug;
15 use GPSTgeo;
17 BEGIN {
18 use Exporter ();
19 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
21 my $rcs_id = '$Id$';
22 push(@main::version_array, $rcs_id);
23 $VERSION = ($rcs_id =~ / (\d+) /, $1);
25 @ISA = qw(Exporter);
26 @EXPORT = qw(&trackpoint &postgresql_copy_safe);
27 %EXPORT_TAGS = ();
29 our @EXPORT_OK;
31 our $Spc = " ";
33 sub trackpoint {
34 # Receive a hash and return a trackpoint as a string {{{
35 my %Dat = @_;
37 defined($Dat{'type'}) || return(undef);
38 defined($Dat{'format'}) || return(undef);
39 defined($Dat{'error'}) || return(undef);
40 defined($Dat{'break'}) || ($Dat{'break'} = 0);
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 my $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 ($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{'type'} eq "tp") {
73 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
74 if ($Dat{'format'} eq "gpsml") {
75 # {{{
76 my $Elem = length($err_str) ? "etp" : "tp";
77 my $tp_str = "";
78 $Dat{'break'} && ($Retval .= "<break/>\n");
79 $tp_str .= join("",
80 $print_time
81 ? sprintf("<time>%04u-%02u-%02uT" .
82 "%02u:%02u:%02gZ</time> ",
83 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
84 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
86 : "",
87 (length($Dat{'lat'}))
88 ? "<lat>" . $Dat{'lat'}*1.0 . "</lat> "
89 : "",
90 (length($Dat{'lon'}))
91 ? "<lon>" . $Dat{'lon'}*1.0 . "</lon> "
92 : "",
93 (length($Dat{'ele'}))
94 ? "<ele>" . $Dat{'ele'}*1.0 . "</ele> "
95 : "",
96 (length($Dat{'desc'}))
97 ? sprintf("<desc>%s</desc> ",
98 $Dat{'desc'})
99 : ""
101 if (length($tp_str)) {
102 ($tp_str = sprintf("<%s%s> %s</%s>\n",
103 $Elem,
104 length($err_str) ? " err=\"$err_str\"" : "",
105 $tp_str,
106 $Elem
110 $Retval .= $tp_str;
111 $Dat{'break'} = 0;
112 # }}}
113 } elsif($Dat{'format'} eq "gpx") {
114 # {{{
115 my $tp_str = "";
116 my $lat_str = length($Dat{'lat'}) ? " lat=\"$Dat{'lat'}\"" : "";
117 my $lon_str = length($Dat{'lon'}) ? " lon=\"$Dat{'lon'}\"" : "";
118 if ($Dat{'break'}) {
119 $tp_str .= "$Spc$Spc$Spc$Spc</trkseg>\n" .
120 "$Spc$Spc$Spc$Spc<trkseg>\n";
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 $tp_str .=
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 $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 $Retval .= $tp_str;
150 # }}}
151 } elsif($Dat{'format'} eq "clean") {
152 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
153 } elsif($Dat{'format'} eq "xgraph") {
154 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
155 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
157 } elsif ($Dat{'format'} eq "pgtab") {
158 $Retval .= join("\t",
159 $Dat{'year'}
160 ? "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
161 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
162 : '\N', # date
163 (length($Dat{'lat'}) && length($Dat{'lon'}))
164 ? "($Dat{'lat'},$Dat{'lon'})"
165 : '\N', # coor
166 length($Dat{'ele'}) ? $Dat{'ele'} : '\N', # ele
167 '\N', # sted
168 '\N', # dist
169 '\N', # description
170 '\N' # avst
171 ) . "\n";
172 } elsif ($Dat{'format'} eq "gpstrans") {
173 # {{{
174 my ($gpt_lat, $gpt_lon) =
175 (ddd_to_dms($Dat{'lat'}), ddd_to_dms($Dat{'lon'}));
176 if ($print_time) {
177 $Retval .= "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
178 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
179 "$gpt_lat\t$gpt_lon\n";
180 } else {
181 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
183 # }}}
184 } else {
185 $Retval = undef;
187 } else {
188 $Retval = undef;
190 return $Retval;
191 # }}}
194 sub postgresql_copy_safe {
195 # {{{
196 my $Str = shift;
197 $Str =~ s/\\/\\\\/gs;
198 $Str =~ s/\n/\\n/gs;
199 $Str =~ s/\r/\\r/gs;
200 $Str =~ s/\t/\\t/gs;
201 return($Str);
202 # }}}