Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misencoded geometry after converting map matching response to RouteResponse #439

Closed
1ec5 opened this issue Jul 31, 2020 · 2 comments · Fixed by #440
Closed

Misencoded geometry after converting map matching response to RouteResponse #439

1ec5 opened this issue Jul 31, 2020 · 2 comments · Fixed by #440
Labels
Milestone

Comments

@1ec5
Copy link
Contributor

1ec5 commented Jul 31, 2020

As of v0.40.0, the RouteResponse(matching:options:credentials:) initializer, which Directions.calculateRoutes(matching:completionHandler:) uses to convert a Map Matching API response to Directions API–like model objects, produces Route objects whose shapes are misencoded. The coordinates are all shrunken towards Null Island. For example, a route around Cupertino only has coordinates off the coast of Liberia.

RouteResponse(matching:options:credentials:) encodes each Match object as JSON then decodes it as a Route. Whenever we encode or decode to and from a Route or Match, we need to pass in an options object; otherwise, PolyLineString(from:) defaults to RouteShapeFormat.default, which is Polyline5 encoding. We were setting the options on the decoder but not the encoder. Had the options been provided in both directions or omitted entirely, the geometry would’ve been roundtripped correctly. But d98e073 for #406 added the options in only one direction:

let decoder = JSONDecoder()
let encoder = JSONEncoder()
decoder.userInfo[.options] = options
decoder.userInfo[.credentials] = credentials

The following test is supposed to exercise this scenario, but it gets lucky because it doesn’t specify a shape format explicitly, so both the encoder and decoder end up using the same Polyline5 encoding by default:

// uses an atypical precision level for polyline encoding
XCTAssertEqual(round(route.shape!.coordinates.first!.latitude), 33)
XCTAssertEqual(round(route.shape!.coordinates.first!.longitude), -117)
XCTAssertEqual(route.legs.count, 6)

If I modify the test to set the shape format explicitly, the test fails:

diff --git a/Tests/MapboxDirectionsTests/RoutableMatchTests.swift b/Tests/MapboxDirectionsTests/RoutableMatchTests.swift
index 7d84bd5..047a0db 100644
--- a/Tests/MapboxDirectionsTests/RoutableMatchTests.swift
+++ b/Tests/MapboxDirectionsTests/RoutableMatchTests.swift
@@ -29,6 +29,7 @@ class RoutableMatchTest: XCTestCase {
         var routeResponse: RouteResponse!
         
         let matchOptions = MatchOptions(coordinates: locations)
+        matchOptions.shapeFormat = .polyline6
         matchOptions.includesSteps = true
         matchOptions.routeShapeResolution = .full
         for waypoint in matchOptions.waypoints[1..<(locations.count - 1)] {
Test Case '-[MapboxDirectionsTests.RoutableMatchTest testRoutableMatch]' started.
/Users/mxn/hub/MapboxDirections.swift/Tests/MapboxDirectionsTests/RoutableMatchTests.swift:71: error: -[MapboxDirectionsTests.RoutableMatchTest testRoutableMatch] : XCTAssertEqual failed: ("0.0") is not equal to ("33.0")
/Users/mxn/hub/MapboxDirections.swift/Tests/MapboxDirectionsTests/RoutableMatchTests.swift:72: error: -[MapboxDirectionsTests.RoutableMatchTest testRoutableMatch] : XCTAssertEqual failed: ("-1.0") is not equal to ("-117.0")
/Users/mxn/hub/MapboxDirections.swift/Tests/MapboxDirectionsTests/RoutableMatchTests.swift:99: error: -[MapboxDirectionsTests.RoutableMatchTest testRoutableMatch] : XCTAssertEqual failed: ("0.0") is not equal to ("33.0")
/Users/mxn/hub/MapboxDirections.swift/Tests/MapboxDirectionsTests/RoutableMatchTests.swift:100: error: -[MapboxDirectionsTests.RoutableMatchTest testRoutableMatch] : XCTAssertEqual failed: ("-1.0") is not equal to ("-117.0")
Test Case '-[MapboxDirectionsTests.RoutableMatchTest testRoutableMatch]' failed (18.400 seconds).

/cc @mapbox/navigation-ios

@1ec5 1ec5 added the bug label Jul 31, 2020
@1ec5 1ec5 added this to the v0.31.1 milestone Jul 31, 2020
@1ec5
Copy link
Contributor Author

1ec5 commented Jul 31, 2020

Turns out this is fixed by #437.

@1ec5
Copy link
Contributor Author

1ec5 commented Jul 31, 2020

Now moving in #440 due to a Git snafu.

@1ec5 1ec5 closed this as completed in #440 Jul 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant