Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getDistanceSimple() returns NaN with specific parameters #129

Open
emcsween opened this issue Apr 6, 2017 · 12 comments
Open

getDistanceSimple() returns NaN with specific parameters #129

emcsween opened this issue Apr 6, 2017 · 12 comments

Comments

@emcsween
Copy link

emcsween commented Apr 6, 2017

Not sure if it's the near equal longitudes, but this call returns NaN:

geolib.getDistanceSimple({latitude: 43.647862 , longitude: -79.39290290000002}, {latitude: 43.647862, longitude: -79.392903})
@jimmykane
Copy link

Same here. @manuelbieh the docs say its always a number I have more than 1.000 samples that validate that it returns NAN.

@manuelbieh
Copy link
Owner

Oh, that's weird. I'll investigate on this.

@manuelbieh manuelbieh added the bug label Jun 27, 2017
@jimmykane
Copy link

jimmykane commented Jun 27, 2017

@manuelbieh thanks a lot for your time 👍 ! Nice library and project.

@manuelbieh
Copy link
Owner

This issue still exists and I have no clue why. I'll buy ice cream for whoever solves this.

@quocbaovu15101996
Copy link

I see it works normally. Tell me what wrong?

@projectpublius
Copy link

projectpublius commented May 31, 2019

This issue still exists and I have no clue why. I'll buy ice cream for whoever solves this.

I just ran into this myself and I think I figured it out. It's an issue with floating point arithmetic when attempting to call getDistance() on two points that are the same or very close to the same point.

I have an app that records the user's path travelled over time, and sometimes the location is fixed for a short period leading to getDistance() being called on two copies of the same coordinate pair. The expected behavior is that getDistance() should return 0, which it does in most situations. But for certain coordinate pairs, e.g. { latitude: 38.95826, longitude: -77.44167 }, the getDistance() formula

const distance =
        Math.acos(
            Math.sin(toRad(toLat)) * Math.sin(toRad(fromLat)) +
                Math.cos(toRad(toLat)) *
                    Math.cos(toRad(fromLat)) *
                    Math.cos(toRad(fromLon) - toRad(toLon))
        ) * earthRadius;

will evaluate to Math.acos(1.0000000000000002) instead of Math.acos(1) due to floating point arithmetic and will return NaN as a result.

This leads to getPathLength() also returning NaN if the array of points contains these coordinate pairs in sequence.

I think adding a Math.min(1,x) check to the distance formula should fix it:

const distance =
        Math.acos(Math.min(1,
            Math.sin(toRad(toLat)) * Math.sin(toRad(fromLat)) +
                Math.cos(toRad(toLat)) *
                    Math.cos(toRad(fromLat)) *
                    Math.cos(toRad(fromLon) - toRad(toLon))
        )) * earthRadius;

@jimmykane
Copy link

jimmykane commented May 31, 2019 via email

@MajcenT
Copy link

MajcenT commented Mar 6, 2020

Has anyone resolved the "getDistanceFromLine is returning NaN" issue? Are these two issues related?

@jimmykane
Copy link

jimmykane commented Mar 6, 2020 via email

@manuelbieh
Copy link
Owner

This should have been solved by adding a normalizeCosArg helper. Re-open if the error still occurs.

@casperkloppenburg
Copy link

casperkloppenburg commented Jun 6, 2020

I still encounter NaN values for these coordinates on [email protected]. Note that the supplied point is almost identical as the supplied lineStart.

Edit: Ah, I see you fixed this in 3.3.2. Geolib@latest on npm is still 3.3.1 though.

geolib.getDistanceFromLine({
  latitude: 53.0281161107639,
  longitude: 5.64420448614743,
}, {
  latitude: 53.028118,
  longitude: 5.644203,
}, {
  latitude: 53.029021,
  longitude: 5.646562,
})

geolib.getDistanceFromLine({
  latitude: 53.0515182362456,
  longitude: 5.67842625473533,
}, {
  latitude: 53.051521,
  longitude: 5.678421,
}, {
  latitude: 53.051652,
  longitude: 5.67852,
})

geolib.getDistanceFromLine({
  latitude: 53.0933224175307,
  longitude: 5.61011575344944,
}, {
  latitude: 53.093321,
  longitude: 5.610115,
}, {
  latitude: 53.093236,
  longitude: 5.610037,
})

geolib.getDistanceFromLine({
  latitude: 53.0867058030163,
  longitude: 5.59876618900706,
}, {
  latitude: 53.086705,
  longitude: 5.598759,
}, {
  latitude: 53.085538,
  longitude: 5.597901,
})

geolib.getDistanceFromLine({
  latitude: 53.0657207151762,
  longitude: 5.60056383087291,
}, {
  latitude: 53.065721,
  longitude: 5.600568,
}, {
  latitude: 53.062609,
  longitude: 5.600793,
})

@timvahlbrock
Copy link
Contributor

Encountered this issue with 3.3.4 as well. See #304 for faulty scenarios and fix. Fix found by @redcic75

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants