Long / Lat distance calculations???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrskumm
Forum Newbie
Posts: 10
Joined: Tue Jul 22, 2003 5:40 am
Location: Sweden

Long / Lat distance calculations???

Post by mrskumm »

Hello!

I am trying to calculate the distance between two Long/Lat places...

I use the following code, but it won't give me the correct result, anyone know of what I have done wrong?

I have based this on this formula http://jan.ucc.nau.edu/~cvm/latlon_formula.html
(The working calculator on that page is on http://jan.ucc.nau.edu/~cvm/latlongdist.html)

Code: Select all

// Define variables for the calculation
	$dist = 0;			// Distance between source/destination
	$rad_km = 6378;		// Earth Radius in kilometers
	$rad_smi = 3963.1;	// Earth Radius in statue miles
	$rad_nmi = 3443.9;	// Earth Radius in nautical miles

/*
Source Latitude: 373700N -- 37.6166666667
Source Longitude: 1222200W -- 122.366666667
Destination Latitude: 484400N -- 48.7333333333
Destination Longitude: 022300E -- 2.38333333333
*/
	$slat_deg = 37;
	$slat_min = 37;
	$slat_sec = 0;
	$slat_ns = "N";
	$slon_deg = 122;
	$slon_min = 22;
	$slon_sec = 0;
	$slon_we = "W";
	$dlat_deg = 48;
	$dlat_min = 44;
	$dlat_sec = 0;
	$dlat_ns = "N";
	$dlon_deg = 2;
	$dlon_min = 23;
	$dlon_sec = 0;
	$dlon_we = "E";

	$slat_dec = $slat_deg + ( ( $slat_min + ( $slat_sec / 60 ) ) / 60 );
	$slon_dec = $slon_deg + ( ( $slon_min + ( $slon_sec / 60 ) ) / 60 );
	$dlat_dec = $dlat_deg + ( ( $dlat_min + ( $dlat_sec / 60 ) ) / 60 );
	$dlon_dec = $dlon_deg + ( ( $dlon_min + ( $dlon_sec / 60 ) ) / 60 );
	if( $slat_ns == "S" OR $slat_ns == "s" ) {
		$slat_dec = $slat_dec * -1;
	}
	if( $dlat_ns == "S" OR $dlat_ns == "s" ) {
		$dlat_dec = $dlat_dec * -1;
	}
	if( $slon_we == "W" OR $slon_we == "w" ) {
		$slon_dec = $slon_dec * -1;
	}
	if( $dlon_we == "W" OR $dlon_we == "w" ) {
		$dlon_dec = $dlon_dec * -1;
	}
	$a1 = deg2rad( $slat_dec );
	$a2 = deg2rad( $slon_dec );
	$b1 = deg2rad( $dlat_dec );
	$b2 = deg2rad( $dlon_dec );	// I had an error here, but it's still wrong?!
	$r = $rad_km;

	$dist = acos(cos($a1)*cos($b1)*cos($a2)*cos($b2) + cos($a1)*sin($b1)*cos($a2)*sin($b2) + sin($a1)*sin($a2)) * $r;

	echo "<p>Distance: $dist</p>";
It should give something like 8987 km but I get 6324 km?!

Okay, I noticed an error, but now I get 16021 km!?!?

Any helpers ?? Thanks a bunch!

//Lorens
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

I looked over your code and the formula page you pointed to. I entered the values your code seems to be using and got your expected results. So I think I would look into your degree calculations. Just before you convert the values to radians, display the degrees you came up with and see if they are what you expect them to be.

You might also try hardcoding the expected values into your variables and seeing if your formula is working as expected. This should help break the problem apart.
mrskumm
Forum Newbie
Posts: 10
Joined: Tue Jul 22, 2003 5:40 am
Location: Sweden

Post by mrskumm »

I have fixed so the values are printed, the first row of values are the decimal values, and they are as expected...

The second row are after I have converted them to radians with php function deg2rad()... And then my calculated distance (16000km) and the value it should be (8900km)... I will look into this further to try and find what I might have done wrong!

Thanks anyway for your help...
slat: 37.6166666667, slon: -122.366666667, dlat: 48.7333333333, dlon: 2.38333333333


slat (rad): 0.656534686959, slon (rad): -2.13570122802, dlat (rad): 0.850557122139, dlon (rad): 0.0415970138392


Distance (Rad): 16021.2979554


Distance 2 (Rad): 16021.2979554


Expected distance: 8987.2353
My new code is as follows:

Code: Select all

// Define variables for the calculation
	$slat = 0;			// Source Latitude
	$slon = 0;			// Source Longitude
	$dlat = 0;			// Destination Latitude
	$dlon = 0;			// Destination Longitude
	$dist = 0;			// Distance between source/destination
	$rad_km = 6378;		// Earth Radius in kilometers
	$rad_smi = 3963.1;	// Earth Radius in statue miles
	$rad_nmi = 3443.9;	// Earth Radius in nautical miles

	// CONVERT LAT/LONG TO RADIANS!!!!!!!!!!!!!!!!!!!!!!!!!!!1
/*
Source Latitude: 373700N -- 37.6166666667
Source Longitude: 1222200W -- 122.366666667
Destination Latitude: 484400N -- 48.7333333333
Destination Longitude: 022300E -- 2.38333333333
*/
	$slat_deg = 37;
	$slat_min = 37;
	$slat_sec = 0;
	$slat_ns = "N";
	$slon_deg = 122;
	$slon_min = 22;
	$slon_sec = 0;
	$slon_we = "W";
	$dlat_deg = 48;
	$dlat_min = 44;
	$dlat_sec = 0;
	$dlat_ns = "N";
	$dlon_deg = 2;
	$dlon_min = 23;
	$dlon_sec = 0;
	$dlon_we = "E";

	$slat_dec = $slat_deg + ( ( $slat_min + ( $slat_sec / 60 ) ) / 60 );
	$slon_dec = $slon_deg + ( ( $slon_min + ( $slon_sec / 60 ) ) / 60 );
	$dlat_dec = $dlat_deg + ( ( $dlat_min + ( $dlat_sec / 60 ) ) / 60 );
	$dlon_dec = $dlon_deg + ( ( $dlon_min + ( $dlon_sec / 60 ) ) / 60 );
	if( $slat_ns == "S" OR $slat_ns == "s" ) &#123;
		$slat_dec = $slat_dec * -1;
	&#125;
	if( $dlat_ns == "S" OR $dlat_ns == "s" ) &#123;
		$dlat_dec = $dlat_dec * -1;
	&#125;
	if( $slon_we == "W" OR $slon_we == "w" ) &#123;
		$slon_dec = $slon_dec * -1;
	&#125;
	if( $dlon_we == "W" OR $dlon_we == "w" ) &#123;
		$dlon_dec = $dlon_dec * -1;
	&#125;
	$slat_rad = deg2rad( $slat_dec );
	$slon_rad = deg2rad( $slon_dec );
	$dlat_rad = deg2rad( $dlat_dec );
	$dlon_rad = deg2rad( $dlon_dec );/**/

	echo "<p>slat: $slat_dec, slon: $slon_dec,".
		" dlat: $dlat_dec, dlon: $dlon_dec</p>";
	echo "<p>slat (rad): $slat_rad, slon (rad): $slon_rad,".
		" dlat (rad): $dlat_rad, dlon (rad): $dlon_rad</p>";

	$dist = acos(cos($slat_rad)*cos($dlat_rad)*cos($slon_rad)*cos($dlon_rad) + cos($slat_rad)*sin($dlat_rad)*cos($slon_rad)*sin($dlon_rad) + sin($slat_rad)*sin($slon_rad)) * $rad_km;
	echo "<p>Distance (Rad): $dist</p>";

	//	Arccos&#1111;
	//		Cos&#1111;a1] Cos&#1111;b1] Cos&#1111;a2] Cos&#1111;b2] +
	//		Cos&#1111;a1] Sin&#1111;b1] Cos&#1111;a2] Sin&#1111;b2] +
	//		Sin&#1111;a1] Sin&#1111;a2]
	//	] * r
	$dist2 = acos(
		( cos( $slat_rad ) * cos( $dlat_rad ) * cos( $slon_rad ) * cos( $dlon_rad ) ) +
		( cos( $slat_rad ) * sin( $dlat_rad ) * cos( $slon_rad ) * sin( $dlon_rad ) ) +
		( sin( $slat_rad ) * sin( $slon_rad ) )
		) * $rad_km;
	echo "<p>Distance 2 (Rad): $dist2</p>";

	echo "<p>Expected distance: 8987.2353</p>";
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

HotScripts has a script that does this calculation, or at least claims to. It might be worth your time to download it and compare it to yours.

http://www.hotscripts.com/Detailed/20792.html

Hope it helps!
mrskumm
Forum Newbie
Posts: 10
Joined: Tue Jul 22, 2003 5:40 am
Location: Sweden

Post by mrskumm »

Thank you, this code works as expected :)
Post Reply