-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGeoJSON+LineString+DecodePolyline.swift
More file actions
63 lines (52 loc) · 1.39 KB
/
GeoJSON+LineString+DecodePolyline.swift
File metadata and controls
63 lines (52 loc) · 1.39 KB
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
//
// GeoJSON+LineString+DecodePolylineString.swift
//
// Created by Adrian Schoenig on 18/2/17.
//
//
import Foundation
import GeoJSONKit
extension GeoJSON.LineString {
public init(encodedPolyline: String) {
let bytes = encodedPolyline.utf8CString
let length = bytes.count - 1 // ignore 0 at end
var idx = 0
var array: [GeoJSON.Position] = []
var latitude = 0.0
var longitude = 0.0
while idx < length {
var byte = 0
var res = 0
var shift = 0
repeat {
if idx > length {
break
}
byte = Int(bytes[idx]) - 63
idx += 1
res |= (byte & 0x1F) << shift
shift += 5
} while byte >= 0x20
let deltaLat = ((res & 1) != 0 ? ~(res >> 1) : (res >> 1));
latitude += Double(deltaLat)
shift = 0
res = 0
repeat {
if idx > length {
break
}
byte = Int(bytes[idx]) - 0x3F
idx += 1
res |= (byte & 0x1F) << shift
shift += 5
} while byte >= 0x20
let deltaLon = ((res & 1) != 0 ? ~(res >> 1) : (res >> 1));
longitude += Double(deltaLon)
let finalLat = latitude * 1E-5
let finalLon = longitude * 1E-5
let coordinate = GeoJSON.Position(latitude: finalLat, longitude: finalLon)
array.append(coordinate)
}
self.init(positions: array)
}
}