Skip to content

Commit

Permalink
Merge pull request #222 from mapbox/jerrad/osrm-name-nil
Browse files Browse the repository at this point in the history
Mapbox Directions API OSRM Name Fix
  • Loading branch information
JThramer committed Dec 8, 2017
2 parents 1f9ba26 + 778e7c4 commit cd72999
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 6 deletions.
2 changes: 1 addition & 1 deletion MapboxDirections.swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Pod::Spec.new do |s|

# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

s.source_files = "MapboxDirections"
s.source_files = ["MapboxDirections", "MapboxDirections/*/*"]

# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

Expand Down
42 changes: 42 additions & 0 deletions MapboxDirections.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
358D48871E2EAB4500F32A65 /* MBRouteOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 358D48851E2EAB4500F32A65 /* MBRouteOptions.m */; };
358D48881E2EAB4500F32A65 /* MBRouteOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 358D48851E2EAB4500F32A65 /* MBRouteOptions.m */; };
358D48891E2EAB4500F32A65 /* MBRouteOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 358D48851E2EAB4500F32A65 /* MBRouteOptions.m */; };
8D381B611FD9F5B1008D5A58 /* noDestinationName.json in Resources */ = {isa = PBXBuildFile; fileRef = 8D381B601FD9F5B1008D5A58 /* noDestinationName.json */; };
8D381B631FDB01D1008D5A58 /* apiDestinationName.json in Resources */ = {isa = PBXBuildFile; fileRef = 8D381B621FDB01D1008D5A58 /* apiDestinationName.json */; };
8D381B641FDB0898008D5A58 /* noDestinationName.json in Resources */ = {isa = PBXBuildFile; fileRef = 8D381B601FD9F5B1008D5A58 /* noDestinationName.json */; };
8D381B651FDB0899008D5A58 /* noDestinationName.json in Resources */ = {isa = PBXBuildFile; fileRef = 8D381B601FD9F5B1008D5A58 /* noDestinationName.json */; };
8D381B661FDB089E008D5A58 /* apiDestinationName.json in Resources */ = {isa = PBXBuildFile; fileRef = 8D381B621FDB01D1008D5A58 /* apiDestinationName.json */; };
8D381B671FDB089F008D5A58 /* apiDestinationName.json in Resources */ = {isa = PBXBuildFile; fileRef = 8D381B621FDB01D1008D5A58 /* apiDestinationName.json */; };
8D381B6A1FDB101F008D5A58 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D381B691FDB101F008D5A58 /* String.swift */; };
8D381B6B1FDB3D8A008D5A58 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D381B691FDB101F008D5A58 /* String.swift */; };
8D381B6C1FDB3D8B008D5A58 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D381B691FDB101F008D5A58 /* String.swift */; };
8D381B6D1FDB3D8B008D5A58 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D381B691FDB101F008D5A58 /* String.swift */; };
C51538CC1E807FF00093FF3E /* MBAttribute.swift in Sources */ = {isa = PBXBuildFile; fileRef = C51538CB1E807FF00093FF3E /* MBAttribute.swift */; };
C5247D711E818A24004B6154 /* AnnotationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5247D701E818A24004B6154 /* AnnotationTests.swift */; };
C52552B91FA15D5900B1545C /* MBVisualInstruction.swift in Sources */ = {isa = PBXBuildFile; fileRef = C52552B81FA15D5900B1545C /* MBVisualInstruction.swift */; };
Expand Down Expand Up @@ -192,6 +202,9 @@
/* Begin PBXFileReference section */
358D48801E2EAB2500F32A65 /* MBRouteOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MBRouteOptions.h; sourceTree = "<group>"; };
358D48851E2EAB4500F32A65 /* MBRouteOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MBRouteOptions.m; sourceTree = "<group>"; };
8D381B601FD9F5B1008D5A58 /* noDestinationName.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = noDestinationName.json; sourceTree = "<group>"; };
8D381B621FDB01D1008D5A58 /* apiDestinationName.json */ = {isa = PBXFileReference; explicitFileType = text.json; path = apiDestinationName.json; sourceTree = "<group>"; };
8D381B691FDB101F008D5A58 /* String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = "<group>"; };
C51538CB1E807FF00093FF3E /* MBAttribute.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MBAttribute.swift; sourceTree = "<group>"; };
C5247D701E818A24004B6154 /* AnnotationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnnotationTests.swift; sourceTree = "<group>"; };
C52552B81FA15D5900B1545C /* MBVisualInstruction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MBVisualInstruction.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -347,6 +360,23 @@
name = "iOS Frameworks";
sourceTree = "<group>";
};
8D381B5F1FD9F592008D5A58 /* Responses */ = {
isa = PBXGroup;
children = (
8D381B601FD9F5B1008D5A58 /* noDestinationName.json */,
8D381B621FDB01D1008D5A58 /* apiDestinationName.json */,
);
path = Responses;
sourceTree = "<group>";
};
8D381B681FDB1009008D5A58 /* Extensions */ = {
isa = PBXGroup;
children = (
8D381B691FDB101F008D5A58 /* String.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
C5247D721E818A8D004B6154 /* annotation */ = {
isa = PBXGroup;
children = (
Expand All @@ -366,6 +396,7 @@
DA6C9D891CAE442B00094FBC /* MapboxDirections */ = {
isa = PBXGroup;
children = (
8D381B681FDB1009008D5A58 /* Extensions */,
DA6C9D8A1CAE442B00094FBC /* MapboxDirections.h */,
DD6254731AE70CB700017857 /* MBDirections.swift */,
DAC05F171CFC075300FA0071 /* MBRoute.swift */,
Expand Down Expand Up @@ -413,6 +444,7 @@
DA6C9DAD1CAEC93800094FBC /* Fixtures */ = {
isa = PBXGroup;
children = (
8D381B5F1FD9F592008D5A58 /* Responses */,
C5D1D7EE1F6AF8FA00A1C4F1 /* Instructions */,
C5247D721E818A8D004B6154 /* annotation */,
DA737EEC1D061514005BDA16 /* V4 */,
Expand Down Expand Up @@ -846,6 +878,8 @@
DA737EE51D05F91E005BDA16 /* v5_driving_dc_geojson.json in Resources */,
DA1A10CF1D00F975009F82FA /* v5_driving_dc_polyline.json in Resources */,
C5D1D7F11F6AF92500A1C4F1 /* instructions.json in Resources */,
8D381B661FDB089E008D5A58 /* apiDestinationName.json in Resources */,
8D381B641FDB0898008D5A58 /* noDestinationName.json in Resources */,
DA737EEF1D06175B005BDA16 /* v4_driving_dc_geojson.json in Resources */,
C53A022B1E92C281009837BD /* annotation.json in Resources */,
);
Expand All @@ -866,6 +900,8 @@
DA737EE61D05F91E005BDA16 /* v5_driving_dc_geojson.json in Resources */,
DA1A10F31D010251009F82FA /* v5_driving_dc_polyline.json in Resources */,
C5D1D7F21F6AF92600A1C4F1 /* instructions.json in Resources */,
8D381B671FDB089F008D5A58 /* apiDestinationName.json in Resources */,
8D381B651FDB0899008D5A58 /* noDestinationName.json in Resources */,
DA737EF01D06175B005BDA16 /* v4_driving_dc_geojson.json in Resources */,
C53A022C1E92C281009837BD /* annotation.json in Resources */,
);
Expand Down Expand Up @@ -893,8 +929,10 @@
DA737EE41D05F91E005BDA16 /* v5_driving_dc_geojson.json in Resources */,
DAC05F1C1CFC1E5300FA0071 /* v5_driving_dc_polyline.json in Resources */,
C5D1D7F01F6AF91700A1C4F1 /* instructions.json in Resources */,
8D381B611FD9F5B1008D5A58 /* noDestinationName.json in Resources */,
DA737EEE1D06175B005BDA16 /* v4_driving_dc_geojson.json in Resources */,
C5A3D3981E8188FE00D494A0 /* annotation.json in Resources */,
8D381B631FDB01D1008D5A58 /* apiDestinationName.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -970,6 +1008,7 @@
C57D55031DB566A700B94B74 /* MBIntersection.swift in Sources */,
C52CE38F1F6AF63C0069963D /* MBSpokenInstruction.swift in Sources */,
C58EA7AB1E9D7F73008F98CE /* MBCongestion.swift in Sources */,
8D381B6B1FDB3D8A008D5A58 /* String.swift in Sources */,
C547EC691DB59F8F009817F3 /* MBLane.swift in Sources */,
DA1A10C71D00F969009F82FA /* MBDirections.swift in Sources */,
);
Expand Down Expand Up @@ -1009,6 +1048,7 @@
C57D55041DB566A800B94B74 /* MBIntersection.swift in Sources */,
C52CE3901F6AF63D0069963D /* MBSpokenInstruction.swift in Sources */,
C58EA7AC1E9D7F74008F98CE /* MBCongestion.swift in Sources */,
8D381B6C1FDB3D8B008D5A58 /* String.swift in Sources */,
C547EC6A1DB59F90009817F3 /* MBLane.swift in Sources */,
DA1A10ED1D010247009F82FA /* MBDirections.swift in Sources */,
);
Expand Down Expand Up @@ -1048,6 +1088,7 @@
C57D55051DB566A900B94B74 /* MBIntersection.swift in Sources */,
C52CE3911F6AF63E0069963D /* MBSpokenInstruction.swift in Sources */,
C58EA7AD1E9D7F75008F98CE /* MBCongestion.swift in Sources */,
8D381B6D1FDB3D8B008D5A58 /* String.swift in Sources */,
C547EC6B1DB59F91009817F3 /* MBLane.swift in Sources */,
DA1A11041D0103A3009F82FA /* MBDirections.swift in Sources */,
);
Expand All @@ -1071,6 +1112,7 @@
DA6C9DA61CAE462800094FBC /* MBDirections.swift in Sources */,
C55FB44B1F6AEBF6006BD1E9 /* MBSpokenInstruction.swift in Sources */,
C58EA7AA1E9D7EAD008F98CE /* MBCongestion.swift in Sources */,
8D381B6A1FDB101F008D5A58 /* String.swift in Sources */,
C57D55011DB5669600B94B74 /* MBIntersection.swift in Sources */,
DA2E03E91CB0E0B000D1269A /* MBRouteStep.swift in Sources */,
);
Expand Down
7 changes: 7 additions & 0 deletions MapboxDirections/Extensions/String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

extension String {
var nonEmptyString: String? {
return !isEmpty ? self : nil
}
}
12 changes: 7 additions & 5 deletions MapboxDirections/MBRouteOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -480,19 +480,21 @@ open class RouteOptions: NSObject, NSSecureCoding, NSCopying{
internal func response(from json: JSONDictionary) -> ([Waypoint]?, [Route]?) {
var namedWaypoints: [Waypoint]?
if let jsonWaypoints = (json["waypoints"] as? [JSONDictionary]) {
namedWaypoints = zip(jsonWaypoints, waypoints).map { (api, local) -> Waypoint in
namedWaypoints = zip(jsonWaypoints, self.waypoints).map { (api, local) -> Waypoint in
let location = api["location"] as! [Double]
let coordinate = CLLocationCoordinate2D(geoJSON: location)
return Waypoint(coordinate: coordinate, name: local.name ?? api["name"] as? String)
let possibleAPIName = api["name"] as? String
let apiName = possibleAPIName?.nonEmptyString
return Waypoint(coordinate: coordinate, name: local.name ?? apiName)
}
}

let wpts = namedWaypoints ?? waypoints
let waypoints = namedWaypoints ?? self.waypoints

let routes = (json["routes"] as? [JSONDictionary])?.map {
Route(json: $0, waypoints: wpts, routeOptions: self)
Route(json: $0, waypoints: waypoints, routeOptions: self)
}
return (wpts, routes)
return (waypoints, routes)
}

// MARK: NSCopying
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"routes":[{"geometry":"afvnFdrebO@o@xT~@hAx@bAfAK\\c@]","legs":[{"summary":"Reading Road, Reading Road","weight":143.6,"duration":73.2,"steps":[{"intersections":[{"out":0,"entry":[true],"bearings":[95],"location":[-84.411389,39.27665]}],"driving_side":"right","geometry":"afvnFdrebO@o@","mode":"driving","maneuver":{"bearing_after":95,"bearing_before":0,"location":[-84.411389,39.27665],"type":"depart","instruction":"Head east on Hageman Street"},"weight":11.8,"duration":6.8,"name":"Hageman Street","distance":20.8},{"intersections":[{"out":1,"in":2,"entry":[true,true,false],"bearings":[0,180,270],"location":[-84.411148,39.276635]},{"out":2,"in":0,"entry":[false,false,true],"bearings":[0,30,180],"location":[-84.411372,39.274088]},{"out":1,"in":0,"entry":[false,true,true],"bearings":[0,180,270],"location":[-84.411386,39.273937]},{"out":2,"in":0,"entry":[false,true,true],"bearings":[0,30,180],"location":[-84.411401,39.273772]}],"driving_side":"right","geometry":"_fvnFtpebO`DJP@ZBvFTt@B\\B`@@fAFr@D","mode":"driving","maneuver":{"bearing_after":182,"bearing_before":94,"location":[-84.411148,39.276635],"modifier":"right","type":"turn","instruction":"Turn right onto Reading Road"},"weight":41.49999999999999,"pronunciation":"ˈrɛdɪŋ ˈɹoʊd","duration":36.4,"name":"Reading Road","distance":388.5},{"distance":97.7,"name":"Reading Road (US 42)","pronunciation":"ˈrɛdɪŋ ˈɹoʊd","ref":"US 42","maneuver":{"bearing_after":209,"bearing_before":184,"location":[-84.411469,39.273151],"modifier":"slight right","type":"continue","instruction":"Make a slight right to stay on Reading Road (US 42)"},"weight":13.6,"mode":"driving","geometry":"epunFtrebOXPLJHFJHJHVVFHb@d@","intersections":[{"out":2,"in":0,"entry":[false,true,true],"bearings":[0,180,210],"location":[-84.411469,39.273151]},{"out":2,"in":0,"entry":[false,false,true],"bearings":[30,75,210],"location":[-84.411756,39.27278]},{"out":2,"in":0,"entry":[false,false,true],"bearings":[30,75,225],"location":[-84.411875,39.272659]}],"duration":10.7,"driving_side":"right"},{"intersections":[{"out":2,"in":0,"entry":[false,true,true],"bearings":[45,225,300],"location":[-84.412124,39.272437]}],"driving_side":"right","geometry":"wkunFvvebOK\\","mode":"driving","maneuver":{"bearing_after":299,"bearing_before":219,"location":[-84.412124,39.272437],"modifier":"right","type":"turn","instruction":"Turn right"},"weight":34,"duration":8.7,"name":"","distance":14.9},{"intersections":[{"out":0,"in":1,"entry":[true,false,true],"bearings":[30,120,300],"location":[-84.412274,39.272503]}],"driving_side":"right","geometry":"clunFtwebOEAGEUU","mode":"driving","maneuver":{"bearing_after":28,"bearing_before":299,"location":[-84.412274,39.272503],"modifier":"right","type":"turn","instruction":"Turn right"},"weight":42.7,"duration":10.6,"name":"","distance":23.7},{"intersections":[{"in":0,"entry":[true],"bearings":[220],"location":[-84.412115,39.272675]}],"driving_side":"right","geometry":"gmunFvvebO","mode":"driving","maneuver":{"bearing_after":0,"bearing_before":40,"location":[-84.412115,39.272675],"modifier":"left","type":"arrive","instruction":"You have arrived at your destination, on the left"},"weight":0,"duration":0,"name":"","distance":0}],"distance":545.6}],"weight_name":"routability","weight":143.6,"duration":73.2,"distance":545.6}],"waypoints":[{"name":"Hageman Street","location":[-84.411389,39.27665]},{"name":"testpass","location":[-84.412115,39.272675]}],"code":"Ok","uuid":"cjax1m38b0dz85op9878nbugo"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"routes":[{"geometry":"afvnFdrebO@o@xT~@hAx@bAfAK\\c@]","legs":[{"summary":"Reading Road, Reading Road","weight":143.6,"duration":73.2,"steps":[{"intersections":[{"out":0,"entry":[true],"bearings":[95],"location":[-84.411389,39.27665]}],"driving_side":"right","geometry":"afvnFdrebO@o@","mode":"driving","maneuver":{"bearing_after":95,"bearing_before":0,"location":[-84.411389,39.27665],"type":"depart","instruction":"Head east on Hageman Street"},"weight":11.8,"duration":6.8,"name":"Hageman Street","distance":20.8},{"intersections":[{"out":1,"in":2,"entry":[true,true,false],"bearings":[0,180,270],"location":[-84.411148,39.276635]},{"out":2,"in":0,"entry":[false,false,true],"bearings":[0,30,180],"location":[-84.411372,39.274088]},{"out":1,"in":0,"entry":[false,true,true],"bearings":[0,180,270],"location":[-84.411386,39.273937]},{"out":2,"in":0,"entry":[false,true,true],"bearings":[0,30,180],"location":[-84.411401,39.273772]}],"driving_side":"right","geometry":"_fvnFtpebO`DJP@ZBvFTt@B\\B`@@fAFr@D","mode":"driving","maneuver":{"bearing_after":182,"bearing_before":94,"location":[-84.411148,39.276635],"modifier":"right","type":"turn","instruction":"Turn right onto Reading Road"},"weight":41.49999999999999,"pronunciation":"ˈrɛdɪŋ ˈɹoʊd","duration":36.4,"name":"Reading Road","distance":388.5},{"distance":97.7,"name":"Reading Road (US 42)","pronunciation":"ˈrɛdɪŋ ˈɹoʊd","ref":"US 42","maneuver":{"bearing_after":209,"bearing_before":184,"location":[-84.411469,39.273151],"modifier":"slight right","type":"continue","instruction":"Make a slight right to stay on Reading Road (US 42)"},"weight":13.6,"mode":"driving","geometry":"epunFtrebOXPLJHFJHJHVVFHb@d@","intersections":[{"out":2,"in":0,"entry":[false,true,true],"bearings":[0,180,210],"location":[-84.411469,39.273151]},{"out":2,"in":0,"entry":[false,false,true],"bearings":[30,75,210],"location":[-84.411756,39.27278]},{"out":2,"in":0,"entry":[false,false,true],"bearings":[30,75,225],"location":[-84.411875,39.272659]}],"duration":10.7,"driving_side":"right"},{"intersections":[{"out":2,"in":0,"entry":[false,true,true],"bearings":[45,225,300],"location":[-84.412124,39.272437]}],"driving_side":"right","geometry":"wkunFvvebOK\\","mode":"driving","maneuver":{"bearing_after":299,"bearing_before":219,"location":[-84.412124,39.272437],"modifier":"right","type":"turn","instruction":"Turn right"},"weight":34,"duration":8.7,"name":"","distance":14.9},{"intersections":[{"out":0,"in":1,"entry":[true,false,true],"bearings":[30,120,300],"location":[-84.412274,39.272503]}],"driving_side":"right","geometry":"clunFtwebOEAGEUU","mode":"driving","maneuver":{"bearing_after":28,"bearing_before":299,"location":[-84.412274,39.272503],"modifier":"right","type":"turn","instruction":"Turn right"},"weight":42.7,"duration":10.6,"name":"","distance":23.7},{"intersections":[{"in":0,"entry":[true],"bearings":[220],"location":[-84.412115,39.272675]}],"driving_side":"right","geometry":"gmunFvvebO","mode":"driving","maneuver":{"bearing_after":0,"bearing_before":40,"location":[-84.412115,39.272675],"modifier":"left","type":"arrive","instruction":"You have arrived at your destination, on the left"},"weight":0,"duration":0,"name":"","distance":0}],"distance":545.6}],"weight_name":"routability","weight":143.6,"duration":73.2,"distance":545.6}],"waypoints":[{"name":"Hageman Street","location":[-84.411389,39.27665]},{"name":"","location":[-84.412115,39.272675]}],"code":"Ok","uuid":"cjax1m38b0dz85op9878nbugo"}
37 changes: 37 additions & 0 deletions MapboxDirectionsTests/RouteOptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,43 @@ class RouteOptionsTests: XCTestCase {
XCTAssertFalse(testInstance === copy, "Test Instance and copy should not be identical.")

}

//MARK: API Name Handling Tests
private static var testWaypoints: [Waypoint] { return [Waypoint(coordinate: CLLocationCoordinate2D(latitude: 39.27664, longitude:-84.41139)),
Waypoint(coordinate: CLLocationCoordinate2D(latitude: 39.27277, longitude:-84.41226))]}


private func response(for fixtureName: String, waypoints: [Waypoint] = testWaypoints) -> (waypoints:[Waypoint], route:Route)? {
let testBundle = Bundle(for: type(of: self))
guard let fixtureURL = testBundle.url(forResource:fixtureName, withExtension:"json") else { XCTFail(); return nil }
guard let fixtureData = try? Data(contentsOf: fixtureURL, options:.mappedIfSafe) else {XCTFail(); return nil }
guard let fixtureOpaque = try? JSONSerialization.jsonObject(with: fixtureData), let fixture = fixtureOpaque as? JSONDictionary else { XCTFail(); return nil }

let subject = RouteOptions(waypoints: waypoints)
let response = subject.response(from: fixture)

guard let waypoints = response.0, let routes = response.1 else { XCTFail("Expected responses not returned from service"); return nil}
guard let primary = routes.first else {XCTFail("Expected a route in response"); return nil}
return (waypoints:waypoints, route:primary)
}

func testResponseWithoutDestinationName() {
let response = self.response(for: "noDestinationName")!
XCTAssert(response.route.legs.last!.destination.name == nil, "API waypoint with no name (aka \"\") needs to be represented as `nil`.")
}

func testResponseWithDestinationName() {
let response = self.response(for: "apiDestinationName")!
XCTAssert(response.route.legs.last!.destination.name == "testpass", "Waypoint name in fixture response not parsed correctly.")
}

func testResponseWithManuallySetDestinationName() {
let manuallySet = RouteOptionsTests.testWaypoints
manuallySet.last!.name = "manuallyset"

let response = self.response(for: "apiDestinationName", waypoints: manuallySet)!
XCTAssert(response.route.legs.last!.destination.name == "manuallyset", "Waypoint with manually set name should override any computed name.")
}
}

private extension RouteOptions {
Expand Down

0 comments on commit cd72999

Please sign in to comment.