Skip to content

fix: align stop direction calculation with OBA Java semantics#477

Open
3rabiii wants to merge 1 commit intoOneBusAway:mainfrom
3rabiii:fix/stops-direction-logic
Open

fix: align stop direction calculation with OBA Java semantics#477
3rabiii wants to merge 1 commit intoOneBusAway:mainfrom
3rabiii:fix/stops-direction-logic

Conversation

@3rabiii
Copy link
Contributor

@3rabiii 3rabiii commented Feb 25, 2026

Resolves #135

What changed?

This PR completely aligns the Golang AdvancedDirectionCalculator with the original OBA Java implementation. Previously, the Go server produced different or missing stop directions due to several mathematical inconsistencies compared to the Java logic.

Key mathematical & logical fixes introduced:

  • Variance Threshold Fix (The Square Root Trap): The previous Go logic calculated the variance but incorrectly applied math.Sqrt() before comparing it against the varianceThreshold (0.7). This caused valid directions to be discarded. This PR compares the variance directly, matching Java.
  • Population vs. Sample Variance: Updated the variance function to calculate Population Variance (dividing by n instead of n-1) to perfectly match the exact scaling of the Java threshold.
  • Flat-Earth Approximation: Replaced the heavy spherical utils.BearingBetweenPoints with the OBA Java's exact flat-earth coordinate approximation using math.Atan2(dy, dx * cos(lat)) for short distances.
  • Shape Point Window bounds: Fixed an off-by-one bug in the shape point window array bounds (indexTo-1 changed to indexTo) to strictly match Java's 5-point window span.
  • Test Updates: Updated unit tests to expect the corrected Population Variance outputs and ensure complete coverage for the new mathematical logic.
image

@aaronbrethorst
fixes : #135

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect Stop Direction Compared to Java Output

1 participant