-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgeo3x3.smallbasic
75 lines (72 loc) · 1.59 KB
/
geo3x3.smallbasic
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
Sub Geo3x3_encode
If level < 1 Then
res = ""
Goto End_Geo3x3_encode
EndIf
res = "E"
If lng < 0 Then
res = "W"
lng = lng + 180.0
EndIf
lat = lat + 90.0
unit = 180.0
For i = 1 To level - 1
unit = unit / 3.0
x = Math.Floor(lng / unit)
y = Math.Floor(lat / unit)
n = x + y * 3 + 1
res = Text.Append(res, n)
lng = lng - x * unit
lat = lat - y * unit
EndFor
End_Geo3x3_encode:
EndSub
Sub Geo3x3_decode
unit = 180.0
lat = 0.0
lng = 0.0
level = 0
clen = Text.GetLength(code)
If clen = 0 Then
Goto End_Geo3x3_decode
EndIf
begin = 1
flg = 0
c = Text.GetSubText(code, 1, 1)
If c = "-" or c = "W" Then
flg = 1
begin = 1
ElseIf c = "+" or c = "E" Then
begin = 1
EndIf
level = 1
For i = begin To clen - 1
n = Text.GetIndexOf("0123456789", Text.GetSubText(code, i + 1, 1)) - 1
If n < 0 Then
i = clen - 1
Else
unit = unit / 3
lng = lng + Math.Remainder(n, 3) * unit
lat = lat + Math.Floor(n / 3) * unit
level = level + 1
EndIf
EndFor
lat = lat + unit / 2.0
lng = lng + unit / 2.0
lat = lat - 90.0
If flg Then
lng = lng - 180.0
EndIf
End_Geo3x3_decode:
EndSub
lat = 35.65858
lng = 139.745433
level = 14
Geo3x3_encode()
TextWindow.WriteLine(res)
code = "E9139659937288"
Geo3x3_decode()
TextWindow.WriteLine(lat)
TextWindow.WriteLine(lng)
TextWindow.WriteLine(level)
TextWindow.WriteLine(unit)