mapbox / mapbox/turf-swift

Haversine distance should use spherical Earth radius

Open
#26 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug JS parity
Dominant language
Swift
Stars
271
Forks
65
PR merge metrics
No merged PRs in 30d

Description

This library currently defines the radius of the Earth as 6 373 000 m for the purpose of converting between meters and radians in the Haversine formula:

https://github.com/mapbox/turf-swift/blob/40c7d772c46837aa9e5ceb60d22d8ad97a4be708/Turf/Turf.swift#L7

Apparently this value came from [an old version of turf-distance](https://github.com/Turfjs/turf-distance/blob/cea4c6cbec582bff8880193a3c2cda38ae731967/index.js#L78) that I originally ported to an internal Swift application, before it got copied into another internal Swift application, then copied into the navigation SDK, then moved here. It’s close to the value of 6 372 797.560 856 that [Osmium](https://github.com/Project-OSRM/osrm-backend/blob/bb1f4a025a3cefd3598a38b9d3e55485d1080ec5/third_party/libosmium/include/osmium/geom/haversine.hpp#L58-L59) describes as “Earth’s quadratic mean radius for WGS84”.

These days, Turf.js uses [a spherical approximation](https://github.com/Turfjs/turf/blob/a94151418cb969868fdb42955a19a133512da0fd/packages/turf-helpers/index.js#L4) of 6 671 008.8 m for its radian-to-meter conversion and Haversine formula. The Haversine formula was always meant to be used with a spherical meters-per-radian value. We should probably change this value to match Turf.js. 🙀

/ref Turfjs/turf#978 Turfjs/turf#1012 Turfjs/turf#1176 https://github.com/mapbox/turf-swift/pull/21#discussion_r157246151
/cc @frederoni @bsudekum @captainbarbosa

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 Turf/Turf.swift at the Earth-radius definition linked in the issue, then compare the Haversine conversion with the referenced Turf.js spherical value. Done means updating the radius to the agreed spherical value and confirming the affected meter-to-radian and Haversine calculations use it consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.