-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGeo3x3.nml
73 lines (65 loc) · 2.04 KB
/
Geo3x3.nml
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
function encode (lat:float, lng:float, level:int) {
if level < 1 then ""
else {
var ic, ilng = if lng >= 0.0 then ('E',lng)
else ('W',lng + 180.0);
var ilat = lat + 90.0;
var iunit = 180.0;
var res = Buffer.create();
Buffer.add_char res ic;
var rlng = &ilng;
var rlat = &ilat;
var runit = &iunit;
function rec loop (i) {
if i < level then {
runit := *runit / 3.0;
var x = Math.floor (*rlng / *runit);
var y = Math.floor (*rlat / *runit);
var c = chr ((ord '0') + x + y * 3 + 1);
Buffer.add_char res c;
rlng := *rlng - (x * (*runit));
rlat := *rlat - (y * (*runit));
loop (i+1);
}
}
loop 1;
Buffer.string res;
}
}
function decode (code:string) {
if String.length code == 0 then (0.0, 0.0, 0, 0.0)
else {
var begin, isWest =
match String.get code 0 {
| c when c == '-' || c == 'W' -> (1,true)
| c when c == '+' || c == 'E' -> (1,false)
| c -> (0,false)
}
var unit = &180.0;
var lat = &0.0;
var lng = &0.0;
var level = &1;
var clen = String.length code;
function rec loop (i) {
if i < clen then {
var n = (ord (String.get code i)) - (ord '0');
if 1 <= n && n <= 9 then {
var n = n -1;
unit := *unit / 3.0;
lng := *lng + n % 3 * (*unit);
lat := *lat + n / 3 * (*unit);
level := *level +1;
loop (i+1);
}
}
}
loop begin;
lat := *lat + *unit / 2.0;
lng := *lng + *unit / 2.0;
lat := *lat - 90.0;
if isWest then {
lng := *lng - 180.0;
}
(*lat, *lng, *level, *unit);
}
}