Skip to content

Commit 2f3942c

Browse files
authored
Feature/encodedpolyline (#24)
* Add `GeoJSON.LineString(encodedPolyline: String)` * Update GHA, test newer Swifts
1 parent 99c542f commit 2f3942c

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

.github/workflows/swift.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
macos:
1111
runs-on: macos-latest
1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414
- name: Build
1515
run: swift build
1616
- name: Test
@@ -21,12 +21,12 @@ jobs:
2121

2222
strategy:
2323
matrix:
24-
swift: ["5.9.1", "5.7.3"]
24+
swift: ["6.0", "5.10", "5.9", "5.8"]
2525

2626
container:
2727
image: swift:${{ matrix.swift }}
2828
steps:
29-
- uses: actions/checkout@v3
29+
- uses: actions/checkout@v4
3030
- name: Build
3131
run: swift build
3232
- name: Test
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// GeoJSON+LineString+DecodePolylineString.swift
3+
//
4+
// Created by Adrian Schoenig on 18/2/17.
5+
//
6+
//
7+
import Foundation
8+
9+
import GeoJSONKit
10+
11+
extension GeoJSON.LineString {
12+
public init(encodedPolyline: String) {
13+
let bytes = encodedPolyline.utf8CString
14+
let length = bytes.count - 1 // ignore 0 at end
15+
var idx = 0
16+
17+
var array: [GeoJSON.Position] = []
18+
19+
var latitude = 0.0
20+
var longitude = 0.0
21+
while idx < length {
22+
var byte = 0
23+
var res = 0
24+
var shift = 0
25+
26+
repeat {
27+
if idx > length {
28+
break
29+
}
30+
byte = Int(bytes[idx]) - 63
31+
idx += 1
32+
res |= (byte & 0x1F) << shift
33+
shift += 5
34+
} while byte >= 0x20
35+
36+
let deltaLat = ((res & 1) != 0 ? ~(res >> 1) : (res >> 1));
37+
latitude += Double(deltaLat)
38+
39+
shift = 0
40+
res = 0
41+
42+
repeat {
43+
if idx > length {
44+
break
45+
}
46+
byte = Int(bytes[idx]) - 0x3F
47+
idx += 1
48+
res |= (byte & 0x1F) << shift
49+
shift += 5
50+
} while byte >= 0x20
51+
52+
let deltaLon = ((res & 1) != 0 ? ~(res >> 1) : (res >> 1));
53+
longitude += Double(deltaLon)
54+
55+
let finalLat = latitude * 1E-5
56+
let finalLon = longitude * 1E-5
57+
let coordinate = GeoJSON.Position(latitude: finalLat, longitude: finalLon)
58+
array.append(coordinate)
59+
}
60+
61+
self.init(positions: array)
62+
}
63+
}

0 commit comments

Comments
 (0)