Turfjs / Turfjs/turf

nearestPointOnLine returns invalid segmentIndex when result is at the end of the last segment of the lineString

Open
#3,023 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

next-major-release
Dominant language
TypeScript
Stars
10.5k
Forks
1k
Avg merge
1h 11m
Merged PRs (30d)
4

Description

When calling nearestPointOnLine and the result is at the end of a segment, there is legacy behaviour that sets the segmentIndex to that of the next segment (end point of one segment being the same as the start point of the next). Unsure why this is the behaviour.

Unfortunately, if the original segment was the last in the lineString the segmentIndex is still incremented meaning it now refers to a non-existent segment.

After fixing #2946 with PR #2951, it became apparent in #3008 some users were using the overflowing segmentIndex as an indicator for whether the nearest point was found at the end of the line. Although undocumented, this caused backwards compatibility issues and will be reverted.

This issue aims to fix the problem properly as a breaking change aligned with a major release. Current plan is to:

  1. ensure segmentIndex always refers to a valid segment
  2. clarify remaining behaviour in #3022

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the nearestPointOnLine entry point and review the behavior described in #2946, PR #2951, #3008, and #3022. Trace how the final segment's index is assigned and confirm the breaking-change plan. Done means segmentIndex always refers to a valid segment and the remaining behavior is clarified.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.