-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgeo3x3.pkl
47 lines (43 loc) · 1022 Bytes
/
geo3x3.pkl
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
module geo3x3
function encode_fn(code, level, i, lat, lng, unit) =
if (i >= level)
code
else
encode_fn(
code + "\(lng ~/ unit + lat ~/ unit * 3 + 1)",
level,
i + 1,
lat - unit * (lat ~/ unit),
lng - unit * (lng ~/ unit),
unit / 3.0
)
function encode(lat, lng, level) =
encode_fn(
if (lng < 0.0) "W" else "E",
level,
1,
lat + 90.0,
if (lng < 0.0) lng + 180.0 else lng,
180.0 / 3.0
)
function decode_fn(code, _lat, _lng, _level, _unit, wflg) =
if (code.length == _level)
let (_lng2 = _lng + _unit * 3.0 / 2.0)
new {
lat = -90.0 + (_lat + _unit * 3.0 / 2.0)
lng = if (wflg) _lng2 - 180.0 else _lng2
level = _level
unit = _unit * 3.0
}
else
let (n = code[_level].toInt() - 1)
decode_fn(
code,
_lat + n ~/ 3 * _unit,
_lng + n % 3 * _unit,
_level + 1,
_unit / 3.0,
wflg
)
function decode(code) =
decode_fn(code, 0.0, 0.0, 1, 180.0 / 3.0, code[0] == "W")