-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgeo3x3.dncl
75 lines (72 loc) · 1.24 KB
/
geo3x3.dncl
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
73
74
75
function encode(lat, lng, level) {
res = "E"
if lng < 0 {
res = "W"
lng = lng + 180
}
lat = lat + 90 # 180:the North Pole, 0:the South Pole
unit = 180
for i = 1 to level - 1 {
unit = unit / 3
x = lng // unit
y = lat // unit
res = res + (x + y * 3 + 1)
lng = lng - x * unit
lat = lat - y * unit
}
return res
}
function char2n(c) {
if c == "0" {
return 0
} else if c == "1" {
return 1
} else if c == "2" {
return 2
} else if c == "3" {
return 3
} else if c == "4" {
return 4
} else if c == "5" {
return 5
} else if c == "6" {
return 6
} else if c == "7" {
return 7
} else if c == "8" {
return 8
} else if c == "9" {
return 9
}
return -1
}
function decode(code) {
flg = 0
if code[0] == "W" {
flg = 1
}
unit = 180
lat = 0
lng = 0
level = 1
while 1 {
n = char2n(code[level])
if n <= 0 {
break
}
unit = unit / 3
n = n - 1
lng = lng + n % 3 * unit
lat = lat + n // 3 * unit
level = level + 1
}
lat = lat + unit / 2
lng = lng + unit / 2
lat = lat - 90
if flg {
lng = lng - 180
}
return [lat, lng, level, unit]
}
print encode(35.65858, 139.745433, 14)
print decode("E9139659937288")