Fixed bugs in closestCoordinate and addArrow functions.#284
Fixed bugs in closestCoordinate and addArrow functions.#284bsudekum merged 1 commit intomapbox:masterfrom
Conversation
|
I have closed the referenced issue, as it emerged from a change in my code. |
|
@hardsetting thanks for the PR! Could you distill this down to a simple example and maybe link some screenshots to geojson.io? My first instinct is that adding /cc @1ec5 |
1ec5
left a comment
There was a problem hiding this comment.
I agree with all the changes proposed here except for the reversal of the first addend in shaftCoordinates and shaftStrokeCoordinates, since the polyline should already be reversed.
| } | ||
| if intersectionDistance != nil && intersectionDistance! < closestCoordinate?.distance ?? CLLocationDistanceMax { | ||
| closestCoordinate = CoordinateAlongPolyline(coordinate: intersectionPoint!, index: index, distance: intersectionDistance!) | ||
| closestCoordinate = CoordinateAlongPolyline(coordinate: intersectionPoint!, index: (distances.0 < distances.1 ? index : index+1), distance: intersectionDistance!) |
There was a problem hiding this comment.
This makes sense, although it does diverge from Turf.js behavior. polyline(along:from:to:) makes use of the index; the only problem would be if both the from and to coordinates passed into polyline(along:from:to:) fell closest to the same vertex on the polyline, in which case the returned array would be empty. But it’s quite an edge case, and I think it would be a problem regardless of this change.
There was a problem hiding this comment.
It turns out this change does cause polyline(along:from:to:) (now LineString.sliced(from:to:)) to behave erratically: mapbox/turf-swift#27 (comment).
| let shaftLength = max(min(50 * metersPerPoint(atLatitude: maneuverCoordinate!.latitude), 50), 10) | ||
| let shaftCoordinates = polyline(along: polylineCoordinates!, within: -shaftLength / 2, of: maneuverCoordinate!) | ||
| + polyline(along: polylineCoordinates!, within: shaftLength, of: maneuverCoordinate!) | ||
| let shaftCoordinates = Array(polyline(along: polylineCoordinates!, within: -shaftLength / 2, of: maneuverCoordinate!).reversed() |
There was a problem hiding this comment.
polyline(along:within:of:) already reverses the polyline, so there’s no need to do it here too.
| let shaftCoordinates = polyline(along: polylineCoordinates!, within: -shaftLength / 2, of: maneuverCoordinate!) | ||
| + polyline(along: polylineCoordinates!, within: shaftLength, of: maneuverCoordinate!) | ||
| let shaftCoordinates = Array(polyline(along: polylineCoordinates!, within: -shaftLength / 2, of: maneuverCoordinate!).reversed() | ||
| + polyline(along: polylineCoordinates!, within: shaftLength, of: maneuverCoordinate!).suffix(from: 1)) |
There was a problem hiding this comment.
mapbox/mapbox-gl-native#8808 (which is in the v3.6.0 betas) makes the map SDK more resilient to redundant vertices, but it’s good to address the root cause too. 👍
| } | ||
| if intersectionDistance != nil && intersectionDistance! < closestCoordinate?.distance ?? CLLocationDistanceMax { | ||
| closestCoordinate = CoordinateAlongPolyline(coordinate: intersectionPoint!, index: index, distance: intersectionDistance!) | ||
| closestCoordinate = CoordinateAlongPolyline(coordinate: intersectionPoint!, index: (distances.0 < distances.1 ? index : index+1), distance: intersectionDistance!) |
There was a problem hiding this comment.
My first instinct is that adding
index + 1will in fact return the next closest coordinate and not the actual closest coordinate.
@bsudekum, this change can be seen as completing the work begun in #39, making the index more consistent with the returned coordinate. index initially points to the vertex at the beginning of the segment being examined, so if the coordinate is closer to the end of the segment, the closest vertex would be at index + 1.
FWIW, I believe at one point this function did accept an optional
look next point?argument.
You’re probably thinking of includeDistanceToNextCoordinate, which was removed in #9. In addition to conditionally incrementing the index in this third case, the includeDistanceToNextCoordinate argument also updated the intersectionDistance (incorrectly).
|
@hardsetting this is close! have a sec to update the PR with @1ec5's feedback? |
|
I apologize for the delay, have been busy for the last couple weeks. Regarding the reversal of first half of the shaft, the |
1ec5
left a comment
There was a problem hiding this comment.
Ah, rereading the implementation of shaftCoordinates, I understand why you un-reversed the first half of the shaft. The implementation on master starts at the maneuver point, goes backwards, then skips back to the maneuver point before going forward to the tip of the arrow. The implementation here simply ensures continuity between the two halves of the shaft. 👍
* Begin adding telem events * fix * add timezone * Update * Add remaining events * conditional import * Move back to core * make private * Consolidate telemetry state, outline full telemetry spec * Update date format * Store more state; add device * Updated telemetry event handling * Round events * Add dialog view controller * Reset telemetry session if route updated after arrival * Remove logs; trailing closure syntax * Add userId to feedback event * Hide feedback dialog after 0.5s * Moar trailing closure * Add temporary pod dependency on Mapbox-iOS-SDK * Pass nil feedback description for now * Extend locations collected * Fix location serialization * Reduce locations collected * More battery level precision * remove battery enable flag * Add snapshot to reroute event * Add update + cancel feedback functions * Screenshots as jpeg * Wait until style has loaded before doing things to it (#339) * Fix location permissions and adopt iOS 11 description * Fixed bugs in closestCoordinate and addArrow functions. (#284) * Defer simulated location updates until next run loop (#344) * Did arrive once (#347) * add back * Use telem library * add init * add * add release * fix string * updater * fix * update args * add framework * add certs * move certs * struct * more struct * add * add to tests * add to obj-c * remove * update * bump * Add upcoming step information * Remove telem staging token check (handled upstream) * Add previous step information * Only capitalize first character of step maneuver keys * copy framework * trailing * add framework * add access token * fix * make optional * rename * update event lib * name not version * Better types * Update * No telem in sim * add back * Dont pass in access token * fix * Switch to nested step dictionary in feedback events * Add debug metrics user info key * Fix feedback event types * Moved all event dictionary generation to MMEEventsManager * Move feedback event dict update logic * Bump * Pin to MapboxDirections.swift 0.10.1
Referring to #283
In closestCoordinate function:
when the closest point to the polyline segment is the projection, now the index is set to the index of the coordinate closest to the intersection.
In addArrow function:
shafts are now generated by concatenating the points before the maneuver in the right order with the points after the maneuver, making sure the closest point to the maneuver is taken once.
EDIT:
I now realize that the changes in closestCoordinate might not be strictly necessary, but it's probably clearer and safer now anyways.