mjaschen / phpgeo

Simple Yet Powerful Geo Library for PHP
https://phpgeo.marcusjaschen.de
MIT License
1.56k stars 195 forks source link

Point-to-line distance is incorrect sometimes #92

Closed magnuswikhog closed 2 years ago

magnuswikhog commented 2 years ago

Version of phpgeo: 4.1.0

$ll1 = array (
    'lat' => 55.98467000751469413444283418357372283935546875,
    'lon' => 13.544430000324179985682349069975316524505615234375,
);
$ll2 = array (
    'lat' => 55.98461833972562118333371472544968128204345703125,
    'lon' => 13.54429500218709137016048771329224109649658203125,
);
$p = array (
    'lat' => 55.97609299999999876717993174679577350616455078125,
    'lon' => 13.5475989999999999469082467840053141117095947265625,
);

$dist = $pointToLineDistanceCalculator->getDistance(
    new Coordinate($p['lat'], $p['lon']), 
    new Line(
        new Coordinate($ll1['lat'], $ll1['lon']), 
        new Coordinate($ll2['lat'], $ll2['lon'])
    )
);

Result

898.46032179829 meters

But multiple other tools and formulas give a different result, around 971 meters, differing almost 100 meters!

I've seen similar differences on other line segments and coordinates too, but the above one is a really big difference. Any ideas what's going on here? Which one is correct??

mjaschen commented 2 years ago

But multiple other tools and formulas give a different result

I'd like to investigate this – can you provide one or two examples?

magnuswikhog commented 2 years ago

Two of the online tools I tried: https://www.omnicalculator.com/other/latitude-longitude-distance https://rl.se/avstand

I just noticed something strange - if I reduce the number of decimals on $ll2 to 5, I get 970.532, which is near the 971 I've got from other tools and formulas.

mjaschen commented 2 years ago

Ok, thanks! I'll setup some test cases and keep you updated.

magnuswikhog commented 2 years ago

Thanks for the exteremely quick reply!

I should mention that the long floating point numbers in the first post come from var_export of some actual coordinates, which is why they are so long.

mjaschen commented 2 years ago

Version 4.2.0 should fix this issue. Can you confirm this?

magnuswikhog commented 2 years ago

Now it looks okay, thanks for the quick fix!