* /trunk/vg
[gpstools.git] / gpsman2gpx
blob743225b19fb6fd4584bcd81c6e2b7dc54dc0899a
1 #!/usr/bin/perl -w
3 #=======================================================================
4 # $Id$
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 2 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
14 BEGIN {
15 push(@INC, "$ENV{'HOME'}/bin/src/gpstools");
18 BEGIN {
19 our @version_array;
22 use strict;
23 use Getopt::Long;
25 use GPSTxml;
27 $| = 1;
29 our $Debug = 0;
31 our %Opt = (
33 'debug' => 0,
34 'help' => 0,
35 'quiet' => 0,
36 'verbose' => 0,
37 'version' => 0,
41 our $progname = $0;
42 $progname =~ s/^.*\/(.*?)$/$1/;
44 my $rcs_id = '$Id$';
45 my $id_date = $rcs_id;
46 $id_date =~ s/^.*?\d+ (\d\d\d\d-.*?\d\d:\d\d:\d\d\S+).*/$1/;
48 push(@main::version_array, $rcs_id);
50 Getopt::Long::Configure("bundling");
51 GetOptions(
53 "debug" => \$Opt{'debug'},
54 "help|h" => \$Opt{'help'},
55 "quiet|q" => \$Opt{'quiet'},
56 "verbose|v+" => \$Opt{'verbose'},
57 "version" => \$Opt{'version'},
59 ) || die("$progname: Option error. Use -h for help.\n");
61 $Opt{'debug'} && ($Debug = 1);
62 $Opt{'help'} && usage(0);
63 if ($Opt{'version'}) {
64 print_version();
65 exit(0);
68 print(<<END);
69 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
70 <gpx
71 version="1.1"
72 creator="gpsman2gpx - http://svn.sunbase.org/repos/utils/trunk/src/gpstools/gpsman2gpx"
73 xmlns="http://www.topografix.com/GPX/1/1"
74 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
75 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
77 <!-- \$Id\$ -->
78 END
80 while (<>) {
81 my $Line = $_;
82 if (
83 $Line =~ /^
84 (.*?)\t # name
85 (.*?)\t # cmt
86 (.*?)\t # some kind of worthless date
87 ([NS].+?)\t # lat
88 ([WE].+?)\t # lon
89 (.*) # lots of uninteresting stuff but with altitude
92 ) {
93 my ($Name, $Cmt, $wl_date, $Lat, $Lon, $Rest) =
94 ( $1, $2, $3, $4, $5, $6);
95 my $Ele = "";
97 $Name = txt_to_xml($Name);
98 $Cmt = txt_to_xml($Cmt);
99 $Lat = conv_pos($Lat);
100 $Lon = conv_pos($Lon);
101 if ($Rest =~ /ele=([\d\.]+)\D/) {
102 $Ele = 1.0 * $1;
104 print(" <wpt lat=\"$Lat\" lon=\"$Lon\">\n");
105 print(" <ele>$Ele</ele>\n") if length($Ele);
106 print(" <name>$Name</name>\n") if length($Name);
107 print(" <cmt>$Cmt</cmt>\n") if length($Cmt);
108 print(" </wpt>\n");
112 print("</gpx>\n");
114 sub conv_pos {
115 # {{{
116 my $Retval = shift;
118 if ($Retval =~ /^([NSWE])([0-9\.]+)$/) {
119 my ($Pref, $Deg) = ($1, $2);
120 $Retval = ($Pref =~ /[SW]/)
121 ? 0-$Deg
122 : $Deg;
123 } else {
124 warn("\"$Retval\": Invalid coordinate\n") unless $Opt{'quiet'};
126 return $Retval;
127 # }}}
130 sub print_version {
131 # Print program version {{{
132 for (@main::version_array) {
133 print("$_\n");
135 # }}}
136 } # print_version()
138 sub usage {
139 # Send the help message to stdout {{{
140 my $Retval = shift;
142 if ($Opt{'verbose'}) {
143 print("\n");
144 print_version();
146 print(<<END);
148 Usage: $progname [options] [file [files [...]]]
150 Options:
152 -h, --help
153 Show this help.
154 -v, --verbose
155 Increase level of verbosity. Can be repeated.
156 -q, --quiet
157 Be quiet, don’t warn about unknown lines and stuff.
158 --version
159 Print version information.
160 --debug
161 Print debugging messages.
164 exit($Retval);
165 # }}}
166 } # usage()
168 sub msg {
169 # Print a status message to stderr based on verbosity level {{{
170 my ($verbose_level, $Txt) = @_;
172 if ($Opt{'verbose'} >= $verbose_level) {
173 print(STDERR "$progname: $Txt\n");
175 # }}}
176 } # msg()
178 sub D {
179 # Print a debugging message {{{
180 $Debug || return;
181 my @call_info = caller;
182 chomp(my $Txt = shift);
183 my $File = $call_info[1];
184 $File =~ s#\\#/#g;
185 $File =~ s#^.*/(.*?)$#$1#;
186 print(STDERR "$File:$call_info[2] $$ $Txt\n");
187 return("");
188 # }}}
189 } # D()
191 __END__
193 # Plain Old Documentation (POD) {{{
195 =pod
197 =head1 NAME
201 =head1 REVISION
203 $Id$
205 =head1 SYNOPSIS
207 [options] [file [files [...]]]
209 =head1 DESCRIPTION
213 =head1 OPTIONS
215 =over 4
217 =item B<-h>, B<--help>
219 Print a brief help summary.
221 =item B<-v>, B<--verbose>
223 Increase level of verbosity. Can be repeated.
225 =item B<--version>
227 Print version information.
229 =item B<--debug>
231 Print debugging messages.
233 =back
235 =head1 BUGS
239 =head1 AUTHOR
241 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
243 =head1 COPYRIGHT
245 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
246 This is free software; see the file F<COPYING> for legalese stuff.
248 =head1 LICENCE
250 This program is free software; you can redistribute it and/or modify it
251 under the terms of the GNU General Public License as published by the
252 Free Software Foundation; either version 2 of the License, or (at your
253 option) any later version.
255 This program is distributed in the hope that it will be useful, but
256 WITHOUT ANY WARRANTY; without even the implied warranty of
257 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
258 See the GNU General Public License for more details.
260 You should have received a copy of the GNU General Public License along
261 with this program; if not, write to the Free Software Foundation, Inc.,
262 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
264 =head1 SEE ALSO
266 =cut
268 # }}}
270 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
271 # End of file $Id$