-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGeo3x3.php
69 lines (65 loc) · 1.32 KB
/
Geo3x3.php
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
<?php
class Geo3x3 {
public static function encode(float $lat, float $lng, int $level): string
{
if ($level < 1) {
return '';
}
if ($lng >= 0) {
$res = 'E';
} else {
$res = 'W';
$lng += 180;
}
$lat += 90;
$unit = 180.0;
for ($i = 1; $i < $level; $i++) {
$unit /= 3;
$x = (int)($lng / $unit);
$y = (int)($lat / $unit);
$res = $res . chr(ord('0') + $x + $y * 3 + 1);
$lng -= $x * $unit;
$lat -= $y * $unit;
}
return $res;
}
/**
* @phpstan-return array{float, float, positive-int, float}
*/
public static function decode(string $code): array
{
$c = $code[0];
$begin = 0;
$flg = false;
if (in_array($c, ['-', 'W'], true)) {
$flg = true;
$begin++;
} elseif (in_array($c, ['E', '+'], true)) {
$begin++;
}
$unit = 180.0;
$lat = 0.0;
$lng = 0.0;
$level = 1;
$clen = strlen($code);
for ($i = $begin; $i < $clen; $i++) {
$c = $code[$i];
$n = (int)$c;
if ($n == 0) {
break;
}
$unit /= 3;
$n--;
$lng += $n % 3 * $unit;
$lat += intdiv($n, 3) * $unit;
$level++;
}
$lat += $unit / 2;
$lng += $unit / 2;
$lat -= 90;
if ($flg) {
$lng -= 180.0;
}
return [$lat, $lng, $level, $unit];
}
}