Skip to content

Commit

Permalink
Migrated to Turf shims for Core Location types
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Mar 3, 2021
1 parent 612e4e9 commit 714c524
Show file tree
Hide file tree
Showing 32 changed files with 174 additions and 292 deletions.
9 changes: 3 additions & 6 deletions Sources/MapboxDirections/DirectionsResult.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import Foundation
import Polyline
#if canImport(CoreLocation)
import CoreLocation
#endif
import Turf

/**
Expand All @@ -24,7 +21,7 @@ open class DirectionsResult: Codable {

// MARK: Creating a Directions Result

init(legs: [RouteLeg], shape: LineString?, distance: CLLocationDistance, expectedTravelTime: TimeInterval, typicalTravelTime: TimeInterval? = nil) {
init(legs: [RouteLeg], shape: LineString?, distance: Turf.LocationDistance, expectedTravelTime: TimeInterval, typicalTravelTime: TimeInterval? = nil) {
self.legs = legs
self.shape = shape
self.distance = distance
Expand All @@ -50,7 +47,7 @@ open class DirectionsResult: Codable {
throw DirectionsCodingError.missingOptions
}

distance = try container.decode(CLLocationDistance.self, forKey: .distance)
distance = try container.decode(Turf.LocationDistance.self, forKey: .distance)
expectedTravelTime = try container.decode(TimeInterval.self, forKey: .expectedTravelTime)
typicalTravelTime = try container.decodeIfPresent(TimeInterval.self, forKey: .typicalTravelTime)

Expand Down Expand Up @@ -134,7 +131,7 @@ open class DirectionsResult: Codable {
The value of this property accounts for the distance that the user must travel to traverse the path of the route. It is the sum of the `distance` properties of the route’s legs, not the sum of the direct distances between the route’s waypoints. You should not assume that the user would travel along this distance at a fixed speed.
*/
public let distance: CLLocationDistance
public let distance: Turf.LocationDistance

/**
The route’s expected travel time, measured in seconds.
Expand Down
40 changes: 18 additions & 22 deletions Sources/MapboxDirections/Extensions/Codable.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import Foundation
import Polyline
import Turf
import func Polyline.encodeCoordinates
#if canImport(CoreLocation)
import CoreLocation
import typealias Polyline.LocationCoordinate2D
#else
import struct Polyline.LocationCoordinate2D
#endif
import Turf

extension LineString {
/**
Returns a string representation of the line string in [Polyline Algorithm Format](https://developers.google.com/maps/documentation/utilities/polylinealgorithm).
*/
func polylineEncodedString(precision: Double = 1e5) -> String {
#if canImport(CoreLocation)
return encodeCoordinates(coordinates, precision: precision)
let coordinates = self.coordinates
#else
return encodeCoordinates(coordinates.map { LocationCoordinate2D($0) }, precision: precision)
let coordinates = self.coordinates.map { Polyline.LocationCoordinate2D(latitude: $0.latitude, longitude: $0.longitude) }
#endif
return encodeCoordinates(coordinates, precision: precision)
}
}

Expand Down Expand Up @@ -45,7 +48,7 @@ extension PolyLineString: Codable {
switch options?.shapeFormat ?? .default {
case .geoJSON:
let lineStringContainer = try decoder.container(keyedBy: LineStringCodingKeys.self)
let coordinates = try lineStringContainer.decode([CLLocationCoordinate2DCodable].self, forKey: .coordinates).map { $0.decodedCoordinates }
let coordinates = try lineStringContainer.decode([LocationCoordinate2DCodable].self, forKey: .coordinates).map { $0.decodedCoordinates }
self = .lineString(LineString(coordinates))
case .polyline, .polyline6:
let precision = options?.shapeFormat == .polyline6 ? 1e6 : 1e5
Expand All @@ -59,19 +62,18 @@ extension PolyLineString: Codable {
switch self {
case let .lineString(lineString):
var lineStringContainer = encoder.container(keyedBy: LineStringCodingKeys.self)
try lineStringContainer.encode(lineString.coordinates.map { CLLocationCoordinate2DCodable($0) }, forKey: .coordinates)
try lineStringContainer.encode(lineString.coordinates.map { LocationCoordinate2DCodable($0) }, forKey: .coordinates)
case let .polyline(encodedPolyline, precision: _):
try container.encode(encodedPolyline)
}
}
}

struct CLLocationCoordinate2DCodable: Codable {
var latitude: CLLocationDegrees
var longitude: CLLocationDegrees
var decodedCoordinates: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: latitude,
longitude: longitude)
struct LocationCoordinate2DCodable: Codable {
var latitude: Turf.LocationDegrees
var longitude: Turf.LocationDegrees
var decodedCoordinates: Turf.LocationCoordinate2D {
return Turf.LocationCoordinate2D(latitude: latitude, longitude: longitude)
}

func encode(to encoder: Encoder) throws {
Expand All @@ -82,18 +84,12 @@ struct CLLocationCoordinate2DCodable: Codable {

init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
longitude = try container.decode(CLLocationDegrees.self)
latitude = try container.decode(CLLocationDegrees.self)
longitude = try container.decode(Turf.LocationDegrees.self)
latitude = try container.decode(Turf.LocationDegrees.self)
}

init(_ coordinate: CLLocationCoordinate2D) {
init(_ coordinate: Turf.LocationCoordinate2D) {
latitude = coordinate.latitude
longitude = coordinate.longitude
}
}

extension CLLocationCoordinate2D {
var codableCoordinates: CLLocationCoordinate2DCodable {
return CLLocationCoordinate2DCodable(self)
}
}
31 changes: 3 additions & 28 deletions Sources/MapboxDirections/Extensions/CoreLocation.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Foundation
#if canImport(CoreLocation)
import CoreLocation
#else
import Turf
import Polyline
#endif
import Turf


#if canImport(CoreLocation)
/**
Expand All @@ -30,34 +29,10 @@ public typealias LocationSpeed = Double
The accuracy of a geographical coordinate.
*/
public typealias LocationAccuracy = Double

extension CLLocationCoordinate2D {
init(_ locationCoordinate2D: LocationCoordinate2D) {
self.init(latitude: locationCoordinate2D.latitude, longitude: locationCoordinate2D.longitude)
}
}

extension LocationCoordinate2D {
init(_ clLocationCoordinate2D: CLLocationCoordinate2D) {
self.init(latitude: clLocationCoordinate2D.latitude, longitude: clLocationCoordinate2D.longitude)
}
}
#endif

extension CLLocationCoordinate2D {
extension LocationCoordinate2D {
internal var requestDescription: String {
return "\(longitude.rounded(to: 1e6)),\(latitude.rounded(to: 1e6))"
}
}

#if canImport(CoreLocation)
extension CLLocation {
/**
Initializes a CLLocation object with the given coordinate pair.
*/
internal convenience init(coordinate: CLLocationCoordinate2D) {
self.init(latitude: coordinate.latitude, longitude: coordinate.longitude)
}
}
#endif

10 changes: 6 additions & 4 deletions Sources/MapboxDirections/Extensions/GeoJSON.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Foundation
import func Polyline.decodePolyline
#if canImport(CoreLocation)
import CoreLocation
import typealias Polyline.LocationCoordinate2D
#else
import struct Polyline.LocationCoordinate2D
#endif
import Polyline
import Turf

extension BoundingBox: CustomStringConvertible {
Expand All @@ -22,7 +24,7 @@ extension LineString {
}

init(encodedPolyline: String, precision: Double) throws {
guard var coordinates = decodePolyline(encodedPolyline, precision: precision) as [LocationCoordinate2D]? else {
guard var coordinates = decodePolyline(encodedPolyline, precision: precision) as [Polyline.LocationCoordinate2D]? else {
throw GeometryError.cannotDecodePolyline(precision: precision)
}
// If the polyline has zero length with both endpoints at the same coordinate, Polyline drops one of the coordinates.
Expand All @@ -34,7 +36,7 @@ extension LineString {
#if canImport(CoreLocation)
self.init(coordinates)
#else
self.init(coordinates.map { CLLocationCoordinate2D($0) })
self.init(coordinates.map { Turf.LocationCoordinate2D(latitude: $0.latitude, longitude: $0.longitude) })
#endif
}
}
Expand Down
20 changes: 8 additions & 12 deletions Sources/MapboxDirections/Intersection.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import Foundation
#if canImport(CoreLocation)
import CoreLocation
#else
import Turf
#endif

/**
A single cross street along a step.
*/
public struct Intersection {
// MARK: Creating an Intersection

public init(location: CLLocationCoordinate2D,
headings: [CLLocationDirection],
public init(location: LocationCoordinate2D,
headings: [LocationDirection],
approachIndex: Int,
outletIndex: Int,
outletIndexes: IndexSet,
Expand Down Expand Up @@ -46,18 +42,18 @@ public struct Intersection {
/**
The geographic coordinates at the center of the intersection.
*/
public let location: CLLocationCoordinate2D
public let location: LocationCoordinate2D

// MARK: Getting the Roads that Meet at the Intersection

/**
An array of `CLLocationDirection`s indicating the absolute headings of the roads that meet at the intersection.
An array of `LocationDirection`s indicating the absolute headings of the roads that meet at the intersection.
A road is represented in this array by a heading indicating the direction from which the road meets the intersection. To get the direction of travel when leaving the intersection along the road, rotate the heading 180 degrees.
A single road that passes through this intersection is represented by two items in this array: one for the segment that enters the intersection and one for the segment that exits it.
*/
public let headings: [CLLocationDirection]
public let headings: [LocationDirection]

/**
The indices of the items in the `headings` array that correspond to the roads that may be used to leave the intersection.
Expand Down Expand Up @@ -232,7 +228,7 @@ extension Intersection: Codable {

func encode(to encoder: Encoder, administrativeRegionIndex: Int?, geometryIndex: Int?) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(CLLocationCoordinate2DCodable(location), forKey: .location)
try container.encode(LocationCoordinate2DCodable(location), forKey: .location)
try container.encode(headings, forKey: .headings)

try container.encodeIfPresent(approachIndex, forKey: .approachIndex)
Expand Down Expand Up @@ -290,8 +286,8 @@ extension Intersection: Codable {

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
location = try container.decode(CLLocationCoordinate2DCodable.self, forKey: .location).decodedCoordinates
headings = try container.decode([CLLocationDirection].self, forKey: .headings)
location = try container.decode(LocationCoordinate2DCodable.self, forKey: .location).decodedCoordinates
headings = try container.decode([LocationDirection].self, forKey: .headings)

if let lanes = try container.decodeIfPresent([Lane].self, forKey: .lanes) {
approachLanes = lanes.map { $0.indications }
Expand Down
6 changes: 1 addition & 5 deletions Sources/MapboxDirections/MapMatching/Match.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import Foundation
#if canImport(CoreLocation)
import CoreLocation
#endif
import Polyline
import Turf

/**
Expand Down Expand Up @@ -65,7 +61,7 @@ open class Match: DirectionsResult {
- parameter confidence: A number between 0 and 1 that indicates the Map Matching API’s confidence that the match is accurate. A higher confidence means the match is more likely to be accurate.
- parameter weight: A `Weight` enum, which represents the weight given to a specific `Match`.
*/
public init(legs: [RouteLeg], shape: LineString?, distance: CLLocationDistance, expectedTravelTime: TimeInterval, confidence: Float, weight: Weight) {
public init(legs: [RouteLeg], shape: LineString?, distance: LocationDistance, expectedTravelTime: TimeInterval, confidence: Float, weight: Weight) {
self.confidence = confidence
self.weight = weight
super.init(legs: legs, shape: shape, distance: distance, expectedTravelTime: expectedTravelTime)
Expand Down
5 changes: 2 additions & 3 deletions Sources/MapboxDirections/MapMatching/MatchOptions.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Foundation
#if canImport(CoreLocation)
import CoreLocation
#else
import Turf
#endif
import Turf

/**
A `MatchOptions` object is a structure that specifies the criteria for results returned by the Mapbox Map Matching API.
Expand Down Expand Up @@ -34,7 +33,7 @@ open class MatchOptions: DirectionsOptions {
- parameter coordinates: An array of geographic coordinates representing locations to attempt to match against the road network. The array should contain at least two locations (the source and destination) and at most 100 locations. (Some profiles, such as `DirectionsProfileIdentifier.automobileAvoidingTraffic`, [may have lower limits](https://docs.mapbox.com/api/navigation/#directions).) Each coordinate is converted into a `Waypoint` object.
- parameter profileIdentifier: A string specifying the primary mode of transportation for the routes. `DirectionsProfileIdentifier.automobile` is used by default.
*/
public convenience init(coordinates: [CLLocationCoordinate2D], profileIdentifier: DirectionsProfileIdentifier? = nil) {
public convenience init(coordinates: [LocationCoordinate2D], profileIdentifier: DirectionsProfileIdentifier? = nil) {
let waypoints = coordinates.map {
Waypoint(coordinate: $0)
}
Expand Down
6 changes: 1 addition & 5 deletions Sources/MapboxDirections/MapMatching/Tracepoint.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import Foundation
#if canImport(CoreLocation)
import CoreLocation
#else
import Turf
#endif

/**
A `Tracepoint` represents a location matched to the road network.
Expand All @@ -18,7 +14,7 @@ public class Tracepoint: Waypoint {
case countOfAlternatives = "alternatives_count"
}

init(coordinate: CLLocationCoordinate2D, countOfAlternatives: Int, name: String?) {
init(coordinate: LocationCoordinate2D, countOfAlternatives: Int, name: String?) {
self.countOfAlternatives = countOfAlternatives
super.init(coordinate: coordinate, name: name)
}
Expand Down
5 changes: 1 addition & 4 deletions Sources/MapboxDirections/Route.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import Foundation
#if canImport(CoreLocation)
import CoreLocation
#endif
import Turf

/**
Expand All @@ -20,7 +17,7 @@ open class Route: DirectionsResult {
- parameter expectedTravelTime: The route’s expected travel time, measured in seconds.
- parameter typicalTravelTime: The route’s typical travel time, measured in seconds.
*/
public override init(legs: [RouteLeg], shape: LineString?, distance: CLLocationDistance, expectedTravelTime: TimeInterval, typicalTravelTime: TimeInterval? = nil) {
public override init(legs: [RouteLeg], shape: LineString?, distance: LocationDistance, expectedTravelTime: TimeInterval, typicalTravelTime: TimeInterval? = nil) {
super.init(legs: legs, shape: shape, distance: distance, expectedTravelTime: expectedTravelTime, typicalTravelTime: typicalTravelTime)
}

Expand Down
11 changes: 4 additions & 7 deletions Sources/MapboxDirections/RouteLeg.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import Foundation
#if canImport(CoreLocation)
import CoreLocation
#endif
import Polyline
import Turf

Expand Down Expand Up @@ -36,7 +33,7 @@ open class RouteLeg: Codable {
- parameter typicalTravelTime: The route leg’s typical travel time, measured in seconds.
- parameter profileIdentifier: The primary mode of transportation for the route leg.
*/
public init(steps: [RouteStep], name: String, distance: CLLocationDistance, expectedTravelTime: TimeInterval, typicalTravelTime: TimeInterval? = nil, profileIdentifier: DirectionsProfileIdentifier) {
public init(steps: [RouteStep], name: String, distance: Turf.LocationDistance, expectedTravelTime: TimeInterval, typicalTravelTime: TimeInterval? = nil, profileIdentifier: DirectionsProfileIdentifier) {
self.steps = steps
self.name = name
self.distance = distance
Expand All @@ -61,7 +58,7 @@ open class RouteLeg: Codable {
source = try container.decodeIfPresent(Waypoint.self, forKey: .source)
destination = try container.decodeIfPresent(Waypoint.self, forKey: .destination)
name = try container.decode(String.self, forKey: .name)
distance = try container.decode(CLLocationDistance.self, forKey: .distance)
distance = try container.decode(Turf.LocationDistance.self, forKey: .distance)
expectedTravelTime = try container.decode(TimeInterval.self, forKey: .expectedTravelTime)
typicalTravelTime = try container.decodeIfPresent(TimeInterval.self, forKey: .typicalTravelTime)

Expand Down Expand Up @@ -175,7 +172,7 @@ open class RouteLeg: Codable {
This property is set if the `RouteOptions.attributeOptions` property contains `AttributeOptions.distance`.
*/
open var segmentDistances: [CLLocationDistance]?
open var segmentDistances: [Turf.LocationDistance]?

/**
An array containing the expected travel time (measured in seconds) between each coordinate in the route leg geometry.
Expand Down Expand Up @@ -269,7 +266,7 @@ open class RouteLeg: Codable {
The value of this property accounts for the distance that the user must travel to arrive at the destination from the source. It is not the direct distance between the source and destination, nor should not assume that the user would travel along this distance at a fixed speed.
*/
public let distance: CLLocationDistance
public let distance: Turf.LocationDistance

/**
The route leg’s expected travel time, measured in seconds.
Expand Down
Loading

0 comments on commit 714c524

Please sign in to comment.