-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGeo3x3.d
68 lines (65 loc) · 1.25 KB
/
Geo3x3.d
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
import std.string;
import std.math;
bool encode(double lat, double lng, int level, char[] code) {
auto idx = 0;
auto unit = 180.0;
if (level < 1) {
return false;
}
if (!code || code.length != level) {
return false;
}
if (lng < 0.0) {
code[idx] = 'W';
lng += 180.0;
} else {
code[idx] = 'E';
}
idx++;
lat += 90.0;
for (auto i = 1; i < level; i++) {
unit /= 3;
const x = cast(int)(lng / unit);
const y = cast(int)(lat / unit);
code[idx++] = '0' + x + (y * 3) + 1;
lng -= x * unit;
lat -= y * unit;
}
return true;
}
auto decode(string code) {
if (!code || code.length < 1) {
return null;
}
auto flg = false;
auto begin = 0;
const c = code[0];
if (c == '-' || c == 'W') {
flg = true;
begin = 1;
} else if (c == '+' || c == 'E') {
begin = 1;
}
double unit = 180.0;
double lat = 0.0;
double lng = 0.0;
auto level = 1;
for (auto i = begin; i < code.length; i++) {
auto n = std.string.indexOf("0123456789", code[i]);
if (n == 0) {
break;
}
unit /= 3;
n--;
lng += n % 3 * unit;
lat += n / 3 * unit;
level++;
}
lat += unit / 2;
lng += unit / 2;
lat -= 90.0;
if (flg) {
lng -= 180.0;
}
return [ lat, lng, level, unit ];
}