Skip to content

fix(@turf/midpoint): interpolate altitude (z-coordinate) for 3D points#3087

Open
JSap0914 wants to merge 1 commit into
Turfjs:masterfrom
JSap0914:fix/midpoint-altitude-interpolation
Open

fix(@turf/midpoint): interpolate altitude (z-coordinate) for 3D points#3087
JSap0914 wants to merge 1 commit into
Turfjs:masterfrom
JSap0914:fix/midpoint-altitude-interpolation

Conversation

@JSap0914

Copy link
Copy Markdown
Contributor

Summary

Fixes @turf/midpoint returning the wrong z-coordinate when both input points have altitude (elevation).

Closes #3086

Root Cause

midpoint(p1, p2) calls destination(p1, dist/2, heading).
destination() propagates only the origin's z unchanged, so the result always got z = z1, ignoring z2 entirely.

Fix

After computing the geographic midpoint, extract both z-values via getCoord() and set:

midpoint.geometry.coordinates[2] = (coord1[2] + coord2[2]) / 2;

This runs only when both input points have a z-coordinate; 2D inputs are unaffected.

Changes

  • packages/turf-midpoint/index.ts — average z when both inputs have altitude
  • packages/turf-midpoint/package.json — add @turf/invariant as explicit dependency
  • packages/turf-midpoint/test.ts — two new tests (3D interpolation + 2D unchanged)

Test results

Before fix:

# tests 8 / # pass 7 / # fail 1
not ok 7 z should be the average of the two input altitudes (100+200)/2 = 150
  actual:   100   expected: 150

After fix:

# tests 8 / # pass 8

When both input points have a z-coordinate (altitude), the midpoint
should return a point whose z equals the average (z1 + z2) / 2.

Previously, destination() was called with point1 as the origin, which
propagated point1's z unchanged to the result.  The fix extracts both
z values and assigns the linear average when both are present.

Adds @turf/invariant as an explicit dependency (already a transitive dep
via @turf/destination) to access getCoord().
Copilot AI review requested due to automatic review settings June 30, 2026 04:08

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.

bug(@turf/midpoint): altitude (z-coordinate) not interpolated for 3D points

2 participants