-
Notifications
You must be signed in to change notification settings - Fork 16
/
Geo3x3.cs
63 lines (62 loc) · 1.26 KB
/
Geo3x3.cs
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
using System;
using System.Text;
public class Geo3x3 {
public static String encode(double lat, double lng, int level) {
if (level < 1)
return null;
StringBuilder res = new StringBuilder();
if (lng >= 0) {
res.Append('E');
} else {
res.Append('W');
// res.append('-');
lng += 180;
}
lat += 90;
double unit = 180;
for (int i = 1; i < level; i++) {
unit /= 3;
int x = (int)(lng / unit);
int y = (int)(lat / unit);
res.Append((char)('0' + x + y * 3 + 1));
lng -= x * unit;
lat -= y * unit;
}
return res.ToString();
}
// lat, lng, level, unit
public static double[] decode(String code) {
if (code == null || code.Length == 0)
return null;
int begin = 0;
bool flg = false;
char c = code[0];
if (c == '-' || c == 'W') {
flg = true;
begin = 1;
} else if (c == '+' || c == 'E') {
begin = 1;
}
double unit = 180;
double lat = 0;
double lng = 0;
int level = 1;
int clen = code.Length;
for (int i = begin; i < clen; i++) {
int n = "0123456789".IndexOf(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;
if (flg)
lng -= 180;
return new double[] { lat, lng, level, unit };
}
}