|
| 1 | +// |
| 2 | +// GeoJSON+LineString+EncodedPolyline.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 | + |
| 13 | + // MARK: - Decode |
| 14 | + |
| 15 | + public init(encodedPolyline: String) { |
| 16 | + let bytes = encodedPolyline.utf8CString |
| 17 | + let length = bytes.count - 1 // ignore 0 at end |
| 18 | + var idx = 0 |
| 19 | + |
| 20 | + var array: [GeoJSON.Position] = [] |
| 21 | + |
| 22 | + var latitude = 0.0 |
| 23 | + var longitude = 0.0 |
| 24 | + while idx < length { |
| 25 | + var byte = 0 |
| 26 | + var res = 0 |
| 27 | + var shift = 0 |
| 28 | + |
| 29 | + repeat { |
| 30 | + if idx > length { |
| 31 | + break |
| 32 | + } |
| 33 | + byte = Int(bytes[idx]) - 63 |
| 34 | + idx += 1 |
| 35 | + res |= (byte & 0x1F) << shift |
| 36 | + shift += 5 |
| 37 | + } while byte >= 0x20 |
| 38 | + |
| 39 | + let deltaLat = ((res & 1) != 0 ? ~(res >> 1) : (res >> 1)); |
| 40 | + latitude += Double(deltaLat) |
| 41 | + |
| 42 | + shift = 0 |
| 43 | + res = 0 |
| 44 | + |
| 45 | + repeat { |
| 46 | + if idx > length { |
| 47 | + break |
| 48 | + } |
| 49 | + byte = Int(bytes[idx]) - 0x3F |
| 50 | + idx += 1 |
| 51 | + res |= (byte & 0x1F) << shift |
| 52 | + shift += 5 |
| 53 | + } while byte >= 0x20 |
| 54 | + |
| 55 | + let deltaLon = ((res & 1) != 0 ? ~(res >> 1) : (res >> 1)); |
| 56 | + longitude += Double(deltaLon) |
| 57 | + |
| 58 | + let finalLat = latitude * 1E-5 |
| 59 | + let finalLon = longitude * 1E-5 |
| 60 | + let coordinate = GeoJSON.Position(latitude: finalLat, longitude: finalLon) |
| 61 | + array.append(coordinate) |
| 62 | + } |
| 63 | + |
| 64 | + self.init(positions: array) |
| 65 | + } |
| 66 | + |
| 67 | + // MARK: - Encode |
| 68 | + |
| 69 | + /// Encodes this `GeoJSON.LineString` to a `String` |
| 70 | + /// |
| 71 | + /// Adopted from https://github.com/raphaelmor/Polyline/blob/master/Sources/Polyline/Polyline.swift |
| 72 | + /// |
| 73 | + /// - parameter precision: The precision used to encode coordinates (default: `1e5`) |
| 74 | + /// - returns: A `String` representing the encoded polyline |
| 75 | + public func encodedPolyline(precision: Double = 1e5) -> String { |
| 76 | + var previousCoordinate = IntegerCoordinates(0, 0) |
| 77 | + var encodedPolyline = "" |
| 78 | + |
| 79 | + for position in positions { |
| 80 | + let intLatitude = Int(round(position.latitude * precision)) |
| 81 | + let intLongitude = Int(round(position.longitude * precision)) |
| 82 | + |
| 83 | + let coordinatesDifference = (intLatitude - previousCoordinate.latitude, intLongitude - previousCoordinate.longitude) |
| 84 | + encodedPolyline += Self.encodeCoordinate(coordinatesDifference) |
| 85 | + |
| 86 | + previousCoordinate = (intLatitude, intLongitude) |
| 87 | + } |
| 88 | + |
| 89 | + return encodedPolyline |
| 90 | + } |
| 91 | + |
| 92 | + private typealias IntegerCoordinates = (latitude: Int, longitude: Int) |
| 93 | + |
| 94 | + private static func encodeCoordinate(_ coordinate: IntegerCoordinates) -> String { |
| 95 | + let latitudeString = encodeSingleComponent(coordinate.latitude) |
| 96 | + let longitudeString = encodeSingleComponent(coordinate.longitude) |
| 97 | + return latitudeString + longitudeString |
| 98 | + } |
| 99 | + |
| 100 | + private static func encodeSingleComponent(_ value: Int) -> String { |
| 101 | + var intValue = value |
| 102 | + if intValue < 0 { |
| 103 | + intValue = intValue << 1 |
| 104 | + intValue = ~intValue |
| 105 | + } else { |
| 106 | + intValue = intValue << 1 |
| 107 | + } |
| 108 | + return encodeFiveBitComponents(intValue) |
| 109 | + } |
| 110 | + |
| 111 | + private static func encodeLevel(_ level: UInt32) -> String { |
| 112 | + return encodeFiveBitComponents(Int(level)) |
| 113 | + } |
| 114 | + |
| 115 | + private static func encodeFiveBitComponents(_ value: Int) -> String { |
| 116 | + var remainingComponents = value |
| 117 | + |
| 118 | + var fiveBitComponent = 0 |
| 119 | + var returnString = String() |
| 120 | + |
| 121 | + repeat { |
| 122 | + fiveBitComponent = remainingComponents & 0x1F |
| 123 | + |
| 124 | + if remainingComponents >= 0x20 { |
| 125 | + fiveBitComponent |= 0x20 |
| 126 | + } |
| 127 | + |
| 128 | + fiveBitComponent += 63 |
| 129 | + |
| 130 | + let char = UnicodeScalar(fiveBitComponent)! |
| 131 | + returnString.append(String(char)) |
| 132 | + remainingComponents = remainingComponents >> 5 |
| 133 | + } while (remainingComponents != 0) |
| 134 | + |
| 135 | + return returnString |
| 136 | + } |
| 137 | +} |
0 commit comments