Skip to content

Commit

Permalink
Merge pull request #52 from mapbox/1ec5-geometry-encoding-51
Browse files Browse the repository at this point in the history
Fix crashes and omissions of geometries
  • Loading branch information
1ec5 committed Jun 6, 2016
2 parents b798003 + da6327e commit 39f9e82
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 25 deletions.
8 changes: 8 additions & 0 deletions MapboxDirections.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
DA6C9DA61CAE462800094FBC /* MBDirections.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6254731AE70CB700017857 /* MBDirections.swift */; };
DA6C9DAC1CAEC72800094FBC /* DrivingV5Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA6C9DAB1CAEC72800094FBC /* DrivingV5Tests.swift */; };
DA6C9DB21CAECA0E00094FBC /* Fixture.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA6C9DB11CAECA0E00094FBC /* Fixture.swift */; };
DA737EE41D05F91E005BDA16 /* driving_dc_geojson.json in Resources */ = {isa = PBXBuildFile; fileRef = DA737EE31D05F91E005BDA16 /* driving_dc_geojson.json */; };
DA737EE51D05F91E005BDA16 /* driving_dc_geojson.json in Resources */ = {isa = PBXBuildFile; fileRef = DA737EE31D05F91E005BDA16 /* driving_dc_geojson.json */; };
DA737EE61D05F91E005BDA16 /* driving_dc_geojson.json in Resources */ = {isa = PBXBuildFile; fileRef = DA737EE31D05F91E005BDA16 /* driving_dc_geojson.json */; };
DAC05F161CFBFAC400FA0071 /* MBWaypoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC05F151CFBFAC400FA0071 /* MBWaypoint.swift */; };
DAC05F181CFC075300FA0071 /* MBRoute.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC05F171CFC075300FA0071 /* MBRoute.swift */; };
DAC05F1A1CFC077C00FA0071 /* MBRouteLeg.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC05F191CFC077C00FA0071 /* MBRouteLeg.swift */; };
Expand Down Expand Up @@ -141,6 +144,7 @@
DA6C9D9A1CAE442B00094FBC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
DA6C9DAB1CAEC72800094FBC /* DrivingV5Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DrivingV5Tests.swift; sourceTree = "<group>"; };
DA6C9DB11CAECA0E00094FBC /* Fixture.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Fixture.swift; sourceTree = "<group>"; };
DA737EE31D05F91E005BDA16 /* driving_dc_geojson.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = driving_dc_geojson.json; sourceTree = "<group>"; };
DAC05F151CFBFAC400FA0071 /* MBWaypoint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MBWaypoint.swift; sourceTree = "<group>"; };
DAC05F171CFC075300FA0071 /* MBRoute.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MBRoute.swift; sourceTree = "<group>"; };
DAC05F191CFC077C00FA0071 /* MBRouteLeg.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MBRouteLeg.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -306,6 +310,7 @@
isa = PBXGroup;
children = (
DAC05F1B1CFC1E5300FA0071 /* driving_dc_polyline.json */,
DA737EE31D05F91E005BDA16 /* driving_dc_geojson.json */,
);
path = Fixtures;
sourceTree = "<group>";
Expand Down Expand Up @@ -631,6 +636,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DA737EE51D05F91E005BDA16 /* driving_dc_geojson.json in Resources */,
DA1A10CF1D00F975009F82FA /* driving_dc_polyline.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -646,6 +652,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DA737EE61D05F91E005BDA16 /* driving_dc_geojson.json in Resources */,
DA1A10F31D010251009F82FA /* driving_dc_polyline.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -668,6 +675,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DA737EE41D05F91E005BDA16 /* driving_dc_geojson.json in Resources */,
DAC05F1C1CFC1E5300FA0071 /* driving_dc_polyline.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
3 changes: 2 additions & 1 deletion MapboxDirections/MBDirections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ extension CLLocationCoordinate2D {
}

internal static func coordinates(geoJSON lineString: JSONDictionary) -> [CLLocationCoordinate2D] {
assert(lineString["type"] as? String == "LineString")
let type = lineString["type"] as? String
assert(type == "LineString" || type == "Point")
let coordinates = lineString["coordinates"] as! [[Double]]
return coordinates.map { self.init(geoJSON: $0) }
}
Expand Down
20 changes: 14 additions & 6 deletions MapboxDirections/MBRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ public class Route: NSObject {
}
let distance = json["distance"] as! Double
let expectedTravelTime = json["duration"] as! Double
let coordinates: [CLLocationCoordinate2D]?
if let geometry = json["geometry"] as? String {

var coordinates: [CLLocationCoordinate2D]?
switch json["geometry"] {
case let geometry as JSONDictionary:
coordinates = CLLocationCoordinate2D.coordinates(geoJSON: geometry)
case let geometry as String:
coordinates = decodePolyline(geometry, precision: 1e5)!
} else {
default:
coordinates = nil
}

Expand Down Expand Up @@ -130,10 +134,14 @@ internal class RouteV4: Route {
let leg = RouteLegV4(json: json, source: waypoints.first!, destination: waypoints.last!, profileIdentifier: profileIdentifier)
let distance = json["distance"] as! Double
let expectedTravelTime = json["duration"] as! Double
let coordinates: [CLLocationCoordinate2D]?
if let geometry = json["geometry"] as? String {

var coordinates: [CLLocationCoordinate2D]?
switch json["geometry"] {
case let geometry as JSONDictionary:
coordinates = CLLocationCoordinate2D.coordinates(geoJSON: geometry)
case let geometry as String:
coordinates = decodePolyline(geometry, precision: 1e6)!
} else {
default:
coordinates = nil
}

Expand Down
11 changes: 9 additions & 2 deletions MapboxDirections/MBRouteStep.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Polyline

/**
A `TransportType` specifies the mode of transportation used for part of a route.
*/
Expand Down Expand Up @@ -536,9 +538,14 @@ public class RouteStep: NSObject {

let name = json["name"] as? String

var coordinates: [CLLocationCoordinate2D]? = nil
if let geometry = json["geometry"] as? JSONDictionary {
var coordinates: [CLLocationCoordinate2D]?
switch json["geometry"] {
case let geometry as JSONDictionary:
coordinates = CLLocationCoordinate2D.coordinates(geoJSON: geometry)
case let geometry as String:
coordinates = decodePolyline(geometry, precision: 1e5)!
default:
coordinates = nil
}

self.init(finalHeading: finalHeading, maneuverType: maneuverType, maneuverDirection: maneuverDirection, maneuverLocation: maneuverLocation, name: name, coordinates: coordinates, json: json)
Expand Down
47 changes: 35 additions & 12 deletions MapboxDirectionsTests/DrivingV5Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@ class DrivingV5Tests: XCTestCase {
super.tearDown()
}

func testDirections() {
func testWithFormat(shapeFormat: RouteShapeFormat) {
let expectation = expectationWithDescription("calculating directions should return results")

let queryParams: [String: String?] = [
"alternatives": String(true),
"geometries": "polyline",
"alternatives": "true",
"geometries": String(shapeFormat),
"overview": "full",
"steps": String(true),
"continue_straight": String(true),
"steps": "true",
"continue_straight": "true",
"access_token": BogusToken,
]
stub(isHost("api.mapbox.com")
&& isPath("/directions/v5/mapbox/driving/-122.42,37.78;-77.03,38.91.json")
&& containsQueryParams(queryParams)) { _ in
let path = NSBundle(forClass: self.dynamicType).pathForResource("driving_dc_polyline", ofType: "json")
return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: ["Content-Type": "application/json"])
let path = NSBundle(forClass: self.dynamicType).pathForResource("driving_dc_\(shapeFormat)", ofType: "json")
return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: ["Content-Type": "application/json"])
}

let options = RouteOptions(coordinates: [
CLLocationCoordinate2D(latitude: 37.78, longitude: -122.42),
CLLocationCoordinate2D(latitude: 38.91, longitude: -77.03),
])
XCTAssertEqual(options.shapeFormat, RouteShapeFormat.Polyline, "Route shape format should be Polyline by default.")
options.shapeFormat = shapeFormat
options.includesSteps = true
options.includesAlternativeRoutes = true
options.routeShapeResolution = .Full
Expand All @@ -52,16 +54,37 @@ class DrivingV5Tests: XCTestCase {

XCTAssertNotNil(route)
XCTAssertNotNil(route!.coordinates)
XCTAssertEqual(route!.coordinates!.count, 28372)
XCTAssertEqual(route!.coordinates!.count, 28375)

// confirming actual decoded values is important because the Directions API
// uses an atypical precision level for polyline encoding
XCTAssertEqual(round(route!.coordinates!.first!.latitude), 38)
XCTAssertEqual(round(route!.coordinates!.first!.longitude), -122)
XCTAssertEqual(route!.legs.count, 1)
XCTAssertEqual(route!.legs.first!.steps.count, 81)
XCTAssertEqual(route!.legs.first!.steps[24].distance, 12_623.1)
XCTAssertEqual(route!.legs.first!.steps[24].expectedTravelTime, 422.6)
XCTAssertEqual(route!.legs.first!.steps[24].name, "I 80;US 93 ALT")

let leg = route!.legs.first!
XCTAssertEqual(leg.steps.count, 80)

let step = leg.steps[24]
XCTAssertEqual(step.distance, 223581.6)
XCTAssertEqual(step.expectedTravelTime, 7219.2)
XCTAssertEqual(step.name, "I 80;US 93 Alternate")

XCTAssertNotNil(step.coordinates)
XCTAssertEqual(step.coordinates!.count, 699)
XCTAssertEqual(step.coordinates!.count, Int(step.coordinateCount))
let coordinate = step.coordinates!.first!
XCTAssertEqual(round(coordinate.latitude), 41)
XCTAssertEqual(round(coordinate.longitude), -115)
}

func testGeoJSON() {
XCTAssertEqual(String(RouteShapeFormat.GeoJSON), "geojson")
testWithFormat(.GeoJSON)
}

func testPolyline() {
XCTAssertEqual(String(RouteShapeFormat.Polyline), "polyline")
testWithFormat(.Polyline)
}
}
1 change: 1 addition & 0 deletions MapboxDirectionsTests/Fixtures/driving_dc_geojson.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion MapboxDirectionsTests/Fixtures/driving_dc_polyline.json

Large diffs are not rendered by default.

0 comments on commit 39f9e82

Please sign in to comment.