diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index c17258c..cc16b7e 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -10,7 +10,7 @@ jobs: macos: runs-on: macos-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Build run: swift build - name: Test @@ -21,12 +21,12 @@ jobs: strategy: matrix: - swift: ["5.9.1", "5.7.3"] + swift: ["6.0", "5.10", "5.9", "5.8"] container: image: swift:${{ matrix.swift }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Build run: swift build - name: Test diff --git a/Sources/GeoJSONKitTurf/GeoJSON+LineString+DecodePolyline.swift b/Sources/GeoJSONKitTurf/GeoJSON+LineString+DecodePolyline.swift new file mode 100644 index 0000000..a48cd98 --- /dev/null +++ b/Sources/GeoJSONKitTurf/GeoJSON+LineString+DecodePolyline.swift @@ -0,0 +1,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) + } +}