* /trunk/src/gpstools/postgres/update_things.sql
[gpstools.git] / gpsman2gpx
blob8c8685c57f1ca34372ebcbe10836e2c2dca20754
1 #!/usr/bin/perl -w
3 #=======================================================================
4 # $Id$
5 # Quick & dirty script for converting gpsman(1) sdata files to GPX.
7 # Character set: UTF-8
8 # ©opyleft 2006– Ø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 #=======================================================================
13 BEGIN {
14 push(@INC, "$ENV{'HOME'}/bin/src/gpstools");
17 BEGIN {
18 our @version_array;
21 use strict;
22 use Getopt::Long;
24 use GPSTxml;
26 $| = 1;
28 our $Debug = 0;
30 our %Opt = (
32 'debug' => 0,
33 'help' => 0,
34 'quiet' => 0,
35 'verbose' => 0,
36 'version' => 0,
40 our $progname = $0;
41 $progname =~ s/^.*\/(.*?)$/$1/;
43 my $rcs_id = '$Id$';
44 my $id_date = $rcs_id;
45 $id_date =~ s/^.*?\d+ (\d\d\d\d-.*?\d\d:\d\d:\d\d\S+).*/$1/;
47 push(@main::version_array, $rcs_id);
49 Getopt::Long::Configure("bundling");
50 GetOptions(
52 "debug" => \$Opt{'debug'},
53 "help|h" => \$Opt{'help'},
54 "quiet|q" => \$Opt{'quiet'},
55 "verbose|v+" => \$Opt{'verbose'},
56 "version" => \$Opt{'version'},
58 ) || die("$progname: Option error. Use -h for help.\n");
60 $Opt{'debug'} && ($Debug = 1);
61 $Opt{'help'} && usage(0);
62 if ($Opt{'version'}) {
63 print_version();
64 exit(0);
67 print(<<END);
68 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
69 <gpx
70 version="1.1"
71 creator="gpsman2gpx - http://svn.sunbase.org/repos/utils/trunk/src/gpstools/gpsman2gpx"
72 xmlns="http://www.topografix.com/GPX/1/1"
73 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
74 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
76 <!-- \$Id\$ -->
77 END
79 while (<>) {
80 my $Line = $_;
81 if (
82 $Line =~ /^
83 (.*?)\t # name
84 (.*?)\t # cmt
85 (.*?)\t # some kind of worthless date
86 ([NS].+?)\t # lat
87 ([WE].+?)\t # lon
88 (.*) # lots of uninteresting stuff but with altitude
91 ) {
92 my ($Name, $Cmt, $wl_date, $Lat, $Lon, $Rest) =
93 ( $1, $2, $3, $4, $5, $6);
94 my $Ele = "";
96 $Name = txt_to_xml($Name);
97 $Cmt = txt_to_xml($Cmt);
98 $Lat = conv_pos($Lat);
99 $Lon = conv_pos($Lon);
100 if ($Rest =~ /ele=([\d\.]+)\D/) {
101 $Ele = 1.0 * $1;
103 print(" <wpt lat=\"$Lat\" lon=\"$Lon\">\n");
104 print(" <ele>$Ele</ele>\n") if length($Ele);
105 print(" <name>$Name</name>\n") if length($Name);
106 print(" <cmt>$Cmt</cmt>\n") if length($Cmt);
107 print(" </wpt>\n");
111 print("</gpx>\n");
113 sub conv_pos {
114 # {{{
115 my $Retval = shift;
117 if ($Retval =~ /^([NSWE])([0-9\.]+)$/) {
118 my ($Pref, $Deg) = ($1, $2);
119 $Retval = ($Pref =~ /[SW]/)
120 ? 0-$Deg
121 : $Deg;
122 } else {
123 warn("\"$Retval\": Invalid coordinate\n") unless $Opt{'quiet'};
125 return $Retval;
126 # }}}
129 sub print_version {
130 # Print program version {{{
131 for (@main::version_array) {
132 print("$_\n");
134 # }}}
135 } # print_version()
137 sub usage {
138 # Send the help message to stdout {{{
139 my $Retval = shift;
141 if ($Opt{'verbose'}) {
142 print("\n");
143 print_version();
145 print(<<END);
147 Usage: $progname [options] [file [files [...]]]
149 Options:
151 -h, --help
152 Show this help.
153 -v, --verbose
154 Increase level of verbosity. Can be repeated.
155 -q, --quiet
156 Be quiet, don’t warn about unknown lines and stuff.
157 --version
158 Print version information.
159 --debug
160 Print debugging messages.
163 exit($Retval);
164 # }}}
165 } # usage()
167 sub msg {
168 # Print a status message to stderr based on verbosity level {{{
169 my ($verbose_level, $Txt) = @_;
171 if ($Opt{'verbose'} >= $verbose_level) {
172 print(STDERR "$progname: $Txt\n");
174 # }}}
175 } # msg()
177 sub D {
178 # Print a debugging message {{{
179 $Debug || return;
180 my @call_info = caller;
181 chomp(my $Txt = shift);
182 my $File = $call_info[1];
183 $File =~ s#\\#/#g;
184 $File =~ s#^.*/(.*?)$#$1#;
185 print(STDERR "$File:$call_info[2] $$ $Txt\n");
186 return("");
187 # }}}
188 } # D()
190 __END__
192 # Plain Old Documentation (POD) {{{
194 =pod
196 =head1 NAME
200 =head1 REVISION
202 $Id$
204 =head1 SYNOPSIS
206 [options] [file [files [...]]]
208 =head1 DESCRIPTION
212 =head1 OPTIONS
214 =over 4
216 =item B<-h>, B<--help>
218 Print a brief help summary.
220 =item B<-v>, B<--verbose>
222 Increase level of verbosity. Can be repeated.
224 =item B<--version>
226 Print version information.
228 =item B<--debug>
230 Print debugging messages.
232 =back
234 =head1 BUGS
238 =head1 AUTHOR
240 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
242 =head1 COPYRIGHT
244 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
245 This is free software; see the file F<COPYING> for legalese stuff.
247 =head1 LICENCE
249 This program is free software; you can redistribute it and/or modify it
250 under the terms of the GNU General Public License as published by the
251 Free Software Foundation; either version 2 of the License, or (at your
252 option) any later version.
254 This program is distributed in the hope that it will be useful, but
255 WITHOUT ANY WARRANTY; without even the implied warranty of
256 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
257 See the GNU General Public License for more details.
259 You should have received a copy of the GNU General Public License along
260 with this program; if not, write to the Free Software Foundation, Inc.,
261 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
263 =head1 SEE ALSO
265 =cut
267 # }}}
269 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
270 # End of file $Id$