Remove prerounding of Garmin serial format + update tests
[gpstools.git] / gpsman2gpx
blobfaa255f2a690004e4be42b3a8a3ed6279b6d0f7c
1 #!/usr/bin/perl
3 #=======================================================================
4 # gpsman2gpx
5 # File ID: f66b9fe8-f923-11dd-a087-0001805bf4b1
6 # Quick & dirty script for converting gpsman(1) sdata files to GPX.
8 # Character set: UTF-8
9 # ©opyleft 2006– Ø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 #=======================================================================
14 use strict;
15 use warnings;
17 BEGIN {
18 push(@INC, "$ENV{'HOME'}/bin/src/gpstools");
21 use Getopt::Long;
23 use GPSTxml;
25 $| = 1;
27 our $Debug = 0;
29 our %Opt = (
31 'debug' => 0,
32 'help' => 0,
33 'quiet' => 0,
34 'verbose' => 0,
35 'version' => 0,
39 our $progname = $0;
40 $progname =~ s/^.*\/(.*?)$/$1/;
41 our $VERSION = "0.00";
43 Getopt::Long::Configure("bundling");
44 GetOptions(
46 "debug" => \$Opt{'debug'},
47 "help|h" => \$Opt{'help'},
48 "quiet|q" => \$Opt{'quiet'},
49 "verbose|v+" => \$Opt{'verbose'},
50 "version" => \$Opt{'version'},
52 ) || die("$progname: Option error. Use -h for help.\n");
54 $Opt{'debug'} && ($Debug = 1);
55 $Opt{'help'} && usage(0);
56 if ($Opt{'version'}) {
57 print_version();
58 exit(0);
61 print(<<END);
62 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
63 <gpx
64 version="1.1"
65 creator="gpsman2gpx - http://sunny256.github.com/gpstools/"
66 xmlns="http://www.topografix.com/GPX/1/1"
67 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
68 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
70 END
72 while (<>) {
73 my $Line = $_;
74 if (
75 $Line =~ /^
76 (.*?)\t # name
77 (.*?)\t # cmt
78 (.*?)\t # some kind of worthless date
79 ([NS].+?)\t # lat
80 ([WE].+?)\t # lon
81 (.*) # lots of uninteresting stuff but with altitude
84 ) {
85 my ($Name, $Cmt, $wl_date, $Lat, $Lon, $Rest) =
86 ( $1, $2, $3, $4, $5, $6);
87 my $Ele = "";
89 $Name = txt_to_xml($Name);
90 $Cmt = txt_to_xml($Cmt);
91 $Lat = conv_pos($Lat);
92 $Lon = conv_pos($Lon);
93 if ($Rest =~ /ele=([\d\.]+)\D/) {
94 $Ele = 1.0 * $1;
96 print(" <wpt lat=\"$Lat\" lon=\"$Lon\">\n");
97 print(" <ele>$Ele</ele>\n") if length($Ele);
98 print(" <name>$Name</name>\n") if length($Name);
99 print(" <cmt>$Cmt</cmt>\n") if length($Cmt);
100 print(" </wpt>\n");
104 print("</gpx>\n");
106 sub conv_pos {
107 # {{{
108 my $Retval = shift;
110 if ($Retval =~ /^([NSWE])([0-9\.]+)$/) {
111 my ($Pref, $Deg) = ($1, $2);
112 $Retval = ($Pref =~ /[SW]/)
113 ? 0-$Deg
114 : $Deg;
115 } else {
116 warn("\"$Retval\": Invalid coordinate\n") unless $Opt{'quiet'};
118 return $Retval;
119 # }}}
122 sub print_version {
123 # Print program version {{{
124 print("$progname v$VERSION\n");
125 # }}}
126 } # print_version()
128 sub usage {
129 # Send the help message to stdout {{{
130 my $Retval = shift;
132 if ($Opt{'verbose'}) {
133 print("\n");
134 print_version();
136 print(<<END);
138 Usage: $progname [options] [file [files [...]]]
140 Options:
142 -h, --help
143 Show this help.
144 -v, --verbose
145 Increase level of verbosity. Can be repeated.
146 -q, --quiet
147 Be quiet, don’t warn about unknown lines and stuff.
148 --version
149 Print version information.
150 --debug
151 Print debugging messages.
154 exit($Retval);
155 # }}}
156 } # usage()
158 sub msg {
159 # Print a status message to stderr based on verbosity level {{{
160 my ($verbose_level, $Txt) = @_;
162 if ($Opt{'verbose'} >= $verbose_level) {
163 print(STDERR "$progname: $Txt\n");
165 # }}}
166 } # msg()
168 sub D {
169 # Print a debugging message {{{
170 $Debug || return;
171 my @call_info = caller;
172 chomp(my $Txt = shift);
173 my $File = $call_info[1];
174 $File =~ s#\\#/#g;
175 $File =~ s#^.*/(.*?)$#$1#;
176 print(STDERR "$File:$call_info[2] $$ $Txt\n");
177 return("");
178 # }}}
179 } # D()
181 __END__
183 # Plain Old Documentation (POD) {{{
185 =pod
187 =head1 NAME
191 =head1 SYNOPSIS
193 [options] [file [files [...]]]
195 =head1 DESCRIPTION
199 =head1 OPTIONS
201 =over 4
203 =item B<-h>, B<--help>
205 Print a brief help summary.
207 =item B<-v>, B<--verbose>
209 Increase level of verbosity. Can be repeated.
211 =item B<--version>
213 Print version information.
215 =item B<--debug>
217 Print debugging messages.
219 =back
221 =head1 BUGS
225 =head1 AUTHOR
227 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
229 =head1 COPYRIGHT
231 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
232 This is free software; see the file F<COPYING> for legalese stuff.
234 =head1 LICENCE
236 This program is free software: you can redistribute it and/or modify it
237 under the terms of the GNU General Public License as published by the
238 Free Software Foundation, either version 3 of the License, or (at your
239 option) any later version.
241 This program is distributed in the hope that it will be useful, but
242 WITHOUT ANY WARRANTY; without even the implied warranty of
243 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
244 See the GNU General Public License for more details.
246 You should have received a copy of the GNU General Public License along
247 with this program.
248 If not, see L<http://www.gnu.org/licenses/>.
250 =head1 SEE ALSO
252 =cut
254 # }}}
256 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :