3 #=======================================================================
5 # File ID: 082106ec-f924-11dd-b757-0001805bf4b1
6 # Converts between various GPS formats
9 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 3 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
16 use Time
::Local qw
{ timegm_nocheck
};
19 push(@INC, "$ENV{'HOME'}/bin/src/gpstools");
33 # Initial values for command line arguments {{{
38 'double-y-scale' => 0,
45 'output-format' => "gpsml",
51 'save-to-file' => "\n", # \n = undefined, it’s banned in filenames anyway.
54 'strip-whitespace' => 0,
64 $progname =~ s/^.*\/(.*?)$/$1/;
65 our $VERSION = "0.00";
67 Getopt
::Long
::Configure
("bundling");
69 # Command line options {{{
71 "chronology" => \
$Opt{'chronology'},
72 "create-breaks|t" => \
$Opt{'create-breaks'},
73 "debug" => \
$Opt{'debug'},
74 "double-y-scale|y" => \
$Opt{'double-y-scale'},
75 "epoch|e" => \
$Opt{'epoch'},
76 "fix" => \
$Opt{'fix'},
77 "from-date=s" => \
$Opt{'from-date'},
78 "help|h" => \
$Opt{'help'},
79 "inside" => \
$Opt{'inside'},
80 "near" => \
$Opt{'near'},
81 "output-format|o=s" => \
$Opt{'output-format'},
82 "outside" => \
$Opt{'outside'},
83 "pos1=s" => \
$Opt{'pos1'},
84 "pos2=s" => \
$Opt{'pos2'},
85 "require|r=s" => \
$Opt{'require'},
86 "round|R=s" => \
$Opt{'round'},
87 "save-to-file|S=s" => \
$Opt{'save-to-file'},
88 "short-date|s" => \
$Opt{'short-date'},
89 "skip-dups|d" => \
$Opt{'skip-dups'},
90 "strip-whitespace|w" => \
$Opt{'strip-whitespace'},
91 "time-shift|T=i" => \
$Opt{'time-shift'},
92 "undefined|n=s" => \
$Opt{'undefined'},
93 "verbose|v+" => \
$Opt{'verbose'},
94 "version" => \
$Opt{'version'},
97 ) || die("$progname: Option error. Use -h for help.\n");
101 my $PAUSE_LIMIT = 2 * 60; # Antall sekunder mellom to punkter det må til før en move legges inn.
103 my $DIGIT = '[0-9\.\-\+]'; # Used in regexps
104 $GPST::Spc
= $Opt{'strip-whitespace'} ?
"" : " ";
105 my $Spc = $GPST::Spc
; # FIXME
106 my $found_move = 0; # Settes til 1 hvis en /^# move$/ blir funnet.
109 my ($last_lon, $last_lat, $last_line) =
110 ( 1000, 1000, ""); # Vi kan jo teoretisk sett være i Greenwich eller på ekvator
111 my ($lat1, $lon1, $lat2, $lon2) =
112 (-1000, -1000, 1000, 1000);
115 'gpsbabel' => '/usr/local/bin/gpsbabel',
120 if ($Opt{'output-format'} =~ /^(gpx|pgtab)$/) {
121 $Opt{'require'} .= "p";
124 'ele' => ($Opt{'require'} =~ /e/) ?
1 : 0,
125 'position' => ($Opt{'require'} =~ /p/) ?
1 : 0,
126 'time' => ($Opt{'require'} =~ /t/) ?
1 : 0,
128 $Opt{'require'} =~ /[^ept]/
129 && die("$0: Unknown flag in --require (-r) value\n");
131 $Opt{'debug'} && ($Debug = 1);
132 $Opt{'help'} && usage
(0);
133 if ($Opt{'version'}) {
138 if ($Opt{'pos1'} =~ /^($DIGIT+),($DIGIT+)$/) {
142 if ($Opt{'pos2'} =~ /^($DIGIT+),($DIGIT+)$/) {
157 if ($Opt{'epoch'} && $Opt{'short-date'}) {
158 die("$progname: Cannot mix the --epoch (-e) and --short-date (-s) options\n");
161 if ($Opt{'inside'} && $Opt{'outside'}) {
162 die("$progname: Cannot mix the --inside and --outside options\n");
165 # To avoid printing out extra "/> at the start of svg output:
166 my $svg_start_thing = "";
170 if (defined($Opt{'round'})) {
171 my $R = $Opt{'round'};
172 $R =~ s/([a-z]+)=(\d+)/($Round{$1}=$2, "")/eg;
175 length($Opt{'undefined'}) && ($Udef = $Opt{'undefined'});
177 $Opt{'save-to-file'} eq "\n" && print_header
(*STDOUT
);
184 my $from_stdin = scalar(@ARGV) ?
0 : 1;
186 $from_stdin && push(@ARGV, "-");
188 for $curr_file (@ARGV) {
189 # Scan through stdin or specified files and send every GPS entry to
192 print(STDERR
"$progname: Opening \"$curr_file\" for read\n") if $Opt{'verbose'};
193 if (open(CurrFP
, "<$curr_file")) {
198 'year' => '', 'month' => '', 'day' => '',
199 'hour' => '', 'min' => '', 'sec' => '',
202 'lat' => '', 'lon' => '',
209 $Opt{'epoch'} && ($Dat{'date-format'} = "epoch");
210 $Opt{'short-date'} && ($Dat{'date-format'} = "short");
212 if ($Opt{'save-to-file'} ne "\n") {
213 push(@first_lines, $_);
215 s/^# error // && ($Dat{'error'} = "error");
216 s/^# ?// && ($Dat{'error'} = "desc");
218 if (m
#^<(e?tp)\b(.*?)>(.*?)</(e?tp)>\s*$#) {
219 # gpsml — The main storage format {{{
220 my ($Elem, $Props, $Data) =
222 my $err_str = ($Props =~ /\berr="(.*?)"/) ?
$1 : "error";
223 $Elem eq "etp" && ($Dat{'error'} = $err_str);
225 $Data =~ m
#<time>(.*?)</time># && ($Time = $1);
227 (\d\d\d\d
)-?
(\d\d
)-?
(\d\d
)[T
](\d\d
):?
(\d\d
):?
([\d\
.]+?
)Z
229 ($Dat{'year'}, $Dat{'month'}, $Dat{'day'},
230 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'}) =
235 $Data =~ m
#<lat>($DIGIT*?)</lat># && ($Dat{'lat'} = $1);
236 $Data =~ m
#<lon>($DIGIT*?)</lon># && ($Dat{'lon'} = $1);
237 $Data =~ m
#<ele>($DIGIT*?)</ele># && ($Dat{'ele'} = $1);
238 $Data =~ m
#<desc>(.*?)</desc># && ($Dat{'desc'} = $1);
241 } elsif (m
#^<break\b.*?/>#) {
243 } elsif (m
#^<(title|pause)\b.*?>(.*?)</(title|pause)>#) {
247 } elsif (m
#^<desc\b.*?>(.*$)#s) {
248 $Dat{'what'} = "desc";
250 until ($Txt =~ m
#</desc>#s) {
253 $Txt =~ s
#^(.*)(</desc>.*$)#$1#s;
258 $xml_data .= join("", <CurrFP
>);
259 if (!length($Opt{'output-format'})) {
260 $Opt{'output-format'} = "gpx";
261 print_header
(*STDOUT
);
263 read_xmlfile
($xml_data);
267 } elsif (m
#^(\d+)\t($DIGIT+)\t($DIGIT+)\t($DIGIT)#) {
268 # CSV format, epoch style {{{
269 my ($ep_time, $lon_val, $lat_val, $Alt) =
271 $Dat{'epoch'} = $ep_time;
272 ($Dat{'sec'}, $Dat{'min'}, $Dat{'hour'},
273 $Dat{'day'}, $Dat{'month'}, $Dat{'year'},
274 $Dat{'wday'}, $Dat{'yday'}) = gmtime($ep_time);
275 $Dat{'month'}++; # Urgh Ⅰ
276 $Dat{'year'} += 1900; # Urgh Ⅱ
281 (\d\d\d\d
)-?
(\d\d
)-?
(\d\d
)[T\
](\d\d
):?
(\d\d
):?
(\d\d
)Z?
\t
282 ($DIGIT+)\t($DIGIT+)\t($DIGIT)
285 # CSV format, human-readable date format {{{
286 ($Dat{'year'}, $Dat{'month'}, $Dat{'day'},
287 $Dat{'hour'}, $Dat{'min'}, $Dat{'sec'},
288 $Dat{'lon'}, $Dat{'lat'}, $Dat{'ele'}) =
294 } elsif (/^Trackpoint\t/) {
295 # 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 {{{
298 # N60.41630 E5.31675\t
299 # 09.02.2006 20:24:37 (UTC)\t
307 $Orig =~ s/[\r\n]+$//;
308 my ($Marker_f, $Position_f, $Time_f, $Alt_f, $Depth_f,
309 $Leglength_f, $Legtime_f, $Legspeed_f, $Legcourse_f) =
311 # Nødløsning for å unngå at variabler
313 "\t\t\t\t\t\t\t\t\t\t"
316 # "Position_f=\"$Position_f\" \x7B\x7B\x7B\n",
317 # "Time_f=\"$Time_f\"\n",
318 # "Alt_f=\"$Alt_f\"\n",
319 # "Depth_f=\"$Depth_f\"\n",
320 # "Leglength_f=\"$Leglength_f\"\n",
321 # "Legtime_f=\"$Legtime_f\"\n",
322 # "Legspeed_f=\"$Legspeed_f\"\n",
323 # "Legcourse_f=\"$Legcourse_f\" \x7D\x7D\x7D\n",
328 $Legtime_hour, $Legtime_min, $Legtime_sec,
329 $Legspeed, $Legspeed_unit,
331 ) = ("", "", "", "", "", "", "", "", "", "", "", "", "",
332 "", "", "", "", "", "", "", "", "", "");
333 ($Position_f =~ /^(N|S)([\d\.]+) (W|E)([\d\.]+)/) &&
334 ($NS = $1, $Dat{'lat'} = $2, $WE = $3, $Dat{'lon'} = $4);
335 ($Time_f =~ /^(\d+)\.(\d+)\.(\d+) (\d+):(\d+):(\d+) \((.+?)\)/) &&
336 ($Dat{'day'} = $1, $Dat{'month'} = $2, $Dat{'year'} = $3,
337 $Dat{'hour'} = $4, $Dat{'min'} = $5, $Dat{'sec'} = $6);
338 ($Alt_f =~ /^($DIGIT+) (.*?)/) &&
339 ($Dat{'ele'} = $1, $Alt_unit = $2);
340 # D("ele = \"$Dat{'ele'}\"");
341 ($NS eq "S") && ($Dat{'lat'} = 0-$Dat{'lat'});
342 ($WE eq "W") && ($Dat{'lon'} = 0-$Dat{'lon'});
343 # MapSource in win xp writes YYYY, but YY in win98se.
345 defined($Dat{'year'})
346 && $Dat{'year'} =~ /\d/
347 && $Dat{'year'} < 1900
348 ) && ($Dat{'year'} += 2000);
351 } elsif (/^Track\t(.*?)\t/) {
352 $Dat{'title'} = txt_to_xml
($1);
353 $Dat{'what'} = "title";
359 (\d\d
)/(\d\d)/(\d\d\d\d
)\
(\d\d
):(\d\d
):(\d\d
)\t
364 # T 09/01/2002 11:51:26 60°23'36.3" 5°
19'35.9" {{{
365 my ($lat_d, $lat_m, $lat_s, $lon_d, $lon_m, $lon_s);
366 ($Dat{'month
'}, $Dat{'day
'}, $Dat{'year
'},
367 $Dat{'hour
'}, $Dat{'min
'}, $Dat{'sec
'},
368 $lat_d, $lat_m, $lat_s,
369 $lon_d, $lon_m, $lon_s) =
374 my $Flat = defined($Round{'lat
'}) ? ".$Round{'lat
'}" : "";
375 my $Flon = defined($Round{'lon
'}) ? ".$Round{'lon
'}" : "";
376 $Dat{'lat
'} = sprintf("%${Flat}f",
377 1.0*($lat_d+($lat_m/60)+($lat_s/3600)));
378 $Dat{'lon
'} = sprintf("%${Flon}f",
379 1.0*$lon_d+($lon_m/60)+($lon_s/3600));
384 1\ (\S+)\ (\S+)\ (\S+)\ (\S+)\x20
385 (\d\d)/(\d\d)/(\d\d\d\d)\ (\d\d):(\d\d):(\d\d)
388 # 1 60.3938222 5.3238754 17.3 0 09/01/2002 14:18:23 {{{
389 ($Dat{'lat
'}, $Dat{'lon
'}, $Dat{'speed
'},
391 $Dat{'month
'}, $Dat{'day
'}, $Dat{'year
'},
392 $Dat{'hour
'}, $Dat{'min
'}, $Dat{'sec
'}) =
400 # @020721221336N6048353E00701826S015-00001E4859N1673U0000 {{{
410 (\d\d) # Latitude degree
411 (\d\d) # Latitude minute
412 (\d\d\d) # Latitude minute decimals
414 (\d\d\d) # Longitude degree
415 (\d\d) # Longitude minute
416 (\d\d\d) # Longitude minute degree
422 my ($NS, $EW, $lat_deg, $lat_degmin, $lat_mindec, $lon_deg,
423 $lon_degmin, $lon_mindec);
424 ($Dat{'year
'}, $Dat{'month
'}, $Dat{'day
'}, $Dat{'hour
'},
425 $Dat{'min
'}, $Dat{'sec
'}, $NS, $lat_deg,
426 $lat_degmin, $lat_mindec, $EW,
427 $lon_deg, $lon_degmin, $lon_mindec,
428 $Dat{'accur
'}, $Dat{'ele
'}, $Dat{'unknown
'}) =
429 ($2+2000, $3, $4, $5,
434 my $ep_time = timegm_nocheck(
435 $Dat{'sec
'}, $Dat{'min
'}, $Dat{'hour
'},
436 $Dat{'day
'}, $Dat{'month
'}-1, $Dat{'year
'}
438 $last_time = $ep_time;
439 my $Flat = defined($Round{'lat
'}) ? ".$Round{'lat
'}" : "";
440 my $Flon = defined($Round{'lon
'}) ? ".$Round{'lon
'}" : "";
441 my $tmp_lon = sprintf(
446 my $tmp_lat = sprintf("%${Flat}f",
450 ($NS eq "S") && ($tmp_lat = 0-$tmp_lat);
451 ($EW eq "W") && ($tmp_lon = 0-$tmp_lon);
452 $Dat{'lat
'} = $tmp_lat;
453 $Dat{'lon
'} = $tmp_lon;
456 } elsif (/^(@)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(_{42})/) {
457 # @020721221336__________________________________________ {{{
458 ($Dat{'year
'}, $Dat{'month
'}, $Dat{'day
'},
459 $Dat{'hour
'}, $Dat{'min
'}, $Dat{'sec
'}, $Dat{'rest
'}) =
462 $Dat{'error
'} = "nosignal";
465 } elsif (/^xmaplog /) {
468 ($Opt{'output
-format
'} eq "csv")
469 && ($Opt{'save
-to
-file
'} eq "\n")
471 } elsif (/^Pause: /) {
472 # NOP, is here to cope with old files I’ve lying around.
473 } elsif ($Dat{'error
'} eq "desc") {
475 if (defined($Comment)) {
476 $Comment =~ s/^\s*(.*?)\s*$/$1/;
477 if ($Opt{'output
-format
'} eq "gpsml") {
478 $Dat{'desc
'} = txt_to_xml($Comment);
479 $Dat{'what
'} = "desc";
484 $Opt{'verbose
'} && warn("Line $.: Unknown: \"$_\"\n");
489 warn("$progname: $curr_file: Cannot open file for read: $!\n");
494 print_footer(*STDOUT);
500 my $Txt = join("", @_);
501 $Txt =~ s/<!--(.*?)-->//gs;
502 $Txt =~ s#(<gpx\b.*?>.*?</gpx>)#print_gpx($1)#gse;
510 # D("print_xml_gps(\"$Orig\")\n");
511 $Str =~ s/<!--(.*?)-->//gs;
512 my $fromdate_str = "";
513 if ($Opt{'from
-date
'}) {
514 $fromdate_str = "date >= '$Opt{'from-date'}' AND ";
516 if ($Opt{'output
-format
'} =~ /^(pgwtab|pgwupd)$/) {
520 <wpt\b(.*?)>(.*?)</wpt>
525 my ($Lat, $Lon, $Name, $Ele, $Type, $Time, $Cmt, $Desc, $Src, $Sym) =
526 ('\N
', '\N
', '\N
', '\N
', '\N
', '\N
', '\N
', '\N
', '\N
', '\N
');
528 $attr_wpt =~ /.*lat="($DIGIT+?)"/s &&
529 ($Lat = postgresql_copy_safe($1));
530 $attr_wpt =~ /.*lon="($DIGIT+?)"/s &&
531 ($Lon = postgresql_copy_safe($1));
532 $el_wpt =~ /.*<name\b(.*?)>(.*?)<\/name>/s &&
533 ($Name = postgresql_copy_safe(xml_to_txt($2)));
534 $el_wpt =~ /.*<ele\b(.*?)>(.*?)<\/ele>/s &&
535 ($Ele = postgresql_copy_safe(xml_to_txt($2)));
536 $el_wpt =~ /.*<type\b(.*?)>(.*?)<\/type>/s &&
537 ($Type = postgresql_copy_safe(xml_to_txt($2)));
538 $el_wpt =~ /.*<time\b(.*?)>(.*?)<\/time>/s &&
539 ($Time = postgresql_copy_safe(xml_to_txt($2)));
540 $el_wpt =~ /.*<cmt\b(.*?)>(.*?)<\/cmt>/s &&
541 ($Cmt = postgresql_copy_safe(xml_to_txt($2)));
542 $el_wpt =~ /.*<desc\b(.*?)>(.*?)<\/desc>/s &&
543 ($Desc = postgresql_copy_safe(xml_to_txt($2)));
544 $el_wpt =~ /.*<src\b(.*?)>(.*?)<\/src>/s &&
545 ($Src = postgresql_copy_safe(xml_to_txt($2)));
546 $el_wpt =~ /.*<sym\b(.*?)>(.*?)<\/sym>/s &&
547 ($Sym = postgresql_copy_safe(xml_to_txt($2)));
549 if (length($Opt{'round
'})) {
550 if (defined($Round{'lat
'}) && length($Lat)) {
551 ($Lat = 1.0 * sprintf("%.$Round{'lat
'}f", $Lat));
553 if (defined($Round{'lon
'}) && length($Lon)) {
554 ($Lon = 1.0 * sprintf("%.$Round{'lon
'}f", $Lon));
556 if (defined($Round{'ele
'}) && $Ele ne '\N
') {
557 ($Ele = 1.0 * sprintf("%.$Round{'ele
'}f", $Ele));
561 if ($Opt{'output
-format
'} eq "pgwtab") {
575 } elsif ($Opt{'output
-format
'} eq "pgwupd") {
579 "$Spc${Spc}UPDATE logg SET name = clname(coor) " .
580 "WHERE $fromdate_str(point($Lat,$Lon) <-> coor) < 0.05;",
581 "$Spc${Spc}UPDATE logg SET dist = cldist(coor) " .
582 "WHERE $fromdate_str(point($Lat,$Lon) <-> coor) < 0.05;",
593 <trk\b(.*?)>(.*?)</trk>
599 <name\b(.*?)>(.*?)</name>
602 $tmp_dat{'title
'} = $2;
603 $tmp_dat{'what
'} = "title";
604 $tmp_dat{'error
'} = "";
605 print_entry(%tmp_dat);
610 <trkseg\b(.*?)>(.*?)</trkseg>
616 <trkpt\b(.*?)>(.*?)</trkpt>
619 my ($attr_trkpt, $el_trkpt) =
622 'year
' => '', 'month
' => '', 'day
' => '',
623 'hour
' => '', 'min
' => '', 'sec
' => '',
626 'lat
' => '', 'lon
' => '',
632 ($attr_trkpt =~ /\blon="(.*?)"/) && ($Dat{'lon
'} = $1);
633 ($attr_trkpt =~ /\blat="(.*?)"/) && ($Dat{'lat
'} = $1);
634 ($el_trkpt =~ m#<ele\b.*?>(.*?)</ele>#) && ($Dat{'ele
'} = $1);
638 <time>(\d\d\d\d)-?(\d\d)-?(\d\d)T
639 (\d\d):?(\d\d):?([\d\.]+)Z</time>
642 ($Dat{'year
'}, $Dat{'month
'}, $Dat{'day
'},
643 $Dat{'hour
'}, $Dat{'min
'}, $Dat{'sec
'}) =
644 ($1, $2, $3, $4, $5, $6);
660 local *OutFP = shift;
661 if ($Opt{'output
-format
'} eq "gpsml") {
663 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
667 } elsif ($Opt{'output
-format
'} eq "gpstrans") {
668 print(OutFP "Format: DMS UTC Offset: 0.00 hrs " .
669 "Datum[100]: WGS 84\n");
670 } elsif ($Opt{'output
-format
'} eq "gpx") {
672 qq{<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n},
674 qq{$Spc${Spc}version="1.1"\n},
675 qq{$Spc${Spc}creator="gpst - http://sunny256.github.com/gpstools/"\n},
676 qq{$Spc${Spc}xmlns="http://www.topografix.com/GPX/1/1"\n},
677 qq{$Spc${Spc}xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n},
678 qq{$Spc${Spc}xsi:schemaLocation="http://www.topografix.com/GPX/1/1 },
679 qq{http://www.topografix.com/GPX/1/1/gpx.xsd"\n},
682 qq{$Spc$Spc$Spc$Spc<trkseg>\n},
684 } elsif ($Opt{'output
-format
'} eq "ps") {
685 print(OutFP ps_header(532, 6034, 533, 6040));
687 } elsif ($Opt{'output
-format
'} eq "svg") {
689 "<?xml version=\"1.0\" standalone=\"no\"?>\n",
690 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
691 "$Spc$Spc\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
692 "<svg height=\"1000\" width=\"1000\" viewBox=\"23 70 2 2\"\n",
693 "$Spc${Spc}xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n",
694 "$Spc$Spc<title></title>\n",
695 "$Spc$Spc<desc></desc>\n",
703 local *OutFP = shift;
704 if ($Opt{'output
-format
'} eq "gpsml") {
709 } elsif ($Opt{'output
-format
'} eq "gpx") {
711 "$Spc$Spc$Spc$Spc</trkseg>\n",
715 } elsif ($Opt{'output
-format
'} eq "poscount") {
716 while (my ($l_name, $l_val) = each %Poscount) {
717 $l_name =~ /^(.+?),(.+?)$/
718 && print(OutFP "$1\t$2\t$l_val\n");
720 } elsif ($Opt{'output
-format
'} eq "ps") {
726 } elsif ($Opt{'output
-format
'} eq "svg") {
727 print(OutFP "\"/>\n</svg>\n");
733 # Print a GPS entry with time, latitude, longitude and elevation in
737 defined($Dat{'desc
'}) || ($Dat{'desc
'} = "");
738 defined($Dat{'ele
'}) || ($Dat{'ele
'} = "");
739 defined($Dat{'lat
'}) || ($Dat{'lat
'} = "");
740 defined($Dat{'lon
'}) || ($Dat{'lon
'} = "");
741 defined($Dat{'year
'}) || ($Dat{'year
'} = "");
742 my $print_time = length($Dat{'year
'}) ? 1 : 0;
744 if (!$Req{'position
'} && $Opt{'output
-format
'} eq "gpsml") {
745 $print_pos = (length($Dat{'lat
'}) || length($Dat{'lon
'})) ? 1 : 0;
747 $print_pos = (length($Dat{'lat
'}) && length($Dat{'lon
'})) ? 1 : 0;
750 $Dat{'lat
'} = $Dat{'lon
'} = "";
752 my $print_ele = length($Dat{'ele
'}) ? 1 : 0;
753 my $print_desc = length($Dat{'desc
'}) ? 1 : 0;
755 # D("print_entry(\"" . join("\", \"", @_) . "\");");
758 if ($Opt{'near
'} && $print_pos) {
759 $Line .= sprintf("%s ",
760 list_nearest_waypoints($Dat{'lat
'}, $Dat{'lon
'}));
763 if (length($Opt{'round
'})) {
764 for my $Tmp (qw{ lat lon ele }) {
765 if (defined($Round{$Tmp}) && length($Dat{$Tmp})) {
767 ($Dat{$Tmp} = 1.0 * sprintf("%.$Round{$Tmp}f", $Dat{$Tmp}));
772 if ($Opt{'output
-format
'} eq "poscount") {
773 if (!length($Dat{'error
'})) {
774 my $Name = "$Dat{'lon
'},$Dat{'lat
'}";
775 defined($Poscount{$Name}) || ($Poscount{$Name} = 0);
782 $ep_time = timegm_nocheck(
783 $Dat{'sec
'}, $Dat{'min
'}, $Dat{'hour
'},
784 $Dat{'day
'}, $Dat{'month
'} - 1, $Dat{'year
'}
786 if ($Opt{'time-shift'}) {
787 # D("ep_time før: '$ep_time'");
788 $ep_time += $Opt{'time-shift'};
789 # D("ep_time etter: '$ep_time'");
790 ($Dat{'sec
'}, $Dat{'min
'},$Dat{'hour
'}, $Dat{'day
'},
791 $Dat{'month
'}, $Dat{'year
'}) = gmtime($ep_time);
792 $Dat{'year
'} += 1900;
795 $Dat{'epoch
'} = $ep_time;
796 $Dat{'year
'} = sprintf("%04u", $Dat{'year
'});
797 $Dat{'month
'} = sprintf("%02u", $Dat{'month
'});
798 $Dat{'day
'} = sprintf("%02u", $Dat{'day
'});
799 $Dat{'hour
'} = sprintf("%02u", $Dat{'hour
'});
800 $Dat{'min
'} = sprintf("%02u", $Dat{'min
'});
801 $Dat{'sec
'} = sprintf("%02u", $Dat{'sec
'});
802 if ($Opt{'chronology
'}) {
803 if ($last_time > $ep_time && !length($Dat{'error
'})) {
805 "%s: $curr_file: \"%sZ\": Next date is %s in the past (%sZ)\n",
806 $progname, sec_to_string($last_time, "T"),
807 sec_to_readable($last_time-$ep_time),
808 sec_to_string($ep_time, "T")
810 # FIXME: Make --fix work with gpx.
811 if ($Opt{'fix
'} && ($Opt{'output
-format
'} !~ /^gpx$/)) {
812 $Dat{'error
'} = "chrono";
814 } elsif ($last_time == $ep_time && !length($Dat{'error
'})) {
816 "%s: $curr_file: \"%sZ\": Duplicated time\n",
817 $progname, sec_to_string($last_time, "T")
819 # FIXME: Make --fix work with gpx.
820 if ($Opt{'fix
'} && ($Opt{'output
-format
'} !~ /^gpx$/)) {
821 $Dat{'error
'} = "duptime";
835 if ($Opt{'save
-to
-file
'} ne "\n") {
837 $print_time || return;
838 my $base_name = "$Dat{'year
'}$Dat{'month
'}$Dat{'day
'}T" .
839 "$Dat{'hour
'}$Dat{'min
'}$Dat{'sec
'}Z" .
840 "$Opt{'save
-to
-file
'}";
841 my $file_name = $base_name;
843 for (my $a = 1; (-e $file_name) && ($a < 1000); $a++) {
844 $file_name = "$base_name.dup_$a";
847 die("$progname: $base_name: File already exists, and ran " .
848 "out of attempts to create unique file name\n");
850 if ($Opt{'verbose
'}) {
851 warn("$progname: $base_name: File already exists, using " .
852 "unique name \"$file_name\" instead\n");
855 if (open(ToFP, ">", $file_name)) {
864 ) || die("$progname: $file_name: Cannot write to file: $!\n");
867 if ($Opt{'output
-format
'} eq "gpsml") {
868 printf("<include>%s</include>\n",
869 txt_to_xml($file_name));
870 } elsif ($Opt{'output
-format
'} eq "gpx") {
871 printf("<!-- Saved unconverted data to \"%s\" -->\n",
872 txt_to_xml($file_name));
874 print("$progname: Saved unconverted data to \"$file_name\"\n");
878 die("$progname: $file_name: Cannot create file: $!\n");
886 if ($Dat{'what
'} eq "tp") {
888 if ($Opt{'require'}) {
889 $Req{'time'} && !$print_time && return;
890 $Req{'position
'} && !$print_pos && return;
891 $Req{'ele
'} && !$print_ele && return;
894 if ($Opt{'inside
'} || $Opt{'outside
'}) {
896 ($Dat{'lat
'} < $lat1) ||
897 ($Dat{'lat
'} > $lat2) ||
898 ($Dat{'lon
'} < $lon1) ||
899 ($Dat{'lon
'} > $lon2)
901 $Opt{'inside
'} && return;
903 $Opt{'outside
'} && return;
907 if ($Opt{'output
-format
'} eq "ps") {
914 && ($Dat{'lon
'} eq $last_lon)
915 && ($Dat{'lat
'} eq $last_lat)
917 if ($Opt{'output
-format
'} eq 'gpsml
') {
918 $Dat{'error
'} = "dup";
927 $Opt{'create
-breaks
'}
928 && $ep_time-$last_time > $PAUSE_LIMIT
931 $pause_len = $ep_time-$last_time;
932 # D("pause_len set to '$pause_len'");
936 if ($Opt{'output
-format
'} eq "gpsml") {
937 $Line .= sprintf("<pause>%s</pause>\n",
938 sec_to_readable($ep_time-$last_time));
939 } elsif ($Opt{'output
-format
'} eq "clean") {
940 $pause_len && ($Line .= "\n");
941 } elsif ($Opt{'output
-format
'} eq "csv") {
942 $Line .= sprintf("# Pause: %s\n# move\n",
943 sec_to_readable($ep_time-$last_time));
944 } elsif ($Opt{'output
-format
'} eq "xgraph") {
945 $pause_len && ($Line .= "move ");
952 # Valid data was found, send to stdout {{{
953 unless ($first_time) {
954 $first_time = $ep_time;
956 if ($Opt{'double
-y
-scale
'} && length($Dat{'lat
'})) {
959 if ($Opt{'output
-format
'} eq "gpsml") {
960 if ($Dat{'what
'} eq "tp") {
961 $Dat{'format
'} = "gpsml";
962 $Line .= trackpoint(%Dat);
963 } elsif ($Dat{'what
'} =~ /^(pause|desc|title)$/) {
964 $Line .= sprintf("<%s>%s</%s>\n",
969 } elsif ($Opt{'output
-format
'} eq "pgtab") {
970 if ($Dat{'what
'} eq "tp" && !length($Dat{'error
'})) {
971 $Dat{'format
'} = "pgtab";
972 $Line .= trackpoint(%Dat);
974 } elsif ($Opt{'output
-format
'} eq "xgraph") {
975 if ($print_pos && !length($Dat{'error
'})) {
976 $Dat{'format
'} = "xgraph";
977 $Line .= trackpoint(%Dat);
979 } elsif($Opt{'output
-format
'} eq "gpstrans") {
980 if ($print_pos && !length($Dat{'error
'})) {
981 $Dat{'format
'} = "gpstrans";
982 $Line .= trackpoint(%Dat);
984 } elsif($Opt{'output
-format
'} eq "gpx") {
985 if ($Dat{'what
'} eq "tp") {
986 $Dat{'format
'} = "gpx";
987 $Line .= trackpoint(%Dat);
989 } elsif ($Opt{'output
-format
'} eq "clean") {
990 if ($Dat{'what
'} eq "tp" && !length($Dat{'error
'})) {
991 $Dat{'format
'} = "clean";
992 $Line .= trackpoint(%Dat);
994 } elsif ($Opt{'output
-format
'} eq "ps") {
997 ? "f\n$Dat{'lon
'} $Dat{'lat
'} m\n"
998 : "$Dat{'lon
'} $Dat{'lat
'} l\n"
1000 } elsif ($Opt{'output
-format
'} eq "svg") {
1002 ($last_lon == 1000) || $pause_len
1004 "$svg_start_thing<path\n",
1005 " stroke=\"blue\"\n",
1006 " stroke-width=\"0.001\"\n",
1009 "M $Dat{'lon
'} $Dat{'lat
'}\n")
1010 : "L $Dat{'lon
'} $Dat{'lat
'}\n"
1012 } elsif ($Opt{'output
-format
'} eq "ygraph") {
1013 if (!length($Dat{'error
'})) {
1014 my $Time = $print_time ? ($ep_time - $first_time) * 1 : 0;
1015 $Line .= "\"Time = $Time.0\n$Dat{'lon
'} $Dat{'lat
'}\n\n";
1017 } elsif ($Opt{'output
-format
'} eq "csv") {
1019 if (!length($Dat{'error
'})) {
1020 $Dat{'format
'} = "csv";
1025 : $Opt{'short
-date
'}
1026 ? "$Dat{'year
'}$Dat{'month
'}$Dat{'day
'}T" .
1027 "$Dat{'hour
'}$Dat{'min
'}$Dat{'sec
'}Z"
1028 : "$Dat{'year
'}-$Dat{'month
'}-$Dat{'day
'}T" .
1029 "$Dat{'hour
'}:$Dat{'min
'}:$Dat{'sec
'}Z"
1033 $print_ele ? $Dat{'ele
'} : "", # Elevation
1038 } elsif ($Opt{'output
-format
'} eq "pgwtab") {
1039 # FIXME: NOP at the moment.
1041 die("$progname: \"$Opt{'output
-format
'}\": " .
1042 "Unknown output format\n");
1047 if (!$last_time && $Opt{'output
-format
'} eq "ps") {
1048 $Line .= "$Dat{'lon
'} $Dat{'lat
'} m\n";
1053 if ($Opt{'output
-format
'} eq "gpsml") {
1054 $Line = "<break/>\n$Line";
1056 (!$pause_len && ($Opt{'output
-format
'} eq "xgraph"))
1057 && ($Line .= "move $Line");
1058 ($Opt{'output
-format
'} eq "clean") && ($Line .= "\n");
1059 if ($Opt{'output
-format
'} eq "gpx") {
1060 $Line .= "$Spc$Spc$Spc$Spc</trkseg>\n" .
1061 "$Spc$Spc$Spc$Spc<trkseg>\n";
1067 $print_time && ($last_time = $ep_time);
1069 $last_lon = $Dat{'lon
'};
1070 $last_lat = $Dat{'lat
'};
1072 $last_line = $data_line;
1073 $svg_start_thing = "\"/>\n";
1078 # Send a Postscript header to stdout {{{
1079 my ($bl_lon, $bl_lat, $br_lon, $br_lat) = @_;
1080 my $Date = sec_to_string(time);
1082 "%!PS-Adobe-3.0 EPSF-3.0\n",
1083 "%%Creator: gpst\n",
1085 "%%CreationDate: $Date\n",
1086 "%%BoundingBox: $bl_lon $bl_lat $br_lon $br_lat\n",
1087 "%%DocumentData: Clean7Bit\n",
1090 "/bd { bind def } bind def\n",
1091 "/incompound false def\n",
1092 "/m { moveto } bd\n",
1093 "/l { lineto } bd\n",
1094 "/c { curveto } bd\n",
1095 "/F { incompound not {fill} if } bd\n",
1096 "/f { closepath F } bd\n",
1097 "/S { stroke } bd\n",
1098 "/*u { /incompound true def } bd\n",
1099 "/*U { /incompound false def f} bd\n",
1100 "/k { setcmykcolor } bd\n",
1110 # Print program version {{{
1111 print("$progname v$VERSION\n");
1116 # Send the help message to stdout {{{
1119 if ($Opt{'verbose
'}) {
1125 Converts between various GPS formats.
1127 Usage: $progname [options] [file [files [...]]]
1128 $progname -S [file [files [...]]]
1129 $progname -u [file [files [...]]]
1134 Check for broken chronology, warn about entries with an old
1137 Skip duplicated coordinates.
1139 Use seconds since 1970-01-01 00:00:00 GMT as date format.
1141 Comment out entries which is obviously wrong. Use together with
1142 --chronology to fix those kind of errors. Does not work with GPX
1145 Used by the pgwupd format. Specifies from which date waypoints
1146 should be updated. No checks for valid date format here, let
1147 PostgreSQL take care of that. All variants it understands can be
1152 Print only trackpoints inside a rectangle specified by --pos1 and
1155 Use x as undefined value. Default: "$Udef".
1157 Add names of the three closest waypoints to the trackpoint.
1158 Unfinished and experimental, needs gpsbabel, which is called from
1159 the program as "$Cmd{'gpsbabel
'}".
1160 -o, --output-format x
1161 Use output format x:
1176 Print only trackpoints outside a rectangle specified by --pos1 and
1180 Specifies one corner where x is in "lat,lon" format (decimal
1181 degrees, negative for west or south) of area rectangle used by the
1182 --inside and --outside options.
1184 Specify requirements for trackpoints to be written. x is a string
1185 with the following flags:
1187 Print only waypoints which have an elevation.
1189 Print only waypoints which have a position.
1191 Print only waypoints which have a timestamp.
1192 -R, --round x=y[,x2=y2[...]]
1193 Round trackpoint element x to y decimals. Example:
1194 --round lat=4,lon=5,ele=1
1196 Use short date format.
1197 -S, --save-to-file x
1198 Save the unconverted data to a file with a filename starting with
1199 the timestamp of the first trackpoint. The parameter string x is
1200 added at the end of the filename. For the time being this option
1201 will ignore all other options. Note: If several files are specified
1202 on the command line, all data will be saved into only one file. This
1203 behaviour may change in the future.
1205 Create breaks in track between points with a difference more than
1206 $PAUSE_LIMIT seconds.
1207 -T x, --time-shift x
1208 Move time of trackpoint x seconds forwards or backwards. x can be a
1209 positive or negative integer.
1211 Increase level of verbosity. Can be repeated.
1213 Print version information.
1214 -w, --strip-whitespace
1215 Strip all unnecessary whitespace.
1216 -y, --double-y-scale
1217 Double Y scale (latitude) to get it right in gnuplot.
1219 Print debugging messages.
1227 # Print a status message to stderr based on verbosity level {{{
1228 my ($verbose_level, $Txt) = @_;
1230 if ($Opt{'verbose'} >= $verbose_level) {
1231 print(STDERR
"$progname: $Txt\n");
1239 # Copyleft © Øyvind A. Holm <sunny@sunbase.org>
1241 This program is free software
: you can redistribute it
and/or modify it
1242 under the terms of the GNU General Public License as published by the
1243 Free Software Foundation
, either version
3 of the License
, or (at your
1244 option
) any later version
.
1246 This program is distributed
in the hope that it will be useful
, but
1247 WITHOUT ANY WARRANTY
; without even the implied warranty of
1248 MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE
.
1249 See the GNU General Public License
for more details
.
1251 You should have received a copy of the GNU General Public License along
1253 If
not, see L
<http
://www
.gnu
.org
/licenses/>.
1256 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :