mjaschen / phpgeo

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

Problem with calculating distance using Vincenty #107

Open bjoernffm opened 3 months ago

bjoernffm commented 3 months ago

Hi there,

I try to calculate the distance between two points using this code:

$coordinate1 = new Coordinate(-5.59248, -78.774002);
$coordinate2 = new Coordinate(5.79, 101.15);

$distance = $coordinate1->getDistance($coordinate2, new Vincenty());

It throws an NotConvergingException and I don't understand why.

Is there any solution to this problem or is the vincenty method limited regarding any circumstances?

Thank you!

mjaschen commented 3 months ago

Hello,

as described here, the Vincenty formular can fail to converge in some cases:

Vincenty’s inverse solution can fail on nearly antipodal points. Testing with Geographic­Lib test data, I’ve found this can happen with distances greater than 19,936 km, or within around 75 km of the antipodal point.

I've added a test case and will update the documentation.

As a workaround you can wrap the getDistance() method call in try {} catch {} and recalculate with Haversine in the case that Vincenty fails.