Move use strict before BEGIN and use '' in use_ok()
[gpstools.git] / gpsman2gpx
blob7adf16f37cb84bb5188e3629511b69e319d622b1
1 #!/usr/bin/perl -w
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;
16 BEGIN {
17 push(@INC, "$ENV{'HOME'}/bin/src/gpstools");
20 use Getopt::Long;
22 use GPSTxml;
24 $| = 1;
26 our $Debug = 0;
28 our %Opt = (
30 'debug' => 0,
31 'help' => 0,
32 'quiet' => 0,
33 'verbose' => 0,
34 'version' => 0,
38 our $progname = $0;
39 $progname =~ s/^.*\/(.*?)$/$1/;
40 our $VERSION = "0.00";
42 Getopt::Long::Configure("bundling");
43 GetOptions(
45 "debug" => \$Opt{'debug'},
46 "help|h" => \$Opt{'help'},
47 "quiet|q" => \$Opt{'quiet'},
48 "verbose|v+" => \$Opt{'verbose'},
49 "version" => \$Opt{'version'},
51 ) || die("$progname: Option error. Use -h for help.\n");
53 $Opt{'debug'} && ($Debug = 1);
54 $Opt{'help'} && usage(0);
55 if ($Opt{'version'}) {
56 print_version();
57 exit(0);
60 print(<<END);
61 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
62 <gpx
63 version="1.1"
64 creator="gpsman2gpx - http://sunny256.github.com/gpstools/"
65 xmlns="http://www.topografix.com/GPX/1/1"
66 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
67 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
69 END
71 while (<>) {
72 my $Line = $_;
73 if (
74 $Line =~ /^
75 (.*?)\t # name
76 (.*?)\t # cmt
77 (.*?)\t # some kind of worthless date
78 ([NS].+?)\t # lat
79 ([WE].+?)\t # lon
80 (.*) # lots of uninteresting stuff but with altitude
83 ) {
84 my ($Name, $Cmt, $wl_date, $Lat, $Lon, $Rest) =
85 ( $1, $2, $3, $4, $5, $6);
86 my $Ele = "";
88 $Name = txt_to_xml($Name);
89 $Cmt = txt_to_xml($Cmt);
90 $Lat = conv_pos($Lat);
91 $Lon = conv_pos($Lon);
92 if ($Rest =~ /ele=([\d\.]+)\D/) {
93 $Ele = 1.0 * $1;
95 print(" <wpt lat=\"$Lat\" lon=\"$Lon\">\n");
96 print(" <ele>$Ele</ele>\n") if length($Ele);
97 print(" <name>$Name</name>\n") if length($Name);
98 print(" <cmt>$Cmt</cmt>\n") if length($Cmt);
99 print(" </wpt>\n");
103 print("</gpx>\n");
105 sub conv_pos {
106 # {{{
107 my $Retval = shift;
109 if ($Retval =~ /^([NSWE])([0-9\.]+)$/) {
110 my ($Pref, $Deg) = ($1, $2);
111 $Retval = ($Pref =~ /[SW]/)
112 ? 0-$Deg
113 : $Deg;
114 } else {
115 warn("\"$Retval\": Invalid coordinate\n") unless $Opt{'quiet'};
117 return $Retval;
118 # }}}
121 sub print_version {
122 # Print program version {{{
123 print("$progname v$VERSION\n");
124 # }}}
125 } # print_version()
127 sub usage {
128 # Send the help message to stdout {{{
129 my $Retval = shift;
131 if ($Opt{'verbose'}) {
132 print("\n");
133 print_version();
135 print(<<END);
137 Usage: $progname [options] [file [files [...]]]
139 Options:
141 -h, --help
142 Show this help.
143 -v, --verbose
144 Increase level of verbosity. Can be repeated.
145 -q, --quiet
146 Be quiet, don’t warn about unknown lines and stuff.
147 --version
148 Print version information.
149 --debug
150 Print debugging messages.
153 exit($Retval);
154 # }}}
155 } # usage()
157 sub msg {
158 # Print a status message to stderr based on verbosity level {{{
159 my ($verbose_level, $Txt) = @_;
161 if ($Opt{'verbose'} >= $verbose_level) {
162 print(STDERR "$progname: $Txt\n");
164 # }}}
165 } # msg()
167 sub D {
168 # Print a debugging message {{{
169 $Debug || return;
170 my @call_info = caller;
171 chomp(my $Txt = shift);
172 my $File = $call_info[1];
173 $File =~ s#\\#/#g;
174 $File =~ s#^.*/(.*?)$#$1#;
175 print(STDERR "$File:$call_info[2] $$ $Txt\n");
176 return("");
177 # }}}
178 } # D()
180 __END__
182 # Plain Old Documentation (POD) {{{
184 =pod
186 =head1 NAME
190 =head1 SYNOPSIS
192 [options] [file [files [...]]]
194 =head1 DESCRIPTION
198 =head1 OPTIONS
200 =over 4
202 =item B<-h>, B<--help>
204 Print a brief help summary.
206 =item B<-v>, B<--verbose>
208 Increase level of verbosity. Can be repeated.
210 =item B<--version>
212 Print version information.
214 =item B<--debug>
216 Print debugging messages.
218 =back
220 =head1 BUGS
224 =head1 AUTHOR
226 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
228 =head1 COPYRIGHT
230 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
231 This is free software; see the file F<COPYING> for legalese stuff.
233 =head1 LICENCE
235 This program is free software: you can redistribute it and/or modify it
236 under the terms of the GNU General Public License as published by the
237 Free Software Foundation, either version 3 of the License, or (at your
238 option) any later version.
240 This program is distributed in the hope that it will be useful, but
241 WITHOUT ANY WARRANTY; without even the implied warranty of
242 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
243 See the GNU General Public License for more details.
245 You should have received a copy of the GNU General Public License along
246 with this program.
247 If not, see L<http://www.gnu.org/licenses/>.
249 =head1 SEE ALSO
251 =cut
253 # }}}
255 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :