3 #=======================================================================
5 # File ID: 5e0437a0-fafa-11dd-abd7-000475e441b9
8 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
9 # License: GNU General Public License, see end of file for legal stuff.
10 #=======================================================================
20 our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
23 @EXPORT = qw(&trackpoint &postgresql_copy_safe);
31 # Receive a hash and return a trackpoint as a string {{{
34 defined($Dat{'what'}) || return(undef);
35 defined($Dat{'format'}) || return(undef);
36 defined($Dat{'error'}) || return(undef);
38 defined($Dat{'year'}) || ($Dat{'year'} = 0);
39 defined($Dat{'month'}) || ($Dat{'month'} = 0);
40 defined($Dat{'day'}) || ($Dat{'day'} = 0);
41 defined($Dat{'hour'}) || ($Dat{'hour'} = "");
42 defined($Dat{'min'}) || ($Dat{'min'} = "");
43 defined($Dat{'sec'}) || ($Dat{'sec'} = "");
44 $Dat{'print_time'} = (
48 !length($Dat{'hour'}) ||
49 !length($Dat{'min'}) ||
54 ("$Dat{'year'}$Dat{'month'}$Dat{'day'}$Dat{'hour'}$Dat{'min'}" =~
55 /[^\d]/) || ($Dat{'sec'} =~ /[^\d\.]/)
57 ($Dat{'print_time'} = 0);
59 "$Dat{'lat'}$Dat{'lon'}" =~ /[^\d\.\-\+]/ && return(undef);
61 defined($Dat{'lat'}) || ($Dat{'lat'} = "");
62 defined($Dat{'lon'}) || ($Dat{'lon'} = "");
63 defined($Dat{'ele'}) || ($Dat{'ele'} = "");
64 defined($Dat{'desc'}) || ($Dat{'desc'} = "");
68 if ($Dat{'what'} eq "tp") {
69 if ($Dat{'format'} eq "gpsml") {
70 $Retval .= gen_gpsml_entry
(%Dat);
71 } elsif($Dat{'format'} eq "gpx") {
72 $Retval .= gen_gpx_entry
(%Dat);
73 } elsif($Dat{'format'} eq "clean") {
74 $Retval .= "$Dat{'lon'}\t$Dat{'lat'}\t$Dat{'ele'}\n";
75 } elsif($Dat{'format'} eq "xgraph") {
76 if (length($Dat{'lat'}) && length($Dat{'lon'})) {
77 $Retval .= "$Dat{'lon'} $Dat{'lat'}\n";
79 } elsif ($Dat{'format'} eq "pgtab") {
80 $Retval .= gen_pgtab_entry
(%Dat);
81 } elsif ($Dat{'format'} eq "gpstrans") {
83 my ($gpt_lat, $gpt_lon) =
84 (ddd_to_dms
($Dat{'lat'}), ddd_to_dms
($Dat{'lon'}));
85 if ($Dat{'print_time'}) {
86 $Retval .= "T\t$Dat{'month'}/$Dat{'day'}/$Dat{'year'} " .
87 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}\t" .
88 "$gpt_lat\t$gpt_lon\n";
90 $Retval .= "T\t00/00/00 00:00:00\t$gpt_lat\t$gpt_lon\n";
107 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
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"
141 sub gen_gpsml_entry
{
144 my $err_str = length($Dat{'error'}) ?
$Dat{'error'} : "";
145 my $Elem = length($err_str) ?
"etp" : "tp";
146 my $Retval = join("",
148 ?
sprintf("<time>%04u-%02u-%02uT" .
149 "%02u:%02u:%02gZ</time> ",
150 $Dat{'year'}, $Dat{'month'}, $Dat{'day'},
151 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}*1.0
154 (length($Dat{'lat'}))
155 ?
"<lat>" . $Dat{'lat'}*1.0 . "</lat> "
157 (length($Dat{'lon'}))
158 ?
"<lon>" . $Dat{'lon'}*1.0 . "</lon> "
160 (length($Dat{'ele'}))
161 ?
"<ele>" . $Dat{'ele'}*1.0 . "</ele> "
163 (length($Dat{'desc'}))
164 ?
sprintf("<desc>%s</desc> ",
169 ($Retval = sprintf("<%s%s> %s</%s>\n",
171 length($err_str) ?
" err=\"$err_str\"" : "",
177 } # gen_gpsml_entry()
179 sub gen_pgtab_entry
{
182 my $Retval = join("\t",
184 ?
"$Dat{'year'}-$Dat{'month'}-$Dat{'day'}T" .
185 "$Dat{'hour'}:$Dat{'min'}:$Dat{'sec'}Z"
187 (length($Dat{'lat'}) && length($Dat{'lon'}))
188 ?
"($Dat{'lat'},$Dat{'lon'})"
190 length($Dat{'ele'}) ?
$Dat{'ele'} : '\N', # ele
197 } # gen_pgtab_entry()
199 sub postgresql_copy_safe
{
202 $Str =~ s/\\/\\\\/gs;