Skip to content

fix(@turf/line-slice-along): interpolate altitude on sliced endpoints#3084

Open
JSap0914 wants to merge 1 commit into
Turfjs:masterfrom
JSap0914:fix/line-slice-along-altitude-3007
Open

fix(@turf/line-slice-along): interpolate altitude on sliced endpoints#3084
JSap0914 wants to merge 1 commit into
Turfjs:masterfrom
JSap0914:fix/line-slice-along-altitude-3007

Conversation

@JSap0914

Copy link
Copy Markdown
Contributor

Closes #3007

Bug

When PR #2852 updated destination() to pass through its origin's altitude, lineSliceAlong began emitting wrong elevations for the interpolated start/stop vertices it creates whenever a slice boundary falls in the middle of a segment.

The function calls destination(coords[i], overshot, bearing) — where coords[i] is the far end of the current segment — so the copied altitude was always that of the far endpoint, regardless of where the slice actually fell.

Reproduction (from issue #3007):

const line = turf.lineString([[0, 0, 0], [0, 0.0009, 100]]); // ~100 m, elev 0→100 m
const sliced = turf.lineSliceAlong(line, 0, 50, { units: 'meters' });
// sliced endpoint elevation: 100 ← wrong; expected ≈ 50

Fix

After computing the 2-D destination, calculate the fractional position of the interpolated vertex along its host segment and derive the altitude as a linear interpolation between the two segment endpoints:

fraction = 1 - |overshot| / segDist
altitude = from[2] + fraction * (to[2] - from[2])

If either endpoint has no altitude the altitude component is removed from the output, preserving the 2-D behaviour that existed before #2852.

Verification

Three new tests added to packages/turf-line-slice-along/test.ts:

Test Before fix After fix
Stop vertex altitude on midpoint slice (expected ≈50 m) ❌ 100 ✅ 49.96
Start vertex altitude when slice starts mid-segment (expected ≈25 m) ❌ 100 ✅ 24.98
2-D line produces 2-D interpolated vertex ✅ passes ✅ passes

All 21 pre-existing tests continue to pass unchanged.

…closes Turfjs#3007)

When destination() was updated (Turfjs#2852) to pass through the origin's altitude,
lineSliceAlong began emitting wrong altitudes for interpolated start/stop
vertices: it copied the near segment endpoint's elevation instead of
interpolating linearly between the two segment endpoints.

Fix: after computing the 2-D destination, calculate the fractional position
of the interpolated vertex along its host segment and derive the altitude as
a linear interpolation.  When either segment endpoint lacks an altitude, any
altitude that destination() may have attached is stripped so the result stays
2-D—matching behaviour before Turfjs#2852.

Adds three tests:
  - stop vertex altitude ≈50 m when slicing midpoint of [0→100 m] segment
  - start vertex altitude ≈25 m when slicing starts at 25 m
  - 2-D lines produce 2-D interpolated vertices (no spurious altitude)
Copilot AI review requested due to automatic review settings June 29, 2026 04:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

Unexpected elevation output when using line-chunk / line-slice-along

2 participants