Deleting Patch/xgraph-fix_plus_more.patch, have put it on its own branch
[gpstools.git] / branches / gpst.gpsman-format / GPST.pm
blobb86dd9fd315d1b45f45e71fa1214f366d0ec739e
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;
16 use GPSTxml;
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 &num_expand);
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{'type'}) || 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{'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 $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', # sted
156 '\N', # dist
157 '\N', # description
158 '\N' # avst
159 ) . "\n";
160 } elsif ($Dat{'format'} eq "gpstrans") {
161 # {{{
162 my ($gpt_lat, $gpt_lon) =
163 (ddd_to_dms($Dat{'lat'}), ddd_to_dms($Dat{'lon'}));
164 if ($print_time) {
165 $Retval .= "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
166 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
167 "$gpt_lat\t$gpt_lon\n";
168 } else {
169 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
171 # }}}
172 } else {
173 $Retval = undef;
175 } elsif ($Dat{'type'} eq 'title') {
176 if ($Dat{'format'} eq "gpsml") {
177 $Retval = sprintf("<title>%s</title> ", txt_to_xml($Dat{'title'}));
179 } else {
180 $Retval = undef;
182 return $Retval;
183 # }}}
186 sub postgresql_copy_safe {
187 # {{{
188 my $Str = shift;
189 $Str =~ s/\\/\\\\/gs;
190 $Str =~ s/\n/\\n/gs;
191 $Str =~ s/\r/\\r/gs;
192 $Str =~ s/\t/\\t/gs;
193 return($Str);
194 # }}}
197 sub num_expand {
198 # {{{
199 my $n = shift;
200 my $Retval;
201 return $n unless $n =~ /^(.*)e([-+]?)(.*)$/;
202 my ($num, $sign, $exp) = ($1, $2, $3);
203 my $sig = $sign eq '-' ? "." . ($exp - 1 + length $num) : '';
204 $Retval = sprintf("%${sig}f", $n);
205 D("num_expand('$n') returns '$Retval'");
206 return($Retval);
207 # }}}