Created
September 6, 2016 09:05
-
-
Save braicauc/c4610fb5ade35ab46d3ec5e68e5a51a9 to your computer and use it in GitHub Desktop.
Get a center latitude,longitude from an array of like geopoints
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
/** | |
* Get a center latitude,longitude from an array of like geopoints | |
* | |
* @param array data 2 dimensional array of latitudes and longitudes | |
* For Example: | |
* $data = array | |
* ( | |
* 0 = > array(45.849382, 76.322333), | |
* 1 = > array(45.843543, 75.324143), | |
* 2 = > array(45.765744, 76.543223), | |
* 3 = > array(45.784234, 74.542335) | |
* ); | |
*/ | |
function GetCenterFromDegrees($data) | |
{ | |
if (!is_array($data)) return FALSE; | |
$num_coords = count($data); | |
$X = 0.0; | |
$Y = 0.0; | |
$Z = 0.0; | |
foreach ($data as $coord) | |
{ | |
$lat = $coord[0] * pi() / 180; | |
$lon = $coord[1] * pi() / 180; | |
$a = cos($lat) * cos($lon); | |
$b = cos($lat) * sin($lon); | |
$c = sin($lat); | |
$X += $a; | |
$Y += $b; | |
$Z += $c; | |
} | |
$X /= $num_coords; | |
$Y /= $num_coords; | |
$Z /= $num_coords; | |
$lon = atan2($Y, $X); | |
$hyp = sqrt($X * $X + $Y * $Y); | |
$lat = atan2($Z, $hyp); | |
return array($lat * 180 / pi(), $lon * 180 / pi()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment