-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGeo3x3.ja.mjs
executable file
·72 lines (71 loc) · 1.84 KB
/
Geo3x3.ja.mjs
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
class Geo3x3 {
static エンコード(緯度, 経度, レベル) {
if (レベル < 1) {
return null;
}
if (typeof 緯度 == "string") {
緯度 = parseFloat(緯度);
}
if (typeof 経度 == "string") {
経度 = parseFloat(経度);
}
let コード = "E";
if (経度 < 0.0) {
コード = "W";
経度 += 180.0;
}
緯度 += 90.0; // 180:the North Pole, 0:the South Pole
let ユニット = 180.0;
for (let i = 1; i < レベル; i++) {
ユニット /= 3.0;
const x = Math.floor(経度 / ユニット);
const y = Math.floor(緯度 / ユニット);
コード += x + y * 3 + 1;
経度 -= x * ユニット;
緯度 -= y * ユニット;
}
return コード;
}
static デコード(コード) {
if (!コード || typeof コード !== "string" || !コード.length) {
return null;
}
let フラグ = false;
let 先頭 = 0;
const c = コード.charAt(0);
if (c == "W") {
フラグ = true;
先頭 = 1;
} else if (c == "E") {
先頭 = 1;
} else {
return null;
}
let ユニット = 180.0;
let 緯度 = 0.0;
let 経度 = 0.0;
let レベル = 1;
for (let i = 先頭; i < コード.length; i++) {
let n = "0123456789".indexOf(コード.charAt(i));
if (n == 0) {
break;
}
if (n < 0) {
return null; // err
}
ユニット /= 3;
n--;
経度 += (n % 3) * ユニット;
緯度 += Math.floor(n / 3) * ユニット;
レベル++;
}
緯度 += ユニット / 2;
経度 += ユニット / 2;
緯度 -= 90.0;
if (フラグ) {
経度 -= 180.0;
}
return { 緯度, 経度, レベル, ユニット };
}
}
export { Geo3x3 };