3 #=======================================================================
5 # Converts between various GPS formats
8 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
9 # License: GNU General Public License version 2 or later, see end of
10 # file for legal stuff.
11 #=======================================================================
19 use Time
::Local qw
{ timegm_nocheck
};
22 push(@INC, "$ENV{'HOME'}/bin/src/gpstools");
37 # Initial values for command line arguments {{{
41 'double-y-scale' => 0,
48 'output-format' => "gpsml",
54 'save-to-file' => "\n", # \n = undefined, it’s banned in filenames anyway.
57 'strip-whitespace' => 0,
65 $progname =~ s/^.*\/(.*?)$/$1/;
68 my $id_date = $rcs_id;
69 $id_date =~ s/^.*?\d+ (\d\d\d\d-.*?\d\d:\d\d:\d\d\S+).*/$1/;
71 push(@main::version_array
, $rcs_id);
73 Getopt
::Long
::Configure
("bundling");
75 # Command line options {{{
76 "chronology" => \
$Opt{'chronology'},
77 "create-breaks|t" => \
$Opt{'create-breaks'},
78 "debug" => \
$Opt{'debug'},
79 "double-y-scale|y" => \
$Opt{'double-y-scale'},
80 "epoch|e" => \
$Opt{'epoch'},
81 "fix" => \
$Opt{'fix'},
82 "from-date=s" => \
$Opt{'from-date'},
83 "help|h" => \
$Opt{'help'},
84 "inside" => \
$Opt{'inside'},
85 "near" => \
$Opt{'near'},
86 "output-format|o=s" => \
$Opt{'output-format'},
87 "outside" => \
$Opt{'outside'},
88 "pos1=s" => \
$Opt{'pos1'},
89 "pos2=s" => \
$Opt{'pos2'},
90 "require|r=s" => \
$Opt{'require'},
91 "round|R=s" => \
$Opt{'round'},
92 "save-to-file|S=s" => \
$Opt{'save-to-file'},
93 "short-date|s" => \
$Opt{'short-date'},
94 "skip-dups|d" => \
$Opt{'skip-dups'},
95 "strip-whitespace|w" => \
$Opt{'strip-whitespace'},
96 "undefined|n=s" => \
$Opt{'undefined'},
97 "verbose|v+" => \
$Opt{'verbose'},
98 "version" => \
$Opt{'version'},
100 ) || die("$progname: Option error. Use -h for help.\n");
104 my $PAUSE_LIMIT = 2 * 60; # Antall sekunder mellom to punkter det må til før en move legges inn.
106 my $DIGIT = '[0-9\.\-\+]'; # Used in regexps
107 $GPST::Spc
= $Opt{'strip-whitespace'} ?
"" : " ";
108 my $Spc = $GPST::Spc
; # FIXME
109 my $found_move = 0; # Settes til 1 hvis en /^# move$/ blir funnet.
112 my ($last_lon, $last_lat, $last_line) =
113 ( 1000, 1000, ""); # Vi kan jo teoretisk sett være i Greenwich eller på ekvator
114 my ($lat1, $lon1, $lat2, $lon2) =
115 (-1000, -1000, 1000, 1000);
118 'gpsbabel' => '/usr/local/bin/gpsbabel',
123 if ($Opt{'output-format'} eq "pgtab") {
124 $Opt{'require'} .= "p";
127 'ele' => ($Opt{'require'} =~ /e/) ?
1 : 0,
128 'position' => ($Opt{'require'} =~ /p/) ?
1 : 0,
129 'time' => ($Opt{'require'} =~ /t/) ?
1 : 0,
131 $Opt{'require'} =~ /[^ept]/
132 && die("$0: Unknown flag in --require (-r) value\n");
134 $Opt{'debug'} && ($Debug = 1);
135 $Opt{'help'} && usage
(0);
136 if ($Opt{'version'}) {
141 if ($Opt{'pos1'} =~ /^($DIGIT+),($DIGIT+)$/) {
145 if ($Opt{'pos2'} =~ /^($DIGIT+),($DIGIT+)$/) {
160 if ($Opt{'epoch'} && $Opt{'short-date'}) {
161 die("$progname: Cannot mix the --epoch (-e) and --short-date (-s) options\n");
164 if ($Opt{'inside'} && $Opt{'outside'}) {
165 die("$progname: Cannot mix the --inside and --outside options\n");
168 # To avoid printing out extra "/> at the start of svg output:
169 my $svg_start_thing = "";
173 if (defined($Opt{'round'})) {
174 my $R = $Opt{'round'};
175 $R =~ s/([a-z]+)=(\d+)/($Round{$1}=$2, "")/eg;
178 length($Opt{'undefined'}) && ($Udef = $Opt{'undefined'});
180 $Opt{'save-to-file'} eq "\n" && print_header
(*STDOUT
);
187 my $from_stdin = scalar(@ARGV) ?
0 : 1;
189 $from_stdin && push(@ARGV, "-");
191 for $curr_file (@ARGV) {
192 # Scan through stdin or specified files and send every GPS entry to
195 print(STDERR
"$progname: Opening \"$curr_file\" for read\n") if $Opt{'verbose'};
196 if (open(CurrFP
, "<$curr_file")) {
201 'year' => '', 'month' => '', 'day' => '',
202 'hour' => '', 'min' => '', 'sec' => '',
205 'lat' => '', 'lon' => '',
212 $Opt{'epoch'} && ($Dat{'date-format'} = "epoch");
213 $Opt{'short-date'} && ($Dat{'date-format'} = "short");
215 if ($Opt{'save-to-file'} ne "\n") {
216 push(@first_lines, $_);
218 s/^# error // && ($Dat{'error'} = "error");
219 s/^# ?// && ($Dat{'error'} = "desc");
221 if (m
#^<(e?tp)\b(.*?)>(.*?)</(e?tp)>\s*$#) {
222 # gpsml — The main storage format {{{
223 my ($Elem, $Props, $Data) =
225 my $err_str = ($Props =~ /\berr="(.*?)"/) ?
$1 : "error";
226 $Elem eq "etp" && ($Dat{'error'} = $err_str);
228 $Data =~ m
#<time>(.*?)</time># && ($Time = $1);
230 (\d\d\d\d
)-?
(\d\d
)-?
(\d\d
)[T
](\d\d
):?
(\d\d
):?
([\d\
.]+?
)Z
232 ($Dat{'year'}, $Dat{'month'}, $Dat{'day'},
233 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}) =
238 $Data =~ m
#<lat>($DIGIT*?)</lat># && ($Dat{'lat'} = $1);
239 $Data =~ m
#<lon>($DIGIT*?)</lon># && ($Dat{'lon'} = $1);
240 $Data =~ m
#<ele>($DIGIT*?)</ele># && ($Dat{'ele'} = $1);
241 $Data =~ m
#<desc>(.*?)</desc># && ($Dat{'desc'} = $1);
244 } elsif (m
#^<break\b.*?/>#) {
246 } elsif (m
#^<(title|pause)\b.*?>(.*?)</(title|pause)>#) {
250 } elsif (m
#^<desc\b.*?>(.*$)#s) {
251 $Dat{'type'} = "desc";
253 until ($Txt =~ m
#</desc>#s) {
256 $Txt =~ s
#^(.*)(</desc>.*$)#$1#s;
261 $xml_data .= join("", <CurrFP
>);
262 if (!length($Opt{'output-format'})) {
263 $Opt{'output-format'} = "gpx";
264 print_header
(*STDOUT
);
266 read_xmlfile
($xml_data);
270 } elsif (m
#^(\d+)\t($DIGIT+)\t($DIGIT+)\t($DIGIT)#) {
271 # CSV format, epoch style {{{
272 my ($ep_time, $lon_val, $lat_val, $Alt) =
274 $Dat{'epoch'} = $ep_time;
275 ($Dat{'sec'}, $Dat{'min'}, $Dat{'hour'},
276 $Dat{'day'}, $Dat{'month'}, $Dat{'year'},
277 $Dat{'wday'}, $Dat{'yday'}) = gmtime($ep_time);
278 $Dat{'month'}++; # Urgh Ⅰ
279 $Dat{'year'} += 1900; # Urgh Ⅱ
284 (\d\d\d\d
)-?
(\d\d
)-?
(\d\d
)[T\
](\d\d
):?
(\d\d
):?
(\d\d
)Z?
\t
285 ($DIGIT+)\t($DIGIT+)\t($DIGIT)
288 # CSV format, human-readable date format {{{
289 ($Dat{'year'}, $Dat{'month'}, $Dat{'day'},
290 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'},
291 $Dat{'lon'}, $Dat{'lat'}, $Dat{'ele'}) =
297 } elsif (/^Trackpoint\t/) {
298 # Trackpoint\tN60.41630 E5.31675\t09.02.2006 20:24:37 (UTC)\t13.6 m\t\t93.9 m\t00:00:06\t56 kph\t123° true {{{
301 # N60.41630 E5.31675\t
302 # 09.02.2006 20:24:37 (UTC)\t
310 $Orig =~ s/[\r\n]+$//;
311 my ($Marker_f, $Position_f, $Time_f, $Alt_f, $Depth_f,
312 $Leglength_f, $Legtime_f, $Legspeed_f, $Legcourse_f) =
314 # Nødløsning for å unngå at variabler
316 "\t\t\t\t\t\t\t\t\t\t"
319 "Position_f=\"$Position_f\" \x7B\x7B\x7B\n",
320 "Time_f=\"$Time_f\"\n",
321 "Alt_f=\"$Alt_f\"\n",
322 "Depth_f=\"$Depth_f\"\n",
323 "Leglength_f=\"$Leglength_f\"\n",
324 "Legtime_f=\"$Legtime_f\"\n",
325 "Legspeed_f=\"$Legspeed_f\"\n",
326 "Legcourse_f=\"$Legcourse_f\" \x7D\x7D\x7D\n",
331 $Legtime_hour, $Legtime_min, $Legtime_sec,
332 $Legspeed, $Legspeed_unit,
334 ) = ("", "", "", "", "", "", "", "", "", "", "", "", "",
335 "", "", "", "", "", "", "", "", "", "");
336 ($Position_f =~ /^(N|S)([\d\.]+) (W|E)([\d\.]+)/) &&
337 ($NS = $1, $Dat{'lat'} = $2, $WE = $3, $Dat{'lon'} = $4);
338 ($Time_f =~ /^(\d+)\.(\d+)\.(\d+) (\d+):(\d+):(\d+) \((.+?)\)/) &&
339 ($Dat{'day'} = $1, $Dat{'month'} = $2, $Dat{'year'} = $3,
340 $Dat{'hour'} = $4, $Dat{'min'} = $5, $Dat{'sec'} = $6);
341 ($Alt_f =~ /^($DIGIT+) (.*?)/) &&
342 ($Dat{'ele'} = $1, $Alt_unit = $2);
343 D
("ele = \"$Dat{'ele'}\"");
344 ($NS eq "S") && ($Dat{'lat'} = 0-$Dat{'lat'});
345 ($WE eq "W") && ($Dat{'lon'} = 0-$Dat{'lon'});
346 # MapSource in win xp writes YYYY, but YY in win98se.
348 defined($Dat{'year'})
349 && $Dat{'year'} =~ /\d/
350 && $Dat{'year'} < 1900
351 ) && ($Dat{'year'} += 2000);
354 } elsif (/^Track\t(.*?)\t/) {
355 $Dat{'title'} = txt_to_xml
($1);
356 $Dat{'type'} = "title";
362 (\d\d
)/(\d\d)/(\d\d\d\d
)\
(\d\d
):(\d\d
):(\d\d
)\t
367 # T 09/01/2002 11:51:26 60°23'36.3" 5°
19'35.9" {{{
368 my ($lat_d, $lat_m, $lat_s, $lon_d, $lon_m, $lon_s);
369 ($Dat{'month
'}, $Dat{'day
'}, $Dat{'year
'},
370 $Dat{'hour
'}, $Dat{'min
'}, $Dat{'sec
'},
371 $lat_d, $lat_m, $lat_s,
372 $lon_d, $lon_m, $lon_s) =
377 my $Flat = defined($Round{'lat
'}) ? ".$Round{'lat
'}" : "";
378 my $Flon = defined($Round{'lon
'}) ? ".$Round{'lon
'}" : "";
379 $Dat{'lat
'} = sprintf("%${Flat}f",
380 1.0*($lat_d+($lat_m/60)+($lat_s/3600)));
381 $Dat{'lon
'} = sprintf("%${Flon}f",
382 1.0*$lon_d+($lon_m/60)+($lon_s/3600));
387 1\ (\S+)\ (\S+)\ (\S+)\ (\S+)\x20
388 (\d\d)/(\d\d)/(\d\d\d\d)\ (\d\d):(\d\d):(\d\d)
391 # 1 60.3938222 5.3238754 17.3 0 09/01/2002 14:18:23 {{{
392 ($Dat{'lat
'}, $Dat{'lon
'}, $Dat{'speed
'},
394 $Dat{'month
'}, $Dat{'day
'}, $Dat{'year
'},
395 $Dat{'hour
'}, $Dat{'min
'}, $Dat{'sec
'}) =
403 # @020721221336N6048353E00701826S015-00001E4859N1673U0000 {{{
413 (\d\d) # Latitude degree
414 (\d\d) # Latitude minute
415 (\d\d\d) # Latitude minute decimals
417 (\d\d\d) # Longitude degree
418 (\d\d) # Longitude minute
419 (\d\d\d) # Longitude minute degree
425 my ($NS, $EW, $lat_deg, $lat_degmin, $lat_mindec, $lon_deg,
426 $lon_degmin, $lon_mindec);
427 ($Dat{'year
'}, $Dat{'month
'}, $Dat{'day
'}, $Dat{'hour
'},
428 $Dat{'min
'}, $Dat{'sec
'}, $NS, $lat_deg,
429 $lat_degmin, $lat_mindec, $EW,
430 $lon_deg, $lon_degmin, $lon_mindec,
431 $Dat{'accur
'}, $Dat{'ele
'}, $Dat{'unknown
'}) =
432 ($2+2000, $3, $4, $5,
437 my $ep_time = timegm_nocheck(
438 $Dat{'sec
'}, $Dat{'min
'}, $Dat{'hour
'},
439 $Dat{'day
'}, $Dat{'month
'}-1, $Dat{'year
'}
441 $last_time = $ep_time;
442 my $Flat = defined($Round{'lat
'}) ? ".$Round{'lat
'}" : "";
443 my $Flon = defined($Round{'lon
'}) ? ".$Round{'lon
'}" : "";
444 my $tmp_lon = sprintf(
449 my $tmp_lat = sprintf("%${Flat}f",
453 ($NS eq "S") && ($tmp_lat = 0-$tmp_lat);
454 ($EW eq "W") && ($tmp_lon = 0-$tmp_lon);
455 $Dat{'lat
'} = $tmp_lat;
456 $Dat{'lon
'} = $tmp_lon;
459 } elsif (/^(@)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(_{42})/) {
460 # @020721221336__________________________________________ {{{
461 ($Dat{'year
'}, $Dat{'month
'}, $Dat{'day
'},
462 $Dat{'hour
'}, $Dat{'min
'}, $Dat{'sec
'}, $Dat{'rest
'}) =
465 $Dat{'error
'} = "nosignal";
468 } elsif (/^xmaplog /) {
471 ($Opt{'output
-format
'} eq "csv")
472 && ($Opt{'save
-to
-file
'} eq "\n")
474 } elsif (/^Pause: /) {
475 # NOP, is here to cope with old files I’ve lying around.
476 } elsif ($Dat{'error
'} eq "desc") {
478 if (defined($Comment)) {
479 $Comment =~ s/^\s*(.*?)\s*$/$1/;
480 if ($Opt{'output
-format
'} eq "gpsml") {
481 $Dat{'desc
'} = txt_to_xml($Comment);
482 $Dat{'type
'} = "desc";
487 $Opt{'verbose
'} && warn("Line $.: Unknown: \"$_\"\n");
492 warn("$progname: $curr_file: Cannot open file for read: $!\n");
497 print_footer(*STDOUT);
503 my $Txt = join("", @_);
504 $Txt =~ s/<!--(.*?)-->//gs;
505 $Txt =~ s#(<gpx\b.*?>.*?</gpx>)#print_gpx($1)#gse;
513 # D("print_xml_gps(\"$Orig\")\n");
514 $Str =~ s/<!--(.*?)-->//gs;
515 my $fromdate_str = "";
516 if ($Opt{'from
-date
'}) {
517 $fromdate_str = "date >= '$Opt{'from-date'}' AND ";
519 if ($Opt{'output
-format
'} =~ /^(pgwtab|pgwupd)$/) {
523 <wpt\b(.*?)>(.*?)</wpt>
528 my ($Lat, $Lon, $Name, $Ele, $Type, $Time, $Cmt, $Desc, $Src, $Sym) =
529 ('\N
', '\N
', '\N
', '\N
', '\N
', '\N
', '\N
', '\N
', '\N
', '\N
');
531 $attr_wpt =~ /.*lat="($DIGIT+?)"/s &&
532 ($Lat = postgresql_copy_safe($1));
533 $attr_wpt =~ /.*lon="($DIGIT+?)"/s &&
534 ($Lon = postgresql_copy_safe($1));
535 $el_wpt =~ /.*<name\b(.*?)>(.*?)<\/name>/s &&
536 ($Name = postgresql_copy_safe(xml_to_txt($2)));
537 $el_wpt =~ /.*<ele\b(.*?)>(.*?)<\/ele>/s &&
538 ($Ele = postgresql_copy_safe(xml_to_txt($2)));
539 $el_wpt =~ /.*<type\b(.*?)>(.*?)<\/type>/s &&
540 ($Type = postgresql_copy_safe(xml_to_txt($2)));
541 $el_wpt =~ /.*<time\b(.*?)>(.*?)<\/time>/s &&
542 ($Time = postgresql_copy_safe(xml_to_txt($2)));
543 $el_wpt =~ /.*<cmt\b(.*?)>(.*?)<\/cmt>/s &&
544 ($Cmt = postgresql_copy_safe(xml_to_txt($2)));
545 $el_wpt =~ /.*<desc\b(.*?)>(.*?)<\/desc>/s &&
546 ($Desc = postgresql_copy_safe(xml_to_txt($2)));
547 $el_wpt =~ /.*<src\b(.*?)>(.*?)<\/src>/s &&
548 ($Src = postgresql_copy_safe(xml_to_txt($2)));
549 $el_wpt =~ /.*<sym\b(.*?)>(.*?)<\/sym>/s &&
550 ($Sym = postgresql_copy_safe(xml_to_txt($2)));
552 if ($Opt{'output
-format
'} eq "pgwtab") {
566 } elsif ($Opt{'output
-format
'} eq "pgwupd") {
570 " UPDATE logg SET sted = clname(coor) " .
571 "WHERE $fromdate_str(point($Lat,$Lon) <-> coor) < 0.05;",
572 " UPDATE logg SET dist = cldist(coor) " .
573 "WHERE $fromdate_str(point($Lat,$Lon) <-> coor) < 0.05;",
584 <trk\b(.*?)>(.*?)</trk>
590 <name\b(.*?)>(.*?)</name>
593 $tmp_dat{'title
'} = $2;
594 $tmp_dat{'type
'} = "title";
595 $tmp_dat{'error
'} = "";
596 print_entry(%tmp_dat);
601 <trkseg\b(.*?)>(.*?)</trkseg>
607 <trkpt\b(.*?)>(.*?)</trkpt>
610 my ($attr_trkpt, $el_trkpt) =
613 'year
' => '', 'month
' => '', 'day
' => '',
614 'hour
' => '', 'min
' => '', 'sec
' => '',
617 'lat
' => '', 'lon
' => '',
623 ($attr_trkpt =~ /\blon="(.*?)"/) && ($Dat{'lon
'} = $1);
624 ($attr_trkpt =~ /\blat="(.*?)"/) && ($Dat{'lat
'} = $1);
625 ($el_trkpt =~ m#<ele\b.*?>(.*?)</ele>#) && ($Dat{'ele
'} = $1);
629 <time>(\d\d\d\d)-?(\d\d)-?(\d\d)T
630 (\d\d):?(\d\d):?([\d\.]+)Z</time>
633 ($Dat{'year
'}, $Dat{'month
'}, $Dat{'day
'},
634 $Dat{'hour
'}, $Dat{'min
'}, $Dat{'sec
'}) =
635 ($1, $2, $3, $4, $5, $6);
651 local *OutFP = shift;
652 if ($Opt{'output
-format
'} eq "gpsml") {
654 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
658 } elsif ($Opt{'output
-format
'} eq "gpstrans") {
659 print(OutFP "Format: DMS UTC Offset: 0.00 hrs " .
660 "Datum[100]: WGS 84\n");
661 } elsif ($Opt{'output
-format
'} eq "gpx") {
663 qq{<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n},
665 qq{$Spc${Spc}version="1.1"\n},
666 qq{$Spc${Spc}creator="gpst - http://svn.sunbase.org/repos/utils/trunk/src/gpstools/"\n},
667 qq{$Spc${Spc}xmlns="http://www.topografix.com/GPX/1/1"\n},
668 qq{$Spc${Spc}xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n},
669 qq{$Spc${Spc}xsi:schemaLocation="http://www.topografix.com/GPX/1/1 },
670 qq{http://www.topografix.com/GPX/1/1/gpx.xsd"\n},
673 qq{$Spc$Spc$Spc$Spc<trkseg>\n},
675 } elsif ($Opt{'output
-format
'} eq "ps") {
676 print(OutFP ps_header(532, 6034, 533, 6040));
678 } elsif ($Opt{'output
-format
'} eq "svg") {
680 "<?xml version=\"1.0\" standalone=\"no\"?>\n",
681 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
682 "$Spc$Spc\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
683 "<svg height=\"1000\" width=\"1000\" viewBox=\"23 70 2 2\"\n",
684 "$Spc${Spc}xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n",
685 "$Spc$Spc<title></title>\n",
686 "$Spc$Spc<desc></desc>\n",
694 local *OutFP = shift;
695 if ($Opt{'output
-format
'} eq "gpsml") {
700 } elsif ($Opt{'output
-format
'} eq "gpx") {
702 "$Spc$Spc$Spc$Spc</trkseg>\n",
706 } elsif ($Opt{'output
-format
'} eq "poscount") {
707 while (my ($l_name, $l_val) = each %Poscount) {
708 $l_name =~ /^(.+?),(.+?)$/
709 && print(OutFP "$1\t$2\t$l_val\n");
711 } elsif ($Opt{'output
-format
'} eq "ps") {
717 } elsif ($Opt{'output
-format
'} eq "svg") {
718 print(OutFP "\"/>\n</svg>\n");
724 # Print a GPS entry with time, latitude, longitude and elevation in
728 defined($Dat{'desc
'}) || ($Dat{'desc
'} = "");
729 defined($Dat{'ele
'}) || ($Dat{'ele
'} = "");
730 defined($Dat{'lat
'}) || ($Dat{'lat
'} = "");
731 defined($Dat{'lon
'}) || ($Dat{'lon
'} = "");
732 defined($Dat{'year
'}) || ($Dat{'year
'} = "");
733 my $print_time = length($Dat{'year
'}) ? 1 : 0;
734 my $print_pos = (length($Dat{'lat
'}) && length($Dat{'lon
'})) ? 1 : 0;
736 $Dat{'lat
'} = $Dat{'lon
'} = "";
738 my $print_ele = length($Dat{'ele
'}) ? 1 : 0;
739 my $print_desc = length($Dat{'desc
'}) ? 1 : 0;
741 D("print_entry(\"" . join("\", \"", @_) . "\");");
744 if ($Opt{'near
'} && $print_pos) {
745 $Line .= sprintf("%s ",
746 list_nearest_waypoints($Dat{'lat
'}, $Dat{'lon
'}));
749 if (length($Opt{'round
'})) {
750 for my $Tmp (qw{ lat lon ele }) {
751 if (defined($Round{$Tmp}) && length($Dat{$Tmp})) {
753 ($Dat{$Tmp} = 1.0 * sprintf("%.$Round{$Tmp}f", $Dat{$Tmp}));
758 if ($Opt{'output
-format
'} eq "poscount") {
759 if (!length($Dat{'error
'})) {
760 my $Name = "$Dat{'lon
'},$Dat{'lat
'}";
761 defined($Poscount{$Name}) || ($Poscount{$Name} = 0);
768 $ep_time = timegm_nocheck(
769 $Dat{'sec
'}, $Dat{'min
'}, $Dat{'hour
'},
770 $Dat{'day
'}, $Dat{'month
'} - 1, $Dat{'year
'}
772 $Dat{'epoch
'} = $ep_time;
773 $Dat{'year
'} = sprintf("%04u", $Dat{'year
'});
774 $Dat{'month
'} = sprintf("%02u", $Dat{'month
'});
775 $Dat{'day
'} = sprintf("%02u", $Dat{'day
'});
776 $Dat{'hour
'} = sprintf("%02u", $Dat{'hour
'});
777 $Dat{'min
'} = sprintf("%02u", $Dat{'min
'});
778 $Dat{'sec
'} = sprintf("%02u", $Dat{'sec
'});
779 if ($Opt{'chronology
'}) {
780 if ($last_time > $ep_time && !length($Dat{'error
'})) {
782 "%s: $curr_file: \"%sZ\": Next date is %s in the past (%sZ)\n",
783 $progname, sec_to_string($last_time, "T"),
784 sec_to_readable($last_time-$ep_time),
785 sec_to_string($ep_time, "T")
787 # FIXME: Make --fix work with gpx.
788 if ($Opt{'fix
'} && ($Opt{'output
-format
'} !~ /^gpx$/)) {
789 $Dat{'error
'} = "chrono";
791 } elsif ($last_time == $ep_time && !length($Dat{'error
'})) {
793 "%s: $curr_file: \"%sZ\": Duplicated time\n",
794 $progname, sec_to_string($last_time, "T")
796 # FIXME: Make --fix work with gpx.
797 if ($Opt{'fix
'} && ($Opt{'output
-format
'} !~ /^gpx$/)) {
798 $Dat{'error
'} = "duptime";
812 if ($Opt{'save
-to
-file
'} ne "\n") {
814 $print_time || return;
815 my $base_name = "$Dat{'year
'}$Dat{'month
'}$Dat{'day
'}T" .
816 "$Dat{'hour
'}$Dat{'min
'}$Dat{'sec
'}Z" .
817 "$Opt{'save
-to
-file
'}";
818 my $file_name = $base_name;
820 for (my $a = 1; (-e $file_name) && ($a < 1000); $a++) {
821 $file_name = "$base_name.dup_$a";
824 die("$progname: $base_name: File already exists, and ran " .
825 "out of attempts to create unique file name\n");
827 if ($Opt{'verbose
'}) {
828 warn("$progname: $base_name: File already exists, using " .
829 "unique name \"$file_name\" instead\n");
832 if (open(ToFP, ">", $file_name)) {
841 ) || die("$progname: $file_name: Cannot write to file: $!\n");
844 if ($Opt{'output
-format
'} eq "gpsml") {
845 printf("<include>%s</include>\n",
846 txt_to_xml($file_name));
847 } elsif ($Opt{'output
-format
'} eq "gpx") {
848 printf("<!-- Saved unconverted data to \"%s\" -->\n",
849 txt_to_xml($file_name));
851 print("$progname: Saved unconverted data to \"$file_name\"\n");
855 die("$progname: $file_name: Cannot create file: $!\n");
863 if ($Dat{'type
'} eq "tp") {
865 if ($Opt{'require'}) {
866 $Req{'time'} && !$print_time && return;
867 $Req{'position
'} && !$print_pos && return;
868 $Req{'ele
'} && !$print_ele && return;
871 if ($Opt{'inside
'} || $Opt{'outside
'}) {
873 ($Dat{'lat
'} < $lat1) ||
874 ($Dat{'lat
'} > $lat2) ||
875 ($Dat{'lon
'} < $lon1) ||
876 ($Dat{'lon
'} > $lon2)
878 $Opt{'inside
'} && return;
880 $Opt{'outside
'} && return;
884 if ($Opt{'output
-format
'} eq "ps") {
891 && ($Dat{'lon
'} eq $last_lon)
892 && ($Dat{'lat
'} eq $last_lat)
894 if ($Opt{'output
-format
'} eq 'gpsml
') {
895 $Dat{'error
'} = "dup";
904 $Opt{'create
-breaks
'}
905 && $ep_time-$last_time > $PAUSE_LIMIT
908 $pause_len = $ep_time-$last_time;
909 D("pause_len set to '$pause_len'");
913 if ($Opt{'output
-format
'} eq "gpsml") {
914 $Line .= sprintf("<pause>%s</pause>\n",
915 sec_to_readable($ep_time-$last_time));
916 } elsif ($Opt{'output
-format
'} eq "clean") {
917 $pause_len && ($Line .= "\n");
918 } elsif ($Opt{'output
-format
'} eq "csv") {
919 $Line .= sprintf("# Pause: %s\n# move\n",
920 sec_to_readable($ep_time-$last_time));
921 } elsif ($Opt{'output
-format
'} eq "xgraph") {
922 $pause_len && ($Line .= "move ");
929 # Valid data was found, send to stdout {{{
930 unless ($first_time) {
931 $first_time = $ep_time;
933 if ($Opt{'double
-y
-scale
'} && length($Dat{'lat
'})) {
936 if ($Opt{'output
-format
'} eq "gpsml") {
937 if ($Dat{'type
'} eq "tp") {
938 $Dat{'format
'} = "gpsml";
939 $Line .= trackpoint(%Dat);
940 } elsif ($Dat{'type
'} =~ /^(pause|desc|title)$/) {
941 $Line .= sprintf("<%s>%s</%s>\n",
946 } elsif ($Opt{'output
-format
'} eq "pgtab") {
947 if ($Dat{'type
'} eq "tp" && !length($Dat{'error
'})) {
948 $Dat{'format
'} = "pgtab";
949 $Line .= trackpoint(%Dat);
951 } elsif ($Opt{'output
-format
'} eq "xgraph") {
952 if ($print_pos && !length($Dat{'error
'})) {
953 $Dat{'format
'} = "xgraph";
954 $Line .= trackpoint(%Dat);
956 } elsif($Opt{'output
-format
'} eq "gpstrans") {
957 if ($print_pos && !length($Dat{'error
'})) {
958 $Dat{'format
'} = "gpstrans";
959 $Line .= trackpoint(%Dat);
961 } elsif($Opt{'output
-format
'} eq "gpx") {
962 if ($Dat{'type
'} eq "tp") {
963 $Dat{'format
'} = "gpx";
964 $Line .= trackpoint(%Dat);
966 } elsif ($Opt{'output
-format
'} eq "clean") {
967 if ($Dat{'type
'} eq "tp" && !length($Dat{'error
'})) {
968 $Dat{'format
'} = "clean";
969 $Line .= trackpoint(%Dat);
971 } elsif ($Opt{'output
-format
'} eq "ps") {
974 ? "f\n$Dat{'lon
'} $Dat{'lat
'} m\n"
975 : "$Dat{'lon
'} $Dat{'lat
'} l\n"
977 } elsif ($Opt{'output
-format
'} eq "svg") {
979 ($last_lon == 1000) || $pause_len
981 "$svg_start_thing<path\n",
982 " stroke=\"blue\"\n",
983 " stroke-width=\"0.001\"\n",
986 "M $Dat{'lon
'} $Dat{'lat
'}\n")
987 : "L $Dat{'lon
'} $Dat{'lat
'}\n"
989 } elsif ($Opt{'output
-format
'} eq "ygraph") {
990 if (!length($Dat{'error
'})) {
991 my $Time = $print_time ? ($ep_time - $first_time) * 1 : 0;
992 $Line .= "\"Time = $Time.0\n$Dat{'lon
'} $Dat{'lat
'}\n\n";
994 } elsif ($Opt{'output
-format
'} eq "csv") {
996 if (!length($Dat{'error
'})) {
997 $Dat{'format
'} = "csv";
1002 : $Opt{'short
-date
'}
1003 ? "$Dat{'year
'}$Dat{'month
'}$Dat{'day
'}T" .
1004 "$Dat{'hour
'}$Dat{'min
'}$Dat{'sec
'}Z"
1005 : "$Dat{'year
'}-$Dat{'month
'}-$Dat{'day
'}T" .
1006 "$Dat{'hour
'}:$Dat{'min
'}:$Dat{'sec
'}Z"
1010 $print_ele ? $Dat{'ele
'} : "", # Elevation
1015 } elsif ($Opt{'output
-format
'} eq "pgwtab") {
1016 # FIXME: NOP at the moment.
1018 die("$progname: \"$Opt{'output
-format
'}\": " .
1019 "Unknown output format\n");
1024 if (!$last_time && $Opt{'output
-format
'} eq "ps") {
1025 $Line .= "$Dat{'lon
'} $Dat{'lat
'} m\n";
1030 if ($Opt{'output
-format
'} eq "gpsml") {
1031 $Line = "<break/>\n$Line";
1033 (!$pause_len && ($Opt{'output
-format
'} eq "xgraph"))
1034 && ($Line .= "move $Line");
1035 ($Opt{'output
-format
'} eq "clean") && ($Line .= "\n");
1036 if ($Opt{'output
-format
'} eq "gpx") {
1037 $Line .= "$Spc$Spc$Spc$Spc</trkseg>\n" .
1038 "$Spc$Spc$Spc$Spc<trkseg>\n";
1044 $print_time && ($last_time = $ep_time);
1046 $last_lon = $Dat{'lon
'};
1047 $last_lat = $Dat{'lat
'};
1049 $last_line = $data_line;
1050 $svg_start_thing = "\"/>\n";
1055 # Send a Postscript header to stdout {{{
1056 my ($bl_lon, $bl_lat, $br_lon, $br_lat) = @_;
1057 my $Date = sec_to_string(time);
1059 "%!PS-Adobe-3.0 EPSF-3.0\n",
1060 "%%Creator: $rcs_id\n",
1062 "%%CreationDate: $Date\n",
1063 "%%BoundingBox: $bl_lon $bl_lat $br_lon $br_lat\n",
1064 "%%DocumentData: Clean7Bit\n",
1067 "/bd { bind def } bind def\n",
1068 "/incompound false def\n",
1069 "/m { moveto } bd\n",
1070 "/l { lineto } bd\n",
1071 "/c { curveto } bd\n",
1072 "/F { incompound not {fill} if } bd\n",
1073 "/f { closepath F } bd\n",
1074 "/S { stroke } bd\n",
1075 "/*u { /incompound true def } bd\n",
1076 "/*U { /incompound false def f} bd\n",
1077 "/k { setcmykcolor } bd\n",
1087 # Print program version {{{
1088 for (@main::version_array) {
1095 # Send the help message to stdout {{{
1098 if ($Opt{'verbose
'}) {
1104 Converts between various GPS formats.
1106 Usage: $progname [options] [file [files [...]]]
1107 $progname -S [file [files [...]]]
1108 $progname -u [file [files [...]]]
1113 Check for broken chronology, warn about entries with an old
1116 Skip duplicated coordinates.
1118 Use seconds since 1970-01-01 00:00:00 GMT as date format.
1120 Comment out entries which is obviously wrong. Use together with
1121 --chronology to fix those kind of errors. Does not work with GPX
1124 Used by the pgwupd format. Specifies from which date waypoints
1125 should be updated. No checks for valid date format here, let
1126 PostgreSQL take care of that. All variants it understands can be
1131 Print only trackpoints inside a rectangle specified by --pos1 and
1134 Use x as undefined value. Default: "$Udef".
1136 Add names of the three closest waypoints to the trackpoint.
1137 Unfinished and experimental, needs gpsbabel, which is called from
1138 the program as "$Cmd{'gpsbabel
'}".
1139 -o, --output-format x
1140 Use output format x:
1155 Print only trackpoints outside a rectangle specified by --pos1 and
1159 Specifies one corner where x is in "lat,lon" format (decimal
1160 degrees, negative for west or south) of area rectangle used by the
1161 --inside and --outside options.
1163 Specify requirements for trackpoints to be written. x is a string
1164 with the following flags:
1166 Print only waypoints which have an elevation.
1168 Print only waypoints which have a position.
1170 Print only waypoints which have a timestamp.
1171 -R, --round x=y[,x2=y2[...]]
1172 Round trackpoint element x to y decimals. Example:
1173 --round lat=4,lon=5,ele=1
1175 Use short date format.
1176 -S, --save-to-file x
1177 Save the unconverted data to a file with a filename starting with
1178 the timestamp of the first trackpoint. The parameter string x is
1179 added at the end of the filename. For the time being this option
1180 will ignore all other options. Note: If several files are specified
1181 on the command line, all data will be saved into only one file. This
1182 behaviour may change in the future.
1184 Create breaks in track between points with a difference more than
1185 $PAUSE_LIMIT seconds.
1187 Increase level of verbosity. Can be repeated.
1189 Print version information.
1190 -w, --strip-whitespace
1191 Strip all unnecessary whitespace.
1192 -y, --double-y-scale
1193 Double Y scale (latitude) to get it right in gnuplot.
1195 Print debugging messages.
1203 # Print a status message to stderr based on verbosity level {{{
1204 my ($verbose_level, $Txt) = @_;
1206 if ($Opt{'verbose'} >= $verbose_level) {
1207 print(STDERR
"$progname: $Txt\n");
1215 # Copyleft © Øyvind A. Holm <sunny@sunbase.org>
1217 # This program is free software; you can redistribute it and/or modify
1218 # it under the terms of the GNU General Public License as published by
1219 # the Free Software Foundation; either version 2 of the License, or (at
1220 # your option) any later version.
1222 # This program is distributed in the hope that it will be useful, but
1223 # WITHOUT ANY WARRANTY; without even the implied warranty of
1224 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1225 # See the GNU General Public License for more details.
1227 # You should have received a copy of the GNU General Public License
1228 # along with this program; if not, write to the Free Software
1229 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
1233 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :