Skip to content

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

Description

@JSap0914

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions