-
Notifications
You must be signed in to change notification settings - Fork 7
/
Karte.pm
354 lines (299 loc) · 8.02 KB
/
Karte.pm
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
349
350
351
352
353
# -*- perl -*-
#
# Author: Slaven Rezic
#
# Copyright (C) 1998-2002,2024 Slaven Rezic. All rights reserved.
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License, see the file COPYING.
#
# Mail: slaven@rezic.de
# WWW: http://bbbike.de
#
package Karte;
#use AutoLoader 'AUTOLOAD';
our $VERSION = '1.43';
use strict;
use vars qw(%map %map_by_modname %map_by_coordsys @map $cache_root $map_root);
if (!defined $cache_root) {
$cache_root = $ENV{TMPDIR} || $ENV{TEMP} || '/tmp';
}
if (!defined $map_root) {
$map_root = "/usr/www/berlin";
}
sub preload {
my(@types) = @_;
if ($types[0] eq ':all') {
@types = qw(Standard
Berlinmap1996 Berlinmap1997 Berlinmap1998 Berlinmap1999
Berlinmap2000 Berlinmap2001 Berlinmap2002 Berlinmap2003
Berlinmap2004
Potsdammap2002 Demap2002 Nbrbmap2004
Satmap SatmapGIF GISmap Polar T99 T2001 GDF
GPS Soldner_alt Cityinfo PilotPl PilotPl12 Tk50
Deinplan
);
}
my $karte;
foreach $karte (@types) {
my $module = "Karte::$karte";
eval "require $module";
if ($@) {
warn $@;
} else {
my $o = eval "\$" . $module . "::obj";
next if !$o or exists $map{$o->token};
push @map, $o->token;
$map{$o->token} = $o;
$map_by_modname{$karte} = $o;
if (defined $o->coordsys) {
## sometimes this is legal:
# if (exists $map_by_coordsys{$o->coordsys}) {
# warn "Multiple use of Coordsys <" . $o->coordsys . ">";
# }
$map_by_coordsys{$o->coordsys} = $o;
}
}
}
}
sub name { shift->{Name} }
sub token { shift->{Token} }
sub mimetype { shift->{Mimetype} }
sub coordsys { shift->{Coordsys} }
sub map_root { $map_root }
sub fs_dir {
my $o = shift;
if (defined $o->{Fs_dir}) {
$o->{Fs_dir};
} elsif (defined $o->{Fs_base}) {
$o->map_root . "/" . $o->{Fs_base};
} else {
undef;
}
}
sub cache_dir { shift->{Cache_dir} }
sub root_url { shift->{Root_URL} }
sub x0 { shift->{X0} }
sub x1 { shift->{X1} }
sub x2 { shift->{X2} }
sub y0 { shift->{Y0} }
sub y1 { shift->{Y1} }
sub y2 { shift->{Y2} }
sub width { shift->{Width} }
sub height { shift->{Height} }
sub scrollregion { @{shift->{Scrollregion}} }
# gibt an, ob die Karte mit Umgebung gezeichnet werden kann
sub noenvironment { shift->{NoEnvironment} }
sub to_ppm {
my $self = shift;
($self->mimetype eq 'image/gif'
? 'giftopnm' # giftoppm gibt's bei netpbm nicht
: ($self->mimetype eq 'image/jpeg'
? 'djpeg'
: ($self->mimetype eq 'image/png'
? 'pngtopnm'
: ($self->mimetype eq 'image/tiff'
? 'tifftopnm'
: ($self->mimetype =~ m|^image/x-portable-.*map$|
? 'cat'
: die "Unknown to_ppm for " . $self->mimetype
)
)
)
)
);
}
sub ext {
my $self = shift;
($self->mimetype eq 'image/gif'
? 'gif'
: ($self->mimetype eq 'image/jpeg'
? 'jpg'
: ($self->mimetype eq 'image/png'
? 'png'
: die "Unknown extension for " . $self->mimetype
)
)
);
}
# Erhöhen/Erniedrigen der Kartenkoordinaten
sub incx {
my(undef, $c, $inc) = @_;
$c + $inc;
}
sub incy {
my(undef, $c, $inc) = @_;
$c + $inc;
}
# Die ..._s-Formen erwarten und liefern ein Skalar "x,y"
# Transformationen von und zum normalen Koordinatensystem
sub map2standard {
my($self, $oldx, $oldy) = @_;
($self->x0 + $oldx*$self->x1 + $oldy*$self->x2,
$self->y0 + $oldx*$self->y1 + $oldy*$self->y2
);
}
sub map2standard_s {
my($self, $old) = @_;
# do not use trim_accuracy here ... standard is always int
join(",", map { int } $self->map2standard(split /,/, $old));
}
sub standard2map {
my($self, $newx, $newy) = @_;
((($newx-$self->x0)*$self->y2-($newy-$self->y0)*$self->x2)/
($self->x1*$self->y2-$self->y1*$self->x2),
(($newx-$self->x0)*$self->y1-($newy-$self->y0)*$self->x1)/
($self->x2*$self->y1-$self->x1*$self->y2)
);
}
sub standard2map_s {
my($self, $new) = @_;
join(",", $self->trim_accuracy($self->standard2map(split /,/, $new)));
}
# Transformation zwischen beliebigen Koordinatensystemen
sub map2map {
my($from, $to, $oldx, $oldy) = @_;
$to->standard2map($from->map2standard($oldx, $oldy));
}
sub map2map_s {
my($from, $to, $old) = @_;
join(",", $to->trim_accuracy($from->map2map($to, split /,/, $old)));
}
sub trim_accuracy {
my(undef, $x, $y) = @_;
(int($x), int($y));
}
# Return the rotating angle between maps XXX NYI
sub rotate_angle {
# my($self, $map) = @_;
# my($tx1,$ty1) = $self->map2map($map, 0, 0);
# my($tx2,$ty2) = $self->map2map($map, 1000, 0);
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
}
sub scale_coeff {
my $self = shift;
my($x0, $y0) = $self->map2standard(0, 0);
my($x1, $y1) = $self->map2standard(1, 0);
sqrt(($x1-$x0)*($x1-$x0) + ($y1-$y0)*($y1-$y0));
}
# statische Funktion
# gibt für ein Coordsys (Koordinaten-Abkürzung) das Karten-Objekt zurück
sub coordsys2obj {
my($coordsys) = @_;
my($k, $v);
while(($k,$v) = each %Karte::map) {
my $match_coordsys = $v->coordsys;
return $v if (defined $match_coordsys &&
$match_coordsys eq $coordsys);
}
undef;
}
# create a Karte object without a class
# %args should at least contain X0 => ... to Y2 => ...
sub create_obj {
my($class, %args) = @_;
eval '@' . $class . '::ISA = "Karte"'; die $@ if $@;
my $obj = { %args };
bless $obj, $class;
$obj;
}
# XXX move to "Heavy" module?
sub object_from_file {
my $file = shift;
_object_from_file(-datafromfile => $file);
}
sub object_from_bbd_file {
my $bbd_file = shift;
_object_from_file(-datafrombbd => $bbd_file);
}
sub object_from_any_file {
my $bbd_file = shift;
_object_from_file(-datafromany => $bbd_file);
}
# In argument: [["x,y", "x,y"], ...] where first element is the standard coord
# and the second element the custom map coord
sub object_from_data {
my $array = shift;
require File::Temp;
my($tmpfh,$tmpfile) = File::Temp::tempfile(UNLINK => 1);
print $tmpfh join("\n", map { join " ", @$_ } @$array), "\n";
close $tmpfh;
my $k_obj = _object_from_file(-datafromfile => $tmpfile);
unlink $tmpfile;
$k_obj;
}
sub _object_from_file {
my(@args) = @_;
my $cmd = "$FindBin::RealBin/convert_berlinmap.pl";
if (!-r $cmd) {
$cmd = "$FindBin::RealBin/miscsrc/convert_berlinmap.pl";
}
my $res = `$^X $cmd @args -bbbike`;
if (!$res) {
die "Error while running convert_berlinmap?";
}
my %args;
foreach my $l (split /\n/, $res) {
if ($l =~ /([XY]\d)\s*=>\s*(.*),/) {
$args{$1} = $2;
}
}
foreach my $key (qw(X0 X1 X2 Y0 Y1 Y2)) {
if (!exists $args{$key}) {
warn "$key is missing while converting @args";
}
}
my $k_obj = Karte::create_obj("Karte::Custom", %args);
$k_obj;
}
return 1 if caller;
require Getopt::Long;
Karte::preload(":all");
my($frommap) = "standard";
my($tomap) = "standard";
if (!Getopt::Long::GetOptions("from=s" => \$frommap,
"to=s" => \$tomap,
)) {
usage();
}
my $conv = sub {
my $coord = shift;
join(",", $Karte::map{$tomap}->trim_accuracy($Karte::map{$frommap}->map2map($Karte::map{$tomap},
split/,/, $coord)));
};
if (@ARGV) {
print join(' ', map { $conv->($_) } @ARGV), "\n";
} else {
while(<>) {
chomp;
my $c = $_;
print $conv->($c), "\n";
}
}
sub usage {
Karte::preload(":all");
my $valid_maps = join("\n", map { "- $_ (" . $Karte::map{$_}->name . ")" } sort keys %Karte::map);
die <<EOF;
Usage: $^X $0 [-from map] [-to map] -- x,y [x,y ...]
Where map is any of:
$valid_maps
EOF
}
__END__
=head1 NAME
Karte - conversions between map sets
=head1 SYNOPSIS
In a script:
use Karte;
Karte::preload(":all");
my $frommap = $Karte::map{"standard"};
my $tomap = $Karte::map{"polar"};
($x_polar, $y_polar) = $frommap->map2map($tomap, $x_standard, $y_standard);
From command line:
perl Karte.pm -from standard -to polar -- x,y
For a list of all possible maps use
keys(%Karte::map)
=head1 AUTHOR
Slaven Rezic
=head1 SEE ALSO
L<convert_coordsys>
=cut