3 #=======================================================================
7 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
8 # License: GNU General Public License, see end of file for legal stuff.
9 #=======================================================================
19 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
22 push(@main::version_array
, $rcs_id);
23 $VERSION = ($rcs_id =~ / (\d+) /, $1);
26 @EXPORT = qw(&trackpoint &postgresql_copy_safe);
34 # Receive a hash and return a trackpoint as a string {{{
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'} = "");
52 !length($Dat{'hour'}) ||
53 !length($Dat{'min'}) ||
58 ("$Dat{'year'}$Dat{'month'}$Dat{'day'}$Dat{'hour'}$Dat{'min'}" =~
59 /[^\d]/) || ($Dat{'sec'} =~ /[^\d\.]/)
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'} = "");
72 if ($Dat{'type'} eq "tp") {
73 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
74 if ($Dat{'format'} eq "gpsml") {
76 my $Elem = length($err_str) ?
"etp" : "tp";
78 $Dat{'break'} && ($Retval .= "<break/>\n");
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
88 ?
"<lat>" . $Dat{'lat'}*1.0 . "</lat> "
91 ?
"<lon>" . $Dat{'lon'}*1.0 . "</lon> "
94 ?
"<ele>" . $Dat{'ele'}*1.0 . "</ele> "
96 (length($Dat{'desc'}))
97 ?
sprintf("<desc>%s</desc> ",
101 if (length($tp_str)) {
102 ($tp_str = sprintf("<%s%s> %s</%s>\n",
104 length($err_str) ?
" err=\"$err_str\"" : "",
113 } elsif($Dat{'format'} eq "gpx") {
116 my $lat_str = length($Dat{'lat'}) ?
" lat=\"$Dat{'lat'}\"" : "";
117 my $lon_str = length($Dat{'lon'}) ?
" lon=\"$Dat{'lon'}\"" : "";
119 $tp_str .= "$Spc$Spc$Spc$Spc</trkseg>\n" .
120 "$Spc$Spc$Spc$Spc<trkseg>\n";
122 my ($estr_begin, $estr_ext, $estr_end) =
124 if (length($err_str)) {
125 $estr_begin = "<!-- ";
126 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
129 if (length("$lat_str$lon_str$Dat{'ele'}")) {
132 "$Spc$Spc$Spc$Spc$Spc$Spc",
134 "<trkpt$lat_str$lon_str>",
137 ?
"<ele>$Dat{'ele'}</ele>$Spc"
141 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
142 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
146 "</trkpt>$estr_end\n"
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",
160 ?
"$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
161 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
163 (length($Dat{'lat'}) && length($Dat{'lon'}))
164 ?
"($Dat{'lat'},$Dat{'lon'})"
166 length($Dat{'ele'}) ?
$Dat{'ele'} : '\N', # ele
172 } elsif ($Dat{'format'} eq "gpstrans") {
174 my ($gpt_lat, $gpt_lon) =
175 (ddd_to_dms
($Dat{'lat'}), ddd_to_dms
($Dat{'lon'}));
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";
181 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
194 sub postgresql_copy_safe
{
197 $Str =~ s/\\/\\\\/gs;