doc/til_postgres.txt: Update tracklog import command.
[gpstools.git] / csv2gpx
blob76e9b722f3fd22471000f27729818687bc6912b8
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 <!-- \$Id\$ -->
88 <gpx
89 version="1.1"
90 creator="csv2gpx - http://svn.sunbase.org/repos/utils/trunk/csv2gpx"
91 xmlns="http://www.topografix.com/GPX/1/1"
92 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
93 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
95 END
96 # }}}
99 sub print_version {
100 # Print program version {{{
101 print("$progname v$VERSION\n");
102 # }}}
103 } # print_version()
105 sub usage {
106 # Send the help message to stdout {{{
107 my $Retval = shift;
109 if ($Opt{'verbose'}) {
110 print("\n");
111 print_version();
113 print(<<END);
115 Usage: $progname [options] [file [files [...]]]
117 Options:
119 -h, --help
120 Show this help.
121 -v, --verbose
122 Increase level of verbosity. Can be repeated.
123 --version
124 Print version information.
125 --debug
126 Print debugging messages.
129 exit($Retval);
130 # }}}
131 } # usage()
133 sub msg {
134 # Print a status message to stderr based on verbosity level {{{
135 my ($verbose_level, $Txt) = @_;
137 if ($Opt{'verbose'} >= $verbose_level) {
138 print(STDERR "$progname: $Txt\n");
140 # }}}
141 } # msg()
143 sub D {
144 # Print a debugging message {{{
145 $Debug || return;
146 my @call_info = caller;
147 chomp(my $Txt = shift);
148 my $File = $call_info[1];
149 $File =~ s#\\#/#g;
150 $File =~ s#^.*/(.*?)$#$1#;
151 print(STDERR "$File:$call_info[2] $$ $Txt\n");
152 return("");
153 # }}}
154 } # D()
156 __END__
158 # Plain Old Documentation (POD) {{{
160 =pod
162 =head1 NAME
166 =head1 SYNOPSIS
168 [options] [file [files [...]]]
170 =head1 DESCRIPTION
174 =head1 OPTIONS
176 =over 4
178 =item B<-h>, B<--help>
180 Print a brief help summary.
182 =item B<-v>, B<--verbose>
184 Increase level of verbosity. Can be repeated.
186 =item B<--version>
188 Print version information.
190 =item B<--debug>
192 Print debugging messages.
194 =back
196 =head1 BUGS
200 =head1 AUTHOR
202 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
204 =head1 COPYRIGHT
206 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
207 This is free software; see the file F<COPYING> for legalese stuff.
209 =head1 LICENCE
211 This program is free software: you can redistribute it and/or modify it
212 under the terms of the GNU General Public License as published by the
213 Free Software Foundation, either version 3 of the License, or (at your
214 option) any later version.
216 This program is distributed in the hope that it will be useful, but
217 WITHOUT ANY WARRANTY; without even the implied warranty of
218 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
219 See the GNU General Public License for more details.
221 You should have received a copy of the GNU General Public License along
222 with this program.
223 If not, see L<http://www.gnu.org/licenses/>.
225 =head1 SEE ALSO
227 =cut
229 # }}}
231 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :