Repository navigation
Fix point-origin costs in reverse street searches and keep the origin cost in State.reverse() - #1027
Open
rafapereirabr wants to merge 1 commit into
Open
rafapereirabr wants to merge 1 commit into
rafapereirabr wants to merge 1 commit into
Conversation
rafapereirabr
pushed a commit
to ipea/r5r
that referenced
this pull request
Nov 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Reverse street searches started from a coordinate (
reverseSearch = true+setOrigin(lat, lon), used for egress inPointToPointQueryandProfileResponse) get wrong costs near the origin point. The vertex closer to the point looks more expensive than the farther one. Paths rebuilt from the search withnew StreetPath(state, network, true)report less time and distance than the search itself used, because they leave out the partial street segment and the off-street walk to the point. On real networks this shortens egress legs and can change which stops are chosen: on a Porto Alegre sample data used in r5r, egress walks in r5r ended up to 1.27 km short of the destination and itineraries were up to 25 min faster than the same trips intravel_time_matrix().Cause
StreetRouter.setOrigin(lat, lon), the two start states are costed for a forward search: the state atvertex1getsdistance1and the state atvertex0getsdistance0. WhenreverseSearchis true, the method swaps the vertices but not the costs (see the existingFIXME). The state atvertex0therefore carries the length of the other part of the edge.State.reverse()starts from a zero-cost copy and adds only the differences between consecutive states. The cost already held by the first state (partial edge plus off-street walk) is never added.Fix
setOrigin(lat, lon), each start state gets the distance of the part of its own back edge that the traveler actually covers:distance0along the forward edge fromvertex0,distance1along the backward edge fromvertex1, at that edge's speed. Forward searches, vertices, back edges and turn restrictions are unchanged. TheFIXMEis replaced by a comment.State.reverse()adds the first state's duration and distance to the reversed path. Nothing changes when that state has no cost, e.g. aftersetOrigin(vertex).Only reverse searches are affected. Regional and single-point analyses do not use them, so their results do not change. The partial-edge speed model and
StreetPathgeometry are unchanged.Tests
New
ReversePointOriginTest: each vertex gets the cost of its own part of the split edge, and a reversedStreetPathkeeps the duration and distance of its state. Both fail ondevwithout this change. All 35 tests incom.conveyal.r5.streetspass. A local Windows run of the full suite gave 8 failures, all from temporary files Windows could not delete in non-routing tests (FlexNetworkBuildTest,SceneBuilderTest,OSMTest).Found while working on r5r's detailed itineraries (ipea/r5r#607, by @e-kotov), where both issues are currently worked around.