Bug Report
Package: @turf/midpoint
Description
When both input points carry a z-coordinate (altitude/elevation), midpoint() returns a point whose z equals the origin's z unchanged instead of the linear average (z1 + z2) / 2.
Reproducible example
import { midpoint } from '@turf/midpoint';
import { point } from '@turf/helpers';
const p1 = point([0, 0, 100]); // altitude 100
const p2 = point([10, 0, 200]); // altitude 200
const mid = midpoint(p1, p2);
console.log(mid.geometry.coordinates[2]);
// Actual: 100 ← same as p1's z
// Expected: 150 ← (100 + 200) / 2
Root Cause
midpoint() delegates to destination(origin, dist/2, heading).
destination() propagates only the origin's z-coordinate unchanged (see source):
if (coordinates1[2] !== undefined) {
return point([lng, lat, coordinates1[2]], options.properties);
}
Because midpoint passes point1 as the origin, the result always gets z = z1, discarding z2 entirely.
Expected Behaviour
The midpoint of two 3D points should carry z = (z1 + z2) / 2.
Fix
Extract both z-values and average them when both are defined; fall through to current behaviour (no z) when either is absent.
Bug Report
Package:
@turf/midpointDescription
When both input points carry a z-coordinate (altitude/elevation),
midpoint()returns a point whose z equals the origin's z unchanged instead of the linear average(z1 + z2) / 2.Reproducible example
Root Cause
midpoint()delegates todestination(origin, dist/2, heading).destination()propagates only the origin's z-coordinate unchanged (see source):Because
midpointpassespoint1as the origin, the result always getsz = z1, discardingz2entirely.Expected Behaviour
The midpoint of two 3D points should carry
z = (z1 + z2) / 2.Fix
Extract both z-values and average them when both are defined; fall through to current behaviour (no z) when either is absent.