turf icon indicating copy to clipboard operation
turf copied to clipboard

Bug: Distance calculating incorrectly

Open SukhKalsi opened this issue 7 years ago • 1 comments

I don't have a GeoJSON file however if you copy the snippet below into Turf's website [http://turfjs.org] and use console, this can be reproduced.

Issue I'm having is the distance appears to be calculating incorrectly from what I can see. This can be confirmed when using this site as well - https://www.geodatasource.com/distance-calculator

Scenario: As a user from Apple Store in Regents Street London, Welwyn Garden City is showing closer distance to me than St Albans.

Snippet of code showing the Turf output

const appleStore = turf.point([51.5142284, -0.1419788]);
const welwynGardenCity = turf.point([51.800957, -0.203081]);
const stAlbans = turf.point([51.748829, -0.326842]);

const appleToWelwyn = turf.distance(appleStore, welwynGardenCity, {units: 'miles'});
const appleToStAlbans = turf.distance(appleStore, stAlbans, {units: 'miles'});

console.log(appleToWelwyn); // Returns: 20.255807516378002
console.log(appleToStAlbans); // Returns: 20.63695359074288

Using geodatasource.com website as cited earlier produces the following results which when looking at Google Maps shows more "truer" result IMO:

Apple to Welwyn -> 19.98 Miles Apple to St Albans -> 18.04 Miles

SukhKalsi avatar May 21 '19 15:05 SukhKalsi

@SukhKalsi you are using the wrong coordinates, Turf uses [lng, lat] convention, read docs:

// noticed the inverted coords
const appleStore = turf.point([-0.1419788, 51.5142284]);
const welwynGardenCity = turf.point([-0.203081, 51.800957]);
const stAlbans = turf.point([-0.326842, 51.748829]);

const appleToWelwyn = turf.distance(appleStore, welwynGardenCity, {units: 'miles'});
// 19.983423279415998
const appleToStAlbans = turf.distance(appleStore, stAlbans, {units: 'miles'});
// 18.044411034900413

stebogit avatar May 21 '19 15:05 stebogit