fix(@turf/midpoint): interpolate altitude (z-coordinate) for 3D points#3087
Open
JSap0914 wants to merge 1 commit into
Open
fix(@turf/midpoint): interpolate altitude (z-coordinate) for 3D points#3087JSap0914 wants to merge 1 commit into
JSap0914 wants to merge 1 commit into
Conversation
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().
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.
Summary
Fixes
@turf/midpointreturning the wrong z-coordinate when both input points have altitude (elevation).Closes #3086
Root Cause
midpoint(p1, p2)callsdestination(p1, dist/2, heading).destination()propagates only the origin's z unchanged, so the result always gotz = z1, ignoringz2entirely.Fix
After computing the geographic midpoint, extract both z-values via
getCoord()and set: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 altitudepackages/turf-midpoint/package.json— add@turf/invariantas explicit dependencypackages/turf-midpoint/test.ts— two new tests (3D interpolation + 2D unchanged)Test results
Before fix:
After fix: