Remove forgotten $rcs_id variable after the svn → git conversion
[gpstools.git] / branches / gpst.postgis / GPST.pm
blobd2e98633519ea3620296ba951d5468a018c199e4
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{'what'}) || return(undef);
38 defined($Dat{'format'}) || return(undef);
39 defined($Dat{'error'}) || return(undef);
41 defined($Dat{'year'}) || ($Dat{'year'} = 0);
42 defined($Dat{'month'}) || ($Dat{'month'} = 0);
43 defined($Dat{'day'}) || ($Dat{'day'} = 0);
44 defined($Dat{'hour'}) || ($Dat{'hour'} = "");
45 defined($Dat{'min'}) || ($Dat{'min'} = "");
46 defined($Dat{'sec'}) || ($Dat{'sec'} = "");
47 my $print_time = (
48 !$Dat{'year'} ||
49 !$Dat{'month'} ||
50 !$Dat{'day'} ||
51 !length($Dat{'hour'}) ||
52 !length($Dat{'min'}) ||
53 !length($Dat{'sec'})
54 ) ? 0 : 1;
56 if (
57 ("$Dat{'year'}$Dat{'month'}$Dat{'day'}$Dat{'hour'}$Dat{'min'}" =~
58 /[^\d]/) || ($Dat{'sec'} =~ /[^\d\.]/)
59 ) {
60 ($print_time = 0);
62 "$Dat{'lat'}$Dat{'lon'}" =~ /[^\d\.\-\+]/ && return(undef);
64 defined($Dat{'lat'}) || ($Dat{'lat'} = "");
65 defined($Dat{'lon'}) || ($Dat{'lon'} = "");
66 defined($Dat{'ele'}) || ($Dat{'ele'} = "");
67 defined($Dat{'desc'}) || ($Dat{'desc'} = "");
69 my $Retval = "";
71 if ($Dat{'what'} eq "tp") {
72 my $err_str = length($Dat{'error'}) ? $Dat{'error'} : "";
73 if ($Dat{'format'} eq "gpsml") {
74 # {{{
75 my $Elem = length($err_str) ? "etp" : "tp";
76 $Retval .= join("",
77 $print_time
78 ? sprintf("<time>%04u-%02u-%02uT" .
79 "%02u:%02u:%02gZ</time> ",
80 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
81 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
83 : "",
84 (length($Dat{'lat'}))
85 ? "<lat>" . $Dat{'lat'}*1.0 . "</lat> "
86 : "",
87 (length($Dat{'lon'}))
88 ? "<lon>" . $Dat{'lon'}*1.0 . "</lon> "
89 : "",
90 (length($Dat{'ele'}))
91 ? "<ele>" . $Dat{'ele'}*1.0 . "</ele> "
92 : "",
93 (length($Dat{'desc'}))
94 ? sprintf("<desc>%s</desc> ",
95 $Dat{'desc'})
96 : ""
98 length($Retval) &&
99 ($Retval = sprintf("<%s%s> %s</%s>\n",
100 $Elem,
101 length($err_str) ? " err=\"$err_str\"" : "",
102 $Retval,
103 $Elem)
105 # }}}
106 } elsif($Dat{'format'} eq "gpx") {
107 # {{{
108 my $lat_str = length($Dat{'lat'}) ? " lat=\"$Dat{'lat'}\"" : "";
109 my $lon_str = length($Dat{'lon'}) ? " lon=\"$Dat{'lon'}\"" : "";
110 my ($estr_begin, $estr_ext, $estr_end) =
111 ( "", "", "");
112 if (length($err_str)) {
113 $estr_begin = "<!-- ";
114 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
115 $estr_end = " -->";
117 if (length("$lat_str$lon_str$Dat{'ele'}")) {
118 $Retval .=
119 join("",
120 "$Spc$Spc$Spc$Spc$Spc$Spc",
121 $estr_begin,
122 "<trkpt$lat_str$lon_str>",
123 "$Spc",
124 length($Dat{'ele'})
125 ? "<ele>$Dat{'ele'}</ele>$Spc"
126 : "",
127 $print_time
128 ? "<time>" .
129 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
130 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
131 "</time>$Spc"
132 : "",
133 $estr_ext,
134 "</trkpt>$estr_end\n"
137 # }}}
138 } elsif($Dat{'format'} eq "clean") {
139 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
140 } elsif($Dat{'format'} eq "xgraph") {
141 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
142 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
144 } elsif ($Dat{'format'} eq "pgtab") {
145 $Retval .= join("\t",
146 $Dat{'year'}
147 ? "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
148 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
149 : '\N', # date
150 (length($Dat{'lat'}) && length($Dat{'lon'}))
151 ? "($Dat{'lat'},$Dat{'lon'})"
152 : '\N', # coor
153 length($Dat{'ele'}) ? $Dat{'ele'} : '\N', # ele
154 '\N', # sted
155 '\N', # dist
156 '\N', # description
157 '\N' # avst
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 # }}}