-
Notifications
You must be signed in to change notification settings - Fork 7
/
any2bbd
executable file
·348 lines (323 loc) · 8.79 KB
/
any2bbd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/perl -w
# -*- perl -*-
#
# $Id: any2bbd,v 1.26 2009/01/16 21:34:23 eserte Exp $
# Author: Slaven Rezic
#
# Copyright (C) 2005,2006 Slaven Rezic. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Mail: slaven@rezic.de
# WWW: http://www.rezic.de/eserte/
#
use strict;
use FindBin;
use lib ("$FindBin::RealBin/..",
"$FindBin::RealBin/../lib",
);
use Hash::Util qw(lock_keys);
use Archive::Zip qw(:ERROR_CODES);
use File::Temp qw(tempfile);
use File::Basename qw(basename dirname);
use Getopt::Long;
use Strassen::Gpsman;
use Strassen::Core;
use Strassen::MultiStrassen;
# XXX problematisch: die .g7t-Waypoints-Dateien, die Zeilen ohne Koordinaten erzeugen!
# Note: if using any2bbd with -info ... and -append, then make sure that the
# first file wrote a global directive preamble. The easiest is to call any2bbd
# first without any files in non-append mode.
my %opt;
GetOptions(\%opt,
"dir=s",
'exclude=s@',
'excluderx=s@',
"o=s",
"append|a!",
"inputformat|i=s",
"routenamefmt=s",
"infotobbd!",
"info=s",
"noempty",
)
or die <<EOF;
usage: $0 [-i inputformat] -o outfile [-a|-append]
[-routenamefmt ...] [-dir download-dir | files ...]
[-exclude basename [-exclude ...]]
[-excluderx rx [-excluderx ...]]
[-info file]
[-infotobbd]
[-noempty]
EOF
my $download_dir; $download_dir = $opt{dir} if defined $opt{dir};
my $outfile = $opt{o} || die "Missing option -o for output file, use -o - for stdout";
my $routenamefmt = $opt{routenamefmt} || "%b";
my %exclude = map {($_,1)} @{ $opt{exclude} || [] };
my $excludebyrx = sub {
return if !$opt{excluderx};
for (@{ $opt{excluderx} }) {
return 1 if ($_[0] =~ m{$_}i);
}
0;
};
my $do_infotobbd = $opt{infotobbd} || 0;
my $file_info = $opt{info};
my %file_info;
if ($file_info) {
require YAML;
%file_info = %{ YAML::LoadFile($file_info) };
}
my @files = @ARGV;
if (!@files) {
if (defined $download_dir) {
@files = glob("$download_dir/*");
}
}
my @s;
for my $file (@files) {
my $basename = basename($file);
next if $exclude{$basename};
next if $excludebyrx->($file);
print STDERR "$file... ";
my %info = (handled_by => "?", file => $file, subfile => undef);
lock_keys %info;
my %this_file_info = %{ $file_info{$basename} || {} };
# do not lock %this_file_info
my $check_s = sub {
my $s = shift;
if ($s && $s->data && @{ $s->data }) {
print STDERR "OK ($info{handled_by})\n";
1;
} else {
print STDERR "no (usable) GPS data\n";
0;
}
};
eval {
if ($file =~ /\.zip$/i) {
my $zip = Archive::Zip->new;
$zip->read($file) == AZ_OK or die;
print STDERR "\n";
for my $member ($zip->members) {
next if $exclude{ basename($member->fileName) };
next if $excludebyrx->($member->fileName);
print STDERR " " . $member->fileName . "... ";
$info{subfile} = $member->fileName;
my($suffix) = $member->fileName =~ /(\.[^\.]+)$/;
my($tmpfh,$tmpfile) = tempfile(UNLINK => 1,
SUFFIX => $suffix,
);
print $tmpfh ($member->contents)[0];
close $tmpfh;
my $s = run_gpsbabel($tmpfile,
title => routenamefmt($file . "/" . basename($member->fileName)),
info => \%info,
file_info => \%this_file_info,
);
if ($check_s->($s)) {
prepend_info($s, \%info);
push @s, $s;
}
}
} else {
my $work_file = $file;
if ($file =~ m{\.bz2$}) {
my($suffix) = $file =~ m{\.([^.]+)\.bz2$};
my($tmpfh,$tmpfile) = tempfile(UNLINK => 1,
(defined $suffix ? (SUFFIX => $suffix) : ()),
);
open my $fh, "-|", "bzcat", $file
or die "Can't run bzcat $file: $!";
local $/ = \8192;
while(<$fh>) {
print $tmpfh $_;
}
close $tmpfh
or die "Error while writing to temporary file $tmpfile: $!";
$work_file = $tmpfile;
}
if (-f $work_file) {
my $s = run_gpsbabel($work_file,
title => routenamefmt($file),
info => \%info,
file_info => \%this_file_info,
);
if ($check_s->($s)) {
prepend_info($s, \%info);
push @s, $s;
}
} elsif (-d $work_file) {
print STDERR "skipping directory\n";
} else {
print STDERR "skipping non-existent file '$work_file'\n";
}
}
};
warn $@ if $@;
}
if (!@s && $opt{noempty}) {
warn "No usable input files (tried @files), do not write anything...\n";
exit 0;
}
my $ms = MultiStrassen->new(@s);
if ($opt{append}) {
$ms->append($outfile);
} else {
$ms->set_global_directive("category_dash.AccManually" => "3,8");
$ms->set_global_directive("category_color.AccManually" => "#8080c0");
$ms->set_global_directive("category_dash.AccLow" => "2,4");
$ms->set_global_directive("category_color.AccLow" => "#4040a0");
$ms->write($outfile);
}
# XXX use GPS::Gpsbabel::run_gpsbabel instead, once that one
# has all the features of this function (accuracy cat handling etc.)
sub run_gpsbabel {
my($file, %args) = @_;
my $title = $args{title} || $file;
my $info = $args{info} || {};
my $file_info = $args{file_info} || {};
my $cat = "#000080";
if ($file_info->{accuracy}) {
if ($file_info->{accuracy} eq 'manually') {
$cat = "AccManually";
} elsif ($file_info->{accuracy} eq 'low') {
$cat = "AccLow";
} elsif ($file_info->{accuracy} eq 'unusable') {
return undef;
}
}
if ($file_info->{maptitle}) {
$title = $file_info->{maptitle};
}
if ($opt{inputformat}) {
my $s = eval {
require Strassen::FromRoute;
Strassen::FromRoute->new($file, name => $title);
};
if ($s) {
$info->{handled_by} = "Strassen::FromRoute";
return $s;
}
}
if ($file =~ m{( \.(dbf|sbn|sbx|shp|shx)$
| \.(mif|mid)$
| \.e00$
| \.mps$
| \.g7t$
| \.gpx$
| \.kml$
| \.kmz$
| \.ovl$
| \.gpx$
)
}xi) {
my $s = eval { Strassen->new($file, name => $title, cat => $cat) };
if ($s) {
$info->{handled_by} = ref $s;
return $s;
} else {
warn $@;
}
}
my $magic_line;
if (open(my $fh, $file)) {
$magic_line = <$fh>;
if ($magic_line =~ /^%.*(gpsmanager|gps::gpsmanconn)/i) {
close $fh;
my $s = Strassen::Gpsman->new($file,
cat => $cat,
fallbackname => $title);
if ($s) {
$info->{handled_by} = "Strassen::Gpsman";
return $s;
}
}
}
if ((defined $magic_line && $magic_line =~ m{<\?xml.*?><workout>}) # do a real match
|| $file =~ m{\.xml\.gz$} # and this is just lazyness
) {
require GPS::GpsmanData::SportsTracker;
my $gpsman = GPS::GpsmanData::SportsTracker->load($file);
my($tmpfh,$tmpfile) = tempfile(SUFFIX => ".trk", UNLINK => 1);
print $tmpfh $gpsman->as_string;
close $tmpfh
or die "While writing temporary file: $!";
my $s = Strassen::Gpsman->new($tmpfile,
cat => $cat,
fallbackname => $title);
if ($s) {
$info->{handled_by} = "GPS::GpsmanData::SportsTracker and Strassen::Gpsman";
return $s;
}
}
# XXX should really check magic before trying, i.e. 2nd line should match
# /^I\s+PCX5/
my $in_fmt = "pcx";
my $out_fmt;
if (eval { require Strassen::GPX; 1 }) {
# Prefer gpx output, because gpsbabel's pcx->gpx conversion is
# better than pcx->gpsman.
#
# Downside: handling GPX is *MUCH* slower
$out_fmt = "gpx";
} else {
$out_fmt = "gpsman";
}
if ($file =~ /\.gpx$/) {
$in_fmt = "gpx";
$out_fmt = "gpsman";
}
my($ofh,$ofilename) = tempfile(UNLINK => 1,
SUFFIX => "_any2bbd.$out_fmt");
system("gpsbabel", "-t",
"-i", $in_fmt, "-f", $file,
"-o", $out_fmt, -F, $ofilename);
$info->{handled_by} = "gpsbabel";
if ($out_fmt eq 'gpsman') {
# Hack: set track name
my($o2fh,$o2filename) = tempfile(UNLINK => 1);
open(F, $ofilename) or die $!;
while(<F>) {
s/(^!T:)/$1\t$title/;
print $o2fh $_;
}
close F;
close $o2fh;
$ofilename = $o2filename;
}
my $s;
if ($out_fmt eq 'gpsman') {
$s = Strassen::Gpsman->new($ofilename,
cat => $cat,
fallbackname => $title);
$info->{handled_by} .= " and Strassen::Gpsman";
} elsif ($out_fmt eq 'gpx') {
$s = eval { Strassen->new($ofilename,
cat => $cat,
name => $title)
};
$info->{handled_by} .= " and " . ref($s);
} else {
die "Shouldn't happen: out_fmt=<$out_fmt>";
}
$s;
}
sub routenamefmt {
my($file) = @_;
my $rv = $routenamefmt;
$rv =~ s{%b}{ basename($file) }ge;
$rv =~ s{%f}{ $file }g;
$rv =~ s{%d}{ basename(dirname($file)) }ge;
$rv =~ s{%D}{ dirname($file) }ge;
$rv;
}
# XXX Should go to Strassen::?
sub prepend_info {
return if !$do_infotobbd;
my($s, $info) = @_;
unshift @{ $s->data },
"# source file: $info->{file}" . ($info->{subfile} ? "#$info->{subfile}" : "") . "\n",
"# handled_by: $info->{handled_by}\n";
}
__END__