Created
July 25, 2014 06:56
-
-
Save kafene/b7406c0a3cafaa91a371 to your computer and use it in GitHub Desktop.
distance calculations using Spherical Law of Cosines and Haversine Formula
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Spherical Law of Cosines | |
function distance_slc($lat1, $lon1, $lat2, $lon2) { | |
$a = sin(deg2rad($lat1)) * sin(deg2rad($lat2)); | |
$b = cos(deg2rad($lat1)) * cos(deg2rad($lat2)); | |
$c = cos(deg2rad($lon2 - $lon1)); | |
$distance = rad2deg(acos($a + $b * $c)); | |
return round($distance * 60 * 1.1515, 4); | |
} | |
# Haversine Formula | |
function distance_haversine($lat1, $lon1, $lat2, $lon2) { | |
$a = pow(sin(deg2rad(($lat2 - $lat1) / 2)), 2); | |
$b = pow(cos(deg2rad($lat1)), 2); | |
$c = pow(sin(deg2rad(($lon2 - $lon1) / 2)), 2); | |
$d = asin(min(1, sqrt($a + $b * $c))); | |
return round(2 * 3960.00 * $d, 4); | |
} | |
$lat_1 = "47.117828"; | |
$lon_1 = "-88.545625"; | |
$lat_2 = "47.122223"; | |
$lon_2 = "-88.568781"; | |
$slc_distance = distance_slc($lat_1, $lon_1, $lat_2, $lon_2); | |
$hav_distance = distance_haversine($lat_1, $lon_1, $lat_2, $lon_2); | |
var_dump($slc_distance, $hav_distance); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment