-
Notifications
You must be signed in to change notification settings - Fork 6
/
poly2bb.pl
68 lines (62 loc) · 1.88 KB
/
poly2bb.pl
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
#!/usr/bin/perl
# script to extract a polygon file's bbox
# output is in a form suitable for the Mapsforge map-writer "bbox" option
# written by Frederik Ramm <[email protected]>, public domain
# adapted by devemux86
$maxx[0] = -360;
$maxy[0] = -360;
$minx[0] = 360;
$miny[0] = 360;
$area = -1;
while(<>) {
if (/^\d+$/) {
$area++;
$maxx[$area] = -360;
$maxy[$area] = -360;
$minx[$area] = 360;
$miny[$area] = 360;
} elsif (/^\s+([0-9.E+-]+)\s+([0-9.E+-]+)\s*$/) {
my ($x, $y) = ($1, $2);
$maxx[$area] = $x if ($x > $maxx[$area]);
$maxy[$area] = $y if ($y > $maxy[$area]);
$minx[$area] = $x if ($x < $minx[$area]);
$miny[$area] = $y if ($y < $miny[$area]);
}
}
if ($area > 0) {
$west = 0;
for (my $i = 0; $i <= $area; $i++) {
if ($minx[$i] > 0 && $maxx[$i] > 0) {
$west = 1;
last;
}
}
$east = 0;
for (my $i = 0; $i <= $area; $i++) {
if ($minx[$i] < 0 && $maxx[$i] < 0) {
$east = 1;
last;
}
}
if ($west == 0 || $east == 0) {
for (my $i = 0; $i <= $area; $i++) {
$maxx[0] = $maxx[$i] if ($maxx[$i] > $maxx[0]);
$maxy[0] = $maxy[$i] if ($maxy[$i] > $maxy[0]);
$minx[0] = $minx[$i] if ($minx[$i] < $minx[0]);
$miny[0] = $miny[$i] if ($miny[$i] < $miny[0]);
}
$area = 0;
}
}
$buffer = 0.1;
for (my $i = 0; $i <= $area; $i++) {
$miny[$i] = $miny[$i] - $buffer;
$miny[$i] = $miny[$i] < -90 ? -90 : $miny[$i];
$minx[$i] = $minx[$i] - $buffer;
$minx[$i] = $minx[$i] < -180 ? -180 : $minx[$i];
$maxy[$i] = $maxy[$i] + $buffer;
$maxy[$i] = $maxy[$i] > 90 ? 90 : $maxy[$i];
$maxx[$i] = $maxx[$i] + $buffer;
$maxx[$i] = $maxx[$i] > 180 ? 180 : $maxx[$i];
printf "%f,%f,%f,%f\n", $miny[$i], $minx[$i], $maxy[$i], $maxx[$i];
}