-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgeo3x3.awk
executable file
·51 lines (51 loc) · 932 Bytes
/
geo3x3.awk
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
function geo3x3_encode(lat, lng, level, unit,
res, i, x, y) {
if (level < 1) {
return ""
}
res = "E"
if (lng < 0) {
res = "W"
lng += 180
}
lat += 90 # 180:the North Pole, 0:the South Pole
unit = 180
for (i = 1; i < level; i++) {
unit /= 3
x = int(lng / unit)
y = int(lat / unit)
res = res "" (x + y * 3 + 1)
lng -= x * unit
lat -= y * unit
}
return res
}
function geo3x3_decode(code,
clen, flg, unit, lat, lng, level, i, res) {
clen = length(code)
if (clen == 0) {
return null
}
flg = substr(code, 1, 1) == "W"
unit = 180
lat = 0
lng = 0
level = 1
for (i = 1; i < clen; i++) {
n = substr(code, i + 1, 1) - 1
if (n < 0 || n > 9) {
break
}
unit /= 3
lng += (n % 3) * unit
lat += int(n / 3) * unit
level++
}
lat += unit / 2
lng += unit / 2
lat -= 90
if (flg) {
lng -= 180
}
return lat " " lng " " level " " unit
}