Deleting branches/gpst.gpxfix/
[gpstools.git] / csv2gpx
blobf1af1796de4662b3b0ff01ef97307ef05c61f21a
1 #!/usr/bin/perl -w
3 #=======================================================================
4 # csv2gpx
5 # File ID: 9867d624-f742-11dd-910a-000475e441b9
6 # [Description]
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 Getopt::Long;
17 $| = 1;
19 our $Debug = 0;
21 our %Opt = (
23 'debug' => 0,
24 'help' => 0,
25 'verbose' => 0,
26 'version' => 0,
30 our $progname = $0;
31 $progname =~ s/^.*\/(.*?)$/$1/;
32 our $VERSION = "0.00";
34 Getopt::Long::Configure("bundling");
35 GetOptions(
37 "debug" => \$Opt{'debug'},
38 "help|h" => \$Opt{'help'},
39 "verbose|v+" => \$Opt{'verbose'},
40 "version" => \$Opt{'version'},
42 ) || die("$progname: Option error. Use -h for help.\n");
44 $Opt{'debug'} && ($Debug = 1);
45 $Opt{'help'} && usage(0);
46 if ($Opt{'version'}) {
47 print_version();
48 exit(0);
51 my $DIGIT = '0-9\.\-\+';
53 print(gpx_header());
55 while (<>) {
56 my ($Lat, $Lon, $Name, $Phone) = ("", "", "", "");
57 if (/^([$DIGIT]+),([$DIGIT]+),(.*) - (.*?)$/) {
58 ($Lon, $Lat, $Name, $Phone) =
59 (txt_to_xml($1), txt_to_xml($2), txt_to_xml($3), txt_to_xml($4));
60 print(<<END);
61 <wpt lat="$Lat" lon="$Lon">
62 <name>$Name</name>
63 <cmt>$Name - $Phone</cmt>
64 </wpt>
65 END
66 } else {
67 warn(sprintf("%s: Unknown line: \"%s\"\n", $progname, chomp($_)));
71 print("</gpx>\n");
73 sub txt_to_xml {
74 # Convert plain text to XML {{{
75 my $Txt = shift;
76 $Txt =~ s/&/&amp;/gs;
77 $Txt =~ s/</&lt;/gs;
78 $Txt =~ s/>/&gt;/gs;
79 return($Txt);
80 # }}}
83 sub gpx_header {
84 # {{{
85 return(<<END);
86 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
87 <gpx
88 version="1.1"
89 creator="csv2gpx - http://svn.sunbase.org/repos/utils/trunk/csv2gpx"
90 xmlns="http://www.topografix.com/GPX/1/1"
91 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
92 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
94 END
95 # }}}
98 sub print_version {
99 # Print program version {{{
100 print("$progname v$VERSION\n");
101 # }}}
102 } # print_version()
104 sub usage {
105 # Send the help message to stdout {{{
106 my $Retval = shift;
108 if ($Opt{'verbose'}) {
109 print("\n");
110 print_version();
112 print(<<END);
114 Usage: $progname [options] [file [files [...]]]
116 Options:
118 -h, --help
119 Show this help.
120 -v, --verbose
121 Increase level of verbosity. Can be repeated.
122 --version
123 Print version information.
124 --debug
125 Print debugging messages.
128 exit($Retval);
129 # }}}
130 } # usage()
132 sub msg {
133 # Print a status message to stderr based on verbosity level {{{
134 my ($verbose_level, $Txt) = @_;
136 if ($Opt{'verbose'} >= $verbose_level) {
137 print(STDERR "$progname: $Txt\n");
139 # }}}
140 } # msg()
142 sub D {
143 # Print a debugging message {{{
144 $Debug || return;
145 my @call_info = caller;
146 chomp(my $Txt = shift);
147 my $File = $call_info[1];
148 $File =~ s#\\#/#g;
149 $File =~ s#^.*/(.*?)$#$1#;
150 print(STDERR "$File:$call_info[2] $$ $Txt\n");
151 return("");
152 # }}}
153 } # D()
155 __END__
157 # Plain Old Documentation (POD) {{{
159 =pod
161 =head1 NAME
165 =head1 SYNOPSIS
167 [options] [file [files [...]]]
169 =head1 DESCRIPTION
173 =head1 OPTIONS
175 =over 4
177 =item B<-h>, B<--help>
179 Print a brief help summary.
181 =item B<-v>, B<--verbose>
183 Increase level of verbosity. Can be repeated.
185 =item B<--version>
187 Print version information.
189 =item B<--debug>
191 Print debugging messages.
193 =back
195 =head1 BUGS
199 =head1 AUTHOR
201 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
203 =head1 COPYRIGHT
205 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
206 This is free software; see the file F<COPYING> for legalese stuff.
208 =head1 LICENCE
210 This program is free software: you can redistribute it and/or modify it
211 under the terms of the GNU General Public License as published by the
212 Free Software Foundation, either version 3 of the License, or (at your
213 option) any later version.
215 This program is distributed in the hope that it will be useful, but
216 WITHOUT ANY WARRANTY; without even the implied warranty of
217 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
218 See the GNU General Public License for more details.
220 You should have received a copy of the GNU General Public License along
221 with this program.
222 If not, see L<http://www.gnu.org/licenses/>.
224 =head1 SEE ALSO
226 =cut
228 # }}}
230 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :