-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgeo3x3.ceylon
69 lines (66 loc) · 1.44 KB
/
geo3x3.ceylon
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
Float floor(Float n) {
dynamic {
return \iMath.floor(n);
}
}
shared String encode(variable Float lat, variable Float lng, Integer level) {
variable value res = "E";
if (lng < 0.0) {
res = "W";
lng += 180.0;
}
lat += 90.0; // 180:the North Pole, 0:the South Pole
variable value unit = 180.0;
for (i in 1:level - 1) {
unit /= 3.0;
value x = floor(lng / unit);
value y = floor(lat / unit);
value c = "0123456789"[(x + y * 3 + 1).integer];
if (is Character c) {
res = res + "``c``";
}
lng -= x * unit;
lat -= y * unit;
}
return res;
}
shared [Float, Float, Integer, Float] decode(String code) {
variable value flg = false;
variable value begin = 0;
value c = code[0];
if (is Character c) {
if (c == 'W') {
flg = true;
begin = 1;
} else if (c == 'E') {
begin = 1;
}
}
print(begin);
variable value unit = 180.0;
variable value lat = 0.0;
variable value lng = 0.0;
variable value level = 1;
for (i in begin:code.size - 1) {
value c2 = code[i];
variable Integer n = 0;
if (is Character c2) {
n = c2.integer - 49;
}
unit /= 3.0;
lng += (n % 3) * unit;
lat += (n / 3) * unit;
level++;
}
lat += unit / 2.0;
lng += unit / 2.0;
lat -= 90.0;
if (flg) {
lng -= 180.0;
}
return [lat, lng, level, unit];
}
shared void main() {
print(encode(35.65858, 139.745433, 14));
print(decode("E9139659937288"));
}