0

I use algorithm (see: calculating distance between two points) to calculate distance between two points. but it get wrong result, see Java code:

/**
 * group1:set latitude to 89
 */
// set longitude1 = 0, longitude2 = 1
System.out.println(distance(89, 89, 0, 1, 0 , 0)); // 1941.0            result1

// set longitude1 = 0, longitude2 = 2
System.out.println(distance(89, 89, 0, 2, 0 , 0)); // 3881.0            result2

// set longitude1 = 0, longitude2 = 179
System.out.println(distance(89, 89, 0, 179, 0 , 0)); // 222381.0        result3

// set longitude1 = 0, longitude2 = 180
System.out.println(distance(89, 89, 0, 180, 0 , 0)); // 222390.0        result4

/**
 * group2:set latitude to 0
 */
// set longitude1 = 0, longitude2 = 1
System.out.println(distance(0, 0, 0, 1, 0 , 0)); // 111195.0            result5

// set longitude1 = 0, longitude2 = 2
System.out.println(distance(0, 0, 0, 2, 0 , 0)); // 222390.0            result6

// set longitude1 = 0, longitude2 = 179
System.out.println(distance(0, 0, 0, 179, 0 , 0)); // 19903892          result7

// set longitude1 = 0, longitude2 = 180
System.out.println(distance(0, 0, 0, 180, 0 , 0)); // 20015087          result8

in group1:

a = result2 - result1 = 1940

b = result4 - result3 = 9

in group2:

c = result6 - result5 = 111195

d = result8 - result7 = 111195

It's very strange. c = d, group2 is right, but a ≠ b, group1 is wrong.

How do I solve this issue? Is there other higher accuracy algorithm for calculate distance between two coordinate points

Guo
  • 1,761
  • 2
  • 22
  • 45

1 Answers1

2

Well, the results are approximation - if you want higher precision, you would need to use calculations on spheroid, rather than calculations on sphere that the formula uses.

But I don't think this matter at all here. The results you get are correct with a good precision, I think the problem is not precision of the results, but the expectations you have about these distances.

These are geodesic distances - shortest distances on sphere, so the intuition about distances on flat map does not always work here. You might visualize geodesics at e.g. https://academo.org/demos/geodesics/ to get better intuition.

First, result2 and result1 vs. result6 and result5. As you might have noticed, result6 is exactly double of result5, but result2 is slightly less than result1. Why is this? Because geodesic from (89, 0) to (89, 2) does not pass through (89, 1), but it goes slightly further to the north.

Geodesic from (89, 0) to (89, 180) goes through North pole - so it is very different from 180 times of geodesic from (89, 0) to (89, 1).

On the other hand, with latitude 0 the geodesics always follow equator. Or in case of antipodal ends 0, 0 and 0, 180 - all geodesics have the same length. So your expectations about distance relations are correct in this special case.

Michael Entin
  • 7,189
  • 3
  • 21
  • 26