Deleting the branches/gpst.near/ crap, it has its own branch
[gpstools.git] / branches / gpst.postgis / addpoints
blobe3fc4fc871136144f7a9a0f55ba8c5a1b2b176ca
1 #!/usr/bin/perl -w
3 #=======================================================================
4 # $Id$
5 # Add new waypoints or trackpoints to the database.
7 # Character set: UTF-8
8 # ©opyleft 2008– Ø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 our @version_array;
17 use strict;
18 use Getopt::Long;
20 $| = 1;
22 our $Debug = 0;
24 our %Std = (
26 'database' => "gps",
27 'timezone' => "",
28 'type' => "track",
32 our %Opt = (
34 'database' => $Std{'database'},
35 'debug' => 0,
36 'help' => 0,
37 'timezone' => $Std{'timezone'},
38 'type' => $Std{'type'},
39 'verbose' => 0,
40 'version' => 0,
41 'waypoint' => 0,
45 our $progname = $0;
46 $progname =~ s/^.*\/(.*?)$/$1/;
48 my $rcs_id = '$Id$';
49 my $id_date = $rcs_id;
50 $id_date =~ s/^.*?\d+ (\d\d\d\d-.*?\d\d:\d\d:\d\d\S+).*/$1/;
52 push(@main::version_array, $rcs_id);
54 Getopt::Long::Configure("bundling");
55 GetOptions(
57 "database|D=s" => \$Opt{'database'},
58 "debug" => \$Opt{'debug'},
59 "help|h" => \$Opt{'help'},
60 "timezone|T=s" => \$Opt{'timezone'},
61 "type|t=s" => \$Opt{'type'},
62 "verbose|v+" => \$Opt{'verbose'},
63 "version" => \$Opt{'version'},
64 "waypoint|w" => \$Opt{'waypoint'},
66 ) || die("$progname: Option error. Use -h for help.\n");
68 $Opt{'debug'} && ($Debug = 1);
69 $Opt{'help'} && usage(0);
70 if ($Opt{'version'}) {
71 print_version();
72 exit(0);
75 my $tz_str = "";
76 if (length($Opt{'timezone'})) {
77 if ($Opt{'timezone'} =~ /^[\+\-][0-2][0-9]{3}$/) {
78 $tz_str = $Opt{'timezone'};
79 } elsif ($Opt{'timezone'} =~ /^z$/i) {
80 $tz_str = $Opt{'timezone'};
81 } elsif ($Opt{'timezone'} =~ /^[a-z]+$/i) {
82 $tz_str = " $Opt{'timezone'}";
83 } else {
84 die("$progname: $Opt{'timezone'}: Invalid time zone\n");
88 for my $Currarg (@ARGV) {
89 for my $Currfile (glob($Currarg)) {
90 D("Currfile = '$Currfile'");
91 if ($Opt{'type'} =~ /picture/) {
92 if ($Currfile =~ /\.jpg$/i) {
93 my $tz_str = length($Opt{'timezone'})
94 ? "-T $Opt{'timezone'} "
95 : "";
96 my $exec_str =
97 "gpst-pic $tz_str$Currfile | psql -c \"COPY pictures (" .
98 join(", ",
99 "version",
100 "date",
101 "coor",
102 "descr",
103 "filename",
104 "author"
106 ") FROM stdin\" $Opt{'database'}";
107 msg(1, "Executing '$exec_str'...");
108 system($exec_str);
111 if ($Opt{'type'} =~ /track/) {
112 my $exec_str =
113 "gpst -o pgtab -d -rpt $Currfile | " .
114 "psql -a -c \"COPY logg (" .
115 join(", ",
116 "date",
117 "coor",
118 "ele",
119 "sted",
120 "dist",
121 "description",
122 "avst"
124 ") FROM stdin\" $Opt{'database'}";
125 msg(1, "Executing '$exec_str'...");
126 system($exec_str);
128 if ($Opt{'type'} =~ /waypoint/) {
129 my $exec_str =
130 "gpst -o pgwtab $Currfile | " .
131 "psql -a -c \"COPY wayp_new (" .
132 join(", ",
133 "coor",
134 "name",
135 "ele",
136 "type",
137 "time",
138 "cmt",
139 "descr",
140 "src",
141 "sym"
143 ") FROM stdin\" $Opt{'database'}";
144 msg(1, "Executing '$exec_str'...");
145 system($exec_str);
150 sub print_version {
151 # Print program version {{{
152 for (@main::version_array) {
153 print("$_\n");
155 # }}}
156 } # print_version()
158 sub usage {
159 # Send the help message to stdout {{{
160 my $Retval = shift;
162 if ($Opt{'verbose'}) {
163 print("\n");
164 print_version();
166 print(<<END);
168 Usage: $progname [options] [file [files [...]]]
170 Options:
172 -D X, --database X
173 Load into PostgreSQL database X. Default: "$Std{'database'}".
174 -h, --help
175 Show this help.
176 -T X, --timezone X
177 Prepend X as timezone to the date. Valid formats:
178 UTC offset
179 A '+' or '-' followed by a four-digit number (HHMM) which
180 indicates the offset relative to UTC. Examples:
181 +0000
182 -1600
183 +0630
184 Time zone abbreviation. Examples:
188 -t X, --type X
189 Comma-separated list of point types to extract from files:
190 picture
191 track
192 waypoint
193 Default: "$Std{'type'}".
194 -v, --verbose
195 Increase level of verbosity. Can be repeated.
196 --version
197 Print version information.
198 --debug
199 Print debugging messages.
202 exit($Retval);
203 # }}}
204 } # usage()
206 sub msg {
207 # Print a status message to stderr based on verbosity level {{{
208 my ($verbose_level, $Txt) = @_;
210 if ($Opt{'verbose'} >= $verbose_level) {
211 print(STDERR "$progname: $Txt\n");
213 # }}}
214 } # msg()
216 sub D {
217 # Print a debugging message {{{
218 $Debug || return;
219 my @call_info = caller;
220 chomp(my $Txt = shift);
221 my $File = $call_info[1];
222 $File =~ s#\\#/#g;
223 $File =~ s#^.*/(.*?)$#$1#;
224 print(STDERR "$File:$call_info[2] $$ $Txt\n");
225 return("");
226 # }}}
227 } # D()
229 __END__
231 # Plain Old Documentation (POD) {{{
233 =pod
235 =head1 NAME
239 =head1 REVISION
241 $Id$
243 =head1 SYNOPSIS
245 [options] [file [files [...]]]
247 =head1 DESCRIPTION
251 =head1 OPTIONS
253 =over 4
255 =item B<-h>, B<--help>
257 Print a brief help summary.
259 =item B<-v>, B<--verbose>
261 Increase level of verbosity. Can be repeated.
263 =item B<--version>
265 Print version information.
267 =item B<--debug>
269 Print debugging messages.
271 =back
273 =head1 BUGS
277 =head1 AUTHOR
279 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
281 =head1 COPYRIGHT
283 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
284 This is free software; see the file F<COPYING> for legalese stuff.
286 =head1 LICENCE
288 This program is free software; you can redistribute it and/or modify it
289 under the terms of the GNU General Public License as published by the
290 Free Software Foundation; either version 2 of the License, or (at your
291 option) any later version.
293 This program is distributed in the hope that it will be useful, but
294 WITHOUT ANY WARRANTY; without even the implied warranty of
295 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
296 See the GNU General Public License for more details.
298 You should have received a copy of the GNU General Public License along
299 with this program; if not, write to the Free Software Foundation, Inc.,
300 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
302 =head1 SEE ALSO
304 =cut
306 # }}}
308 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
309 # End of file $Id$