Last active
September 22, 2016 15:43
-
-
Save kgundula/c30113eeb37d8580a3e1 to your computer and use it in GitHub Desktop.
Calculate distance between two sets lat/lon coordinates and Calculate destination lat/lon given a starting point lat/lon cordinates, bearing, and distance, and Calculate the bounding box
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
// Calculating the bounding box | |
function boundbox ($lat, $lon, $distance, $units="km") { | |
return array ( "N" => getDestinationCoordinates ($lat,$lon, 0,$distance,$units), | |
"E" => getDestinationCoordinates ($lat,$lon, 90,$distance,$units), | |
"S" => getDestinationCoordinates ($lat,$lon, 180,$distance,$units), | |
"W" => getDestinationCoordinates ($lat,$lon, 270,$distance,$units)); | |
} |
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
// calculate destination lat/lon given a starting point, bearing, and distance | |
function getDestinationCoordinates ( $startlat, $startlon, $bearing, $distance , $units="km" ) | |
{ | |
$radius = strcasecmp($units, "km") ? 3963.19 : 6378.137; | |
$rLat = deg2rad($startlat); | |
$rLon = deg2rad($startlon); | |
$rBearing = deg2rad($bearing); | |
$rAngDist = $distance / $radius; | |
$rLatB = asin(sin($rLat) * cos($rAngDist) + cos($rLat) * sin($rAngDist) * cos($rBearing)); | |
$rLonB = $rLon + atan2(sin($rBearing) * sin($rAngDist) * cos($rLat), cos($rAngDist) - sin($rLat) * sin($rLatB)); | |
return array("lat" => rad2deg($rLatB), "lon" => rad2deg($rLonB)); | |
} |
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
// calculate distance between two lat/lon coordinates | |
function getDistanceBetweenCoordinates ( $latA, $lonA , $latB, $lonB, $units="km" ) | |
{ | |
$radius = strcasecmp($units, "km") ? 3963.19 : 6378.137; | |
$rLatA = deg2rad($latA); | |
$rLatB = deg2rad($latB); | |
$rHalfDeltaLat = deg2rad(($latB - $latA) / 2); | |
$rHalfDeltaLon = deg2rad(($lonB - $lonA) / 2); | |
return 2 * $radius * asin(sqrt(pow(sin($rHalfDeltaLat), 2) + cos($rLatA) * cos($rLatB) * pow(sin($rHalfDeltaLon), 2))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment