Created
July 20, 2015 08:32
-
-
Save ed-flanagan/a3a626be0c4ff2069a43 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| /* | |
| * Great-circle distance computational forumlas | |
| * | |
| * https://en.wikipedia.org/wiki/Great-circle_distance | |
| */ | |
| $earth_radius_km = 6371; | |
| function haversineDistance($latitude1, $longitude1, $latitude2, $longitude2) { | |
| $lat1 = deg2rad($latitude1); | |
| $lng1 = deg2rad($longitude1); | |
| $lat2 = deg2rad($latitude2); | |
| $lng2 = deg2rad($longitude2); | |
| $d_lat = abs($lat1 - $lat2); | |
| $d_lng = abs($lng1 - $lng2); | |
| $a = pow(sin($d_lat / 2), 2) + cos($lat1) * cos($lat2) | |
| * pow(sin($d_lng / 2), 2); | |
| $d_sigma = 2 * asin(sqrt($a)); | |
| global $earth_radius_km; | |
| return($earth_radius_km * $d_sigma); | |
| } | |
| function vincentyDistance($latitude1, $longitude1, $latitude2, $longitude2) { | |
| $lat1 = deg2rad($latitude1); | |
| $lng1 = deg2rad($longitude1); | |
| $lat2 = deg2rad($latitude2); | |
| $lng2 = deg2rad($longitude2); | |
| $d_lng = abs($lng1 - $lng2); | |
| // Numerator | |
| $a = pow(cos($lat2) * sin($d_lng), 2); | |
| $b = cos($lat1) * sin($lat2); | |
| $c = sin($lat1) * cos($lat2) * cos($d_lng); | |
| $d = pow($b - $c, 2); | |
| $e = sqrt($a + $d); | |
| // Denominator | |
| $f = sin($lat1) * sin($lat2); | |
| $g = cos($lat1) * cos($lat2) * cos($d_lng); | |
| $h = $f + $g; | |
| $d_sigma = atan2($e, $h); | |
| global $earth_radius_km; | |
| return($earth_radius_km * $d_sigma); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment