问题
I have a number of points which make getOrthodromicDistance method to fail with exception in geotools lib, while these points are valid lat lon points:
Point which throws the exception (lat,lon):
val p1= (5.318765,-75.786109)
val p2= (-6.32907,106.09254)
eg exception: No convergence for points 75°47,2'W 06°19,7'S and 106°05,6'E 05°19,1'N. java.lang.ArithmeticException: No convergence for points 75°47,2'W 06°19,7'S and 106°05,6'E 05°19,1'N. at org.geotools.referencing.GeodeticCalculator.computeDirection(GeodeticCalculator.java:1073)
Code used in Scala:
def latlonDistance(p1:(Double,Double), p2:(Double,Double)):Double={
val world= new GeodeticCalculator()
world.setStartingGeographicPoint(p1._2, p2._1)
world.setDestinationGeographicPoint(p2._2, p1._1)
world.getOrthodromicDistance
}
Note: My point format passed in latlonDistance is (lat,lon) as mentioned above while, setStartingGeographicPoint, setDestinationGeographicPoint need (lon,lat) order.
Version used:
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>13.2</version>
</dependency>
In python works as expected:
>>> from geopy.distance import vincenty
>>> pt1= [5.318765,-75.786109]
>>> pt2= [-6.32907,106.09254]
>>> vincenty(pt1 , pt2)
Distance(19791.6883647)
It is the orthodromicDistance method in org.geotools.referencing.datum.DefaultEllipsoid which does not converge. Any workarounds?
回答1:
The problem is that this is not a straightforward calculation as the Vincenty algorithm is an iterative process and some sets of points don't necessarily converge (within the limit set).
There are two possible solutions 1 - edit the GeodeticCalculator to increase the number of possible iterations from 12 to 15, this works in this case but I can't guarantee it in others. Or 2 use another algorithm, following links from this question's answers I found the GeographicLib library at Sourceforge and used that instead with your points. It is written by the author (@cffk) of the paper linked to in the other answer.
For your points it gives a very reasonable looking 20004 Km.
来源:https://stackoverflow.com/questions/31964589/geotools-distance-calculation-fails-with-no-convergence-exception-for-several-la