-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGeo3x3.swift
64 lines (64 loc) · 1.36 KB
/
Geo3x3.swift
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
public struct Geo3x3 {
public static func encode(lat: Double, lng: Double, level: Int) -> String {
if level < 1 {
return ""
}
var lng2 = lng
var res = ""
if lng >= 0 {
res += "E"
} else {
res += "W"
lng2 += 180
}
var lat2 = lat + 90 // 180:the North Pole, 0:the South Pole
var unit = 180.0
for _ in 1 ..< level {
unit /= 3
let x = (Int)(lng2 / unit)
let y = (Int)(lat2 / unit)
res += String(x + y * 3 + 1)
lng2 -= Double(x) * unit
lat2 -= Double(y) * unit
}
return res
}
public static func decode(code: String) -> Array<Double> {
var begin = 0
var flg = false
let c = code.first // index(code.startIndex, offsetBy: 1)
if c == "-" || c == "W" {
flg = true
begin = 1
} else if c == "+" || c == "E" {
begin = 1
}
var unit = 180.0
var lat = 0.0
var lng = 0.0
var level = 1
let clen = code.count
let num = "0123456789"
for i in begin ..< clen {
let c = code[code.index(code.startIndex, offsetBy: i)]
if let idx = num.firstIndex(of : c) {
let n = num.distance(from: num.startIndex, to: idx) - 1
if n >= 0 {
unit /= 3
lng += Double(n % 3) * unit
lat += Double(n / 3) * unit
level += 1
}
} else {
break
}
}
lat += unit / 2
lng += unit / 2
lat -= 90
if flg {
lng -= 180.0
}
return [ lat, lng, Double(level), unit ];
}
}