* /trunk/vg
[gpstools.git] / GPST.pm
blob49a5a85f750edcdd91b57faf55c5f1f11cb86138
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 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{'what'} 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 $Retval .= join("",
78 $print_time
79 ? sprintf("<time>%04u-%02u-%02uT" .
80 "%02u:%02u:%02gZ</time> ",
81 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
82 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
84 : "",
85 (length($Dat{'lat'}))
86 ? "<lat>" . $Dat{'lat'}*1.0 . "</lat> "
87 : "",
88 (length($Dat{'lon'}))
89 ? "<lon>" . $Dat{'lon'}*1.0 . "</lon> "
90 : "",
91 (length($Dat{'ele'}))
92 ? "<ele>" . $Dat{'ele'}*1.0 . "</ele> "
93 : "",
94 (length($Dat{'desc'}))
95 ? sprintf("<desc>%s</desc> ",
96 $Dat{'desc'})
97 : ""
99 length($Retval) &&
100 ($Retval = sprintf("<%s%s> %s</%s>\n",
101 $Elem,
102 length($err_str) ? " err=\"$err_str\"" : "",
103 $Retval,
104 $Elem)
106 # }}}
107 } elsif($Dat{'format'} eq "gpx") {
108 # {{{
109 my $lat_str = length($Dat{'lat'}) ? " lat=\"$Dat{'lat'}\"" : "";
110 my $lon_str = length($Dat{'lon'}) ? " lon=\"$Dat{'lon'}\"" : "";
111 my ($estr_begin, $estr_ext, $estr_end) =
112 ( "", "", "");
113 if (length($err_str)) {
114 $estr_begin = "<!-- ";
115 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
116 $estr_end = " -->";
118 if (length("$lat_str$lon_str$Dat{'ele'}")) {
119 $Retval .=
120 join("",
121 "$Spc$Spc$Spc$Spc$Spc$Spc",
122 $estr_begin,
123 "<trkpt$lat_str$lon_str>",
124 "$Spc",
125 length($Dat{'ele'})
126 ? "<ele>$Dat{'ele'}</ele>$Spc"
127 : "",
128 $print_time
129 ? "<time>" .
130 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
131 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
132 "</time>$Spc"
133 : "",
134 $estr_ext,
135 "</trkpt>$estr_end\n"
138 # }}}
139 } elsif($Dat{'format'} eq "clean") {
140 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
141 } elsif($Dat{'format'} eq "xgraph") {
142 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
143 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
145 } elsif ($Dat{'format'} eq "pgtab") {
146 $Retval .= join("\t",
147 $Dat{'year'}
148 ? "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
149 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
150 : '\N', # date
151 (length($Dat{'lat'}) && length($Dat{'lon'}))
152 ? "($Dat{'lat'},$Dat{'lon'})"
153 : '\N', # coor
154 length($Dat{'ele'}) ? $Dat{'ele'} : '\N', # ele
155 '\N', # name
156 '\N', # dist
157 '\N' # description
158 ) . "\n";
159 } elsif ($Dat{'format'} eq "gpstrans") {
160 # {{{
161 my ($gpt_lat, $gpt_lon) =
162 (ddd_to_dms($Dat{'lat'}), ddd_to_dms($Dat{'lon'}));
163 if ($print_time) {
164 $Retval .= "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
165 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
166 "$gpt_lat\t$gpt_lon\n";
167 } else {
168 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
170 # }}}
171 } else {
172 $Retval = undef;
174 } else {
175 $Retval = undef;
177 return $Retval;
178 # }}}
181 sub postgresql_copy_safe {
182 # {{{
183 my $Str = shift;
184 $Str =~ s/\\/\\\\/gs;
185 $Str =~ s/\n/\\n/gs;
186 $Str =~ s/\r/\\r/gs;
187 $Str =~ s/\t/\\t/gs;
188 return($Str);
189 # }}}