2

I have a strange problem using the Cosine formula in my PHP application..

function CalculateDistanceCosine($decA, $decB)
{
    $lon1 = $decA[0]; //This would be equal to point A's longitude, and so on..
    $lat1 = $decA[1]; 
    $lon2 = $decB[0];   
    $lat2 = $decB[1];

    //echo $lon1." ".$lat1."<br/>";
    //echo $lon2." ".$lat2."<br/>";

    $distance  = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon2-$lon1));
    $distance  = acos($distance);
    $distance  = rad2deg($distance);
    $distance  = $distance * 60 * 1.1515;
    $distance  = round($distance, 4);

    return $distance;   
}

My input into this would be something like this:

45.468055555556 -73.741388888889 //- The coordinates for Montreal International Airport
28.428888888889 -81.315833333333 //- Orlando International Airport

However, upon using it, I get wild mistakes.. i.e, "The distance from Montreal to Orlando is 576 KM -- very wrong."

What's interesting is that it is very accurate along the longitude axis. For example, if I gave an input of:

50 -73.741388888889 
50 -81.315833333333 

The error is now only about 50KM, very acceptable.

In other words, why is it neglecting latitudinal differences?

I've tried the Harvesine formula with similar results unfortunately.

Kara
  • 5,650
  • 15
  • 48
  • 55
matrices
  • 33
  • 4

2 Answers2

0

Did you took a look here: Using the Cosine law to calculate distance between 2 points in Objective C?? After the distance calculation with the cosine law the distance doesn't get convert back to degree and it is multiply with the earth radius. IMO you forgot the earth radius.

Community
  • 1
  • 1
Gigamegs
  • 12,342
  • 7
  • 31
  • 71
0

I believe your main problem is that you're getting latitude confused with longitude. You've not shown us the code that's wrong, but bear in mind that in your arrays, you would need the longitude first, followed by the latitude, for your function to work. In the example you've given, you've listed the latitude first, followed by the longitude.

Your code would certainly work out the distance between those two points as written as 576 (in units I'll mention in a minute), but those points aren't actually where you think they are :) Try changing your function as follows:

$lat1 = $decA[0]; //This would be equal to point A's *latitude*, and so on..
$lon1 = $decA[1]; 
$lat2 = $decB[0];   
$lon2 = $decB[1];

...or just pass in the values in the expected order.

Also, I don't recognise your distance multiplier, but it looks like you might be calculating your result in miles, not kilometres. For kilometres, try:

$distance  = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon2-$lon1));
$distance  = acos($distance);
$distance  = $distance * 6372.8;
$distance  = round($distance, 4);

(It's only skipping the conversion back to degrees and the multiplier that's changed, the basic formula remains the same.)

Given the above changes, your distance works out at around 2,008 kilometres. Is that about right? I'm afraid I've never visited Montreal, Orlando, or anywhere in between...

Matt Gibson
  • 36,429
  • 9
  • 89
  • 124
  • This is it exactly. Sigh, the simple things sometimes. Thanks a lot. – matrices Mar 09 '12 at 00:05
  • It's also better readable with list($lat1,$lon1)=explode("", $decA); – Gigamegs Mar 09 '12 at 00:07
  • @user1256214 No problem. It's not an unusual mistake when you're dealing with latitude and longitude, as they're traditionally written the "other way around", conceptually, from computer co-ordinate systems, and even then there are some standards that have them backwards from normal. – Matt Gibson Mar 09 '12 at 00:10
  • @David What does the odd explode() with the empty string give you? Over, for example, `list($lat1, $lon1) = $decA`? I just tried your code and it didn't seem to work (which seems to gel with the explode() manual entry, which says it returns FALSE if the delimiter is an empty string...) – Matt Gibson Mar 09 '12 at 00:17
  • @MattGibson: You don't need explode? I didn't know... it's been a while I used php. My apologoize. – Gigamegs Mar 09 '12 at 00:24