gpst: Legger inn en Postgres-patch som har med en imginfo-funksjon å gjøre. Uferdig.
[gpstools.git] / csv2gpx
blob9ac1bf5f27f101c69aacfd1c8e0fa9ae5ee7d39e
1 #!/usr/bin/perl -w
3 #=======================================================================
4 # $Id$
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 2 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
14 BEGIN {
15 our @version_array;
18 use strict;
19 use Getopt::Long;
21 $| = 1;
23 our $Debug = 0;
25 our %Opt = (
27 'debug' => 0,
28 'help' => 0,
29 'verbose' => 0,
30 'version' => 0,
34 our $progname = $0;
35 $progname =~ s/^.*\/(.*?)$/$1/;
37 my $rcs_id = '$Id$';
38 my $id_date = $rcs_id;
39 $id_date =~ s/^.*?\d+ (\d\d\d\d-.*?\d\d:\d\d:\d\d\S+).*/$1/;
41 push(@main::version_array, $rcs_id);
43 Getopt::Long::Configure("bundling");
44 GetOptions(
46 "debug" => \$Opt{'debug'},
47 "help|h" => \$Opt{'help'},
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 my $DIGIT = '0-9\.\-\+';
62 print(gpx_header());
64 while (<>) {
65 my ($Lat, $Lon, $Name, $Phone) = ("", "", "", "");
66 if (/^([$DIGIT]+),([$DIGIT]+),(.*) - (.*?)$/) {
67 ($Lon, $Lat, $Name, $Phone) =
68 (txt_to_xml($1), txt_to_xml($2), txt_to_xml($3), txt_to_xml($4));
69 print(<<END);
70 <wpt lat="$Lat" lon="$Lon">
71 <name>$Name</name>
72 <cmt>$Name - $Phone</cmt>
73 </wpt>
74 END
75 } else {
76 warn(sprintf("%s: Unknown line: \"%s\"\n", $progname, chomp($_)));
80 print("</gpx>\n");
82 sub txt_to_xml {
83 # Convert plain text to XML {{{
84 my $Txt = shift;
85 $Txt =~ s/&/&amp;/gs;
86 $Txt =~ s/</&lt;/gs;
87 $Txt =~ s/>/&gt;/gs;
88 return($Txt);
89 # }}}
92 sub gpx_header {
93 # {{{
94 return(<<END);
95 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
96 <!-- \$Id\$ -->
97 <gpx
98 version="1.1"
99 creator="csv2gpx - http://svn.sunbase.org/repos/utils/trunk/csv2gpx"
100 xmlns="http://www.topografix.com/GPX/1/1"
101 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
102 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
105 # }}}
108 sub print_version {
109 # Print program version {{{
110 for (@main::version_array) {
111 print("$_\n");
113 # }}}
114 } # print_version()
116 sub usage {
117 # Send the help message to stdout {{{
118 my $Retval = shift;
120 if ($Opt{'verbose'}) {
121 print("\n");
122 print_version();
124 print(<<END);
126 Usage: $progname [options] [file [files [...]]]
128 Options:
130 -h, --help
131 Show this help.
132 -v, --verbose
133 Increase level of verbosity. Can be repeated.
134 --version
135 Print version information.
136 --debug
137 Print debugging messages.
140 exit($Retval);
141 # }}}
142 } # usage()
144 sub msg {
145 # Print a status message to stderr based on verbosity level {{{
146 my ($verbose_level, $Txt) = @_;
148 if ($Opt{'verbose'} >= $verbose_level) {
149 print(STDERR "$progname: $Txt\n");
151 # }}}
152 } # msg()
154 sub D {
155 # Print a debugging message {{{
156 $Debug || return;
157 my @call_info = caller;
158 chomp(my $Txt = shift);
159 my $File = $call_info[1];
160 $File =~ s#\\#/#g;
161 $File =~ s#^.*/(.*?)$#$1#;
162 print(STDERR "$File:$call_info[2] $$ $Txt\n");
163 return("");
164 # }}}
165 } # D()
167 __END__
169 # Plain Old Documentation (POD) {{{
171 =pod
173 =head1 NAME
177 =head1 REVISION
179 $Id$
181 =head1 SYNOPSIS
183 [options] [file [files [...]]]
185 =head1 DESCRIPTION
189 =head1 OPTIONS
191 =over 4
193 =item B<-h>, B<--help>
195 Print a brief help summary.
197 =item B<-v>, B<--verbose>
199 Increase level of verbosity. Can be repeated.
201 =item B<--version>
203 Print version information.
205 =item B<--debug>
207 Print debugging messages.
209 =back
211 =head1 BUGS
215 =head1 AUTHOR
217 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
219 =head1 COPYRIGHT
221 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
222 This is free software; see the file F<COPYING> for legalese stuff.
224 =head1 LICENCE
226 This program is free software; you can redistribute it and/or modify it
227 under the terms of the GNU General Public License as published by the
228 Free Software Foundation; either version 2 of the License, or (at your
229 option) any later version.
231 This program is distributed in the hope that it will be useful, but
232 WITHOUT ANY WARRANTY; without even the implied warranty of
233 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
234 See the GNU General Public License for more details.
236 You should have received a copy of the GNU General Public License along
237 with this program; if not, write to the Free Software Foundation, Inc.,
238 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
240 =head1 SEE ALSO
242 =cut
244 # }}}
246 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
247 # End of file $Id$