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{'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'} = "");
51 !length($Dat{'hour'}) ||
52 !length($Dat{'min'}) ||
57 ("$Dat{'year'}$Dat{'month'}$Dat{'day'}$Dat{'hour'}$Dat{'min'}" =~
58 /[^\d]/) || ($Dat{'sec'} =~ /[^\d\.]/)
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'} = "");
71 if ($Dat{'what'} eq "tp") {
72 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
73 if ($Dat{'format'} eq "gpsml") {
75 my $Elem = length($err_str) ?
"etp" : "tp";
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
85 ?
"<lat>" . $Dat{'lat'}*1.0 . "</lat> "
88 ?
"<lon>" . $Dat{'lon'}*1.0 . "</lon> "
91 ?
"<ele>" . $Dat{'ele'}*1.0 . "</ele> "
93 (length($Dat{'desc'}))
94 ?
sprintf("<desc>%s</desc> ",
99 ($Retval = sprintf("<%s%s> %s</%s>\n",
101 length($err_str) ?
" err=\"$err_str\"" : "",
106 } elsif($Dat{'format'} eq "gpx") {
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) =
112 if (length($err_str)) {
113 $estr_begin = "<!-- ";
114 $estr_ext = "<extensions>$Spc<error>$err_str</error>$Spc</extensions>$Spc";
117 if (length("$lat_str$lon_str$Dat{'ele'}")) {
120 "$Spc$Spc$Spc$Spc$Spc$Spc",
122 "<trkpt$lat_str$lon_str>",
125 ?
"<ele>$Dat{'ele'}</ele>$Spc"
129 "$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
130 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z" .
134 "</trkpt>$estr_end\n"
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",
147 ?
"$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
148 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
150 (length($Dat{'lat'}) && length($Dat{'lon'}))
151 ?
"($Dat{'lat'},$Dat{'lon'})"
153 length($Dat{'ele'}) ?
$Dat{'ele'} : '\N', # ele
159 } elsif ($Dat{'format'} eq "gpstrans") {
161 my ($gpt_lat, $gpt_lon) =
162 (ddd_to_dms
($Dat{'lat'}), ddd_to_dms
($Dat{'lon'}));
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";
168 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
181 sub postgresql_copy_safe
{
184 $Str =~ s/\\/\\\\/gs;