Turfjs / Turfjs/turf

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

Open Beginner friendly
#3,086 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
10.5k
Forks
1k
Avg merge
1h 11m
Merged PRs (30d)
4

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the @turf/midpoint entry point and read packages/turf-destination/index.ts, especially the coordinate handling shown in the report. Verify the 3D midpoint behavior against the reproducible example; done means both defined altitudes produce their linear average while inputs missing either z-coordinate retain the current no-z behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.