-
Notifications
You must be signed in to change notification settings - Fork 16
/
geo3x3.pm
74 lines (70 loc) · 1.28 KB
/
geo3x3.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
package geo3x3;
use Exporter;
@ISA = (Exporter);
@EXPORT = qw(encode, decode);
use strict;
use warnings;
use POSIX;
sub encode {
my $lat = $_[0];
my $lng = $_[1];
my $level = $_[2];
if ($level < 1) {
return "";
}
my $res = "";
if ($lng >= 0.0) {
$res = "E";
} else {
$res = "W";
$lng += 180.0;
}
$lat += 90.0;
my $unit = 180.0;
for (my $i = 1; $i < $level; $i++) {
$unit /= 3;
my $x = floor($lng / $unit);
my $y = floor($lat / $unit);
$res = $res . ($x + $y * 3 + 1);
$lng -= $x * $unit;
$lat -= $y * $unit;
}
return $res;
}
sub decode {
my $code = $_[0];
my $c = substr($code, 0, 1);
my $begin = 0;
my $flg = 0;
if ($c eq "-" or $c eq "W") {
$flg = 1;
$begin = 1;
} elsif ($c eq "+" or $c eq "E") {
$begin = 1;
}
my $unit = 180.0;
my $lat = 0.0;
my $lng = 0.0;
my $level = 1;
my $cc;
my $n;
for (my $i = $begin; $i < length $code; $i++) {
$cc = unpack("C", substr($code, $i, $i + 1));
$n = $cc - 48;
if ($n == 0) {
last;
}
$unit /= 3;
$n--;
$lng += floor($n % 3) * $unit;
$lat += floor($n / 3) * $unit;
$level++;
}
$lat += $unit / 2;
$lng += $unit / 2;
$lat -= 90.0;
if ($flg) {
$lng -= 180.0;
}
return ($lat, $lng, $level, $unit);
}