1

this is a follow up question that I think might help those who are wondering in details of Distance Computing: a follow up to this question : here The best answer gives us distance between 2 given point in Meter.

public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
               Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
               Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;

    int meterConversion = 1609;

    return (float) (dist * meterConversion);
}

but my question starts here:

  1. as commented under the chosen answer " The earth is not a perfect sphere, so this solution results in a slight error ". is this true? if so then how precise is it? ( in Meter )

  2. In solution like this, I see different inputs as the earth radius from 6360 to 6379 KM. is this gap really excusable in output ?

Edit: Let's Imagine our two points are both located within a 5 KM distance.

Community
  • 1
  • 1
Shervin
  • 233
  • 1
  • 13
  • 1
    This question appears to be off-topic because it is about geometry. – Oliver Charlesworth Jan 23 '14 at 20:17
  • @OliCharlesworth mmmm I Really didnt see it that way ! although I cant agree more with you on this , but ... I think Im not the only coder having this hassle. – Shervin Jan 23 '14 at 20:19
  • @OliCharlesworth This question provides a source code example and appears to be related specifically to the accuracy of the algorithm implemented in the source code example, for a specific type of map projection. It is related to geometry, but it would be considered on topic because it meets the criteria of "software algorithm" as mentioned at https://stackoverflow.com/help/on-topic – Michael J. Gray Jan 23 '14 at 20:20
  • @MichaelJ.Gray: But the questions (how accurate is the sphere model of the earth? + does this difference matter?) are not programming questions. – Oliver Charlesworth Jan 23 '14 at 20:22
  • @OliCharlesworth I didnt ask about the mathematical shape of the erath. I asked if the given statement is true. how precise is this solution. – Shervin Jan 23 '14 at 20:25
  • @OliCharlesworth They are asking the accuracy of this particular software implementation of the haversine formula. – Michael J. Gray Jan 23 '14 at 20:26
  • IMHO, for a GPS application, you don't really care if the earth is spheric. You will probably never do more than 1000kms. The error is acceptable. – Arnaud Denoyelle Jan 23 '14 at 20:27
  • @ArnaudDenoyelle I would argue that many GPS devices do care if the Earth is a sphere. This is because many GPS devices have integrated route finding for high precision maritime use and all of them need to have less than 2.5% deviation from a land survey. – Michael J. Gray Jan 23 '14 at 20:33

3 Answers3

3

(1) We can't really tell you the accuracy in meters, because it depends where on Earth the two points you're computing the distance between lie - e.g., for some formulas, accuracy is decent unless the points are approximately opposite eachother on the Earth, in which case accuracy diminishes quickly.

(Note: according to this, "So as long as we're assuming a spherical Earth, any single formula for distance on the Earth is only guaranteed correct within 0.5%" - this gives you an idea of how much error is involved.)

For the haversine formula, according to both Wikipedia and this page (which has a ton of useful information), the margin of error you're looking at is up to 0.55%, but depending on latitude it is typically as low as 0.3%. This means that, for two points 5 KM apart, the error should be approximately 15 M (again, depending on latitude and how good your approximation of the Earth's radius is).

(2) Whether or not the level of error you have is acceptable is entirely dependant on what you're doing with this code. It might be acceptable, or it might not; it depends.

For more information, read this page. There are a lot of ways to do this, with varying degrees of accuracy, and Wikipedia has a pretty good description of quite a few of them.

CmdrMoozy
  • 3,870
  • 3
  • 19
  • 31
  • 1
    You beat me to it. +1 because you mentioned that the haversine formula isn't the only thing you can use and the way you explained #2. It's perfectly acceptable for some applications and maybe not for others is something I believe many would have missed in an answer. – Michael J. Gray Jan 23 '14 at 20:25
  • I added some information for your specific case. Note that the margin of error I'm quoting is approximate, and is certainly not true for *any* two points 5KM apart, but it should give you a general idea of accuracy. – CmdrMoozy Jan 23 '14 at 20:36
1

One thing in the code you posted is not very "stylish":

It defines the earth radius in miles, and later converts to meter.
This is not a good idea. Use meters, this is the corect SI unit. And it avoids one multiplication.

The exakt radius to use: Has not much influence (try it out). But correct would be to use the radius of the WGS84 reference ellipsoid (6378137 meter), because WGS84 most probably is the coordinate reference system your coordinates are related (from GPS on any (smart-)device).

Althought the posted formula was considered best in the post (Greater Circle), it is not (always). Typical application calculate small distances, like your 5km. For that the formula posted is not well suited, because it uses cos() of a small value which is numerically ill-conditioned.

For small distances the haversine is better suited, it avoids the cos() operation. But it is a bit slower, because it uses a sqrt() which is very slow.

About accuracy:
In typical applications there is no true real distance (e.g a road goes up, and down, on a 3d earth surface). So this has more influence than the accuracy of the said formulas.

So, yes for most application the accuracy is acceptable.

If you want it more precise, you can use Vicency's formula. It treats the earth as an ellipsoid and not as a sphere.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
1

For an explanation of the problem on an ellipsoid, see the Wikipedia article Geodesics on an ellipsoid. Java code to compute distances accurately (within 15 nanometers) on the ellipsoid is available as part of GeographicLib.

cffk
  • 1,839
  • 1
  • 12
  • 17