我正在寻找一个 PHP 库/PHP 脚本,它允许我计算给定中心点(纬度/经度)的准确边界框。
使用椭球公式(例如 WGS84)会很棒。我知道,必须有一个图书馆,但我找不到。
//轴承位后函数错误,应该如下:
function getBoundingBox($lat_degrees,$lon_degrees,$distance_in_miles) {
$radius = 3963.1; // of earth in miles
// bearings - FIX
$due_north = deg2rad(0);
$due_south = deg2rad(180);
$due_east = deg2rad(90);
$due_west = deg2rad(270);
// convert latitude and longitude into radians
$lat_r = deg2rad($lat_degrees);
$lon_r = deg2rad($lon_degrees);
// find the northmost, southmost, eastmost and westmost corners $distance_in_miles away
// original formula from
// http://www.movable-type.co.uk/scripts/latlong.html
$northmost = asin(sin($lat_r) * cos($distance_in_miles/$radius) + cos($lat_r) * sin ($distance_in_miles/$radius) * cos($due_north));
$southmost = asin(sin($lat_r) * cos($distance_in_miles/$radius) + cos($lat_r) * sin ($distance_in_miles/$radius) * cos($due_south));
$eastmost = $lon_r + atan2(sin($due_east)*sin($distance_in_miles/$radius)*cos($lat_r),cos($distance_in_miles/$radius)-sin($lat_r)*sin($lat_r));
$westmost = $lon_r + atan2(sin($due_west)*sin($distance_in_miles/$radius)*cos($lat_r),cos($distance_in_miles/$radius)-sin($lat_r)*sin($lat_r));
$northmost = rad2deg($northmost);
$southmost = rad2deg($southmost);
$eastmost = rad2deg($eastmost);
$westmost = rad2deg($westmost);
// sort the lat and long so that we can use them for a between query
if ($northmost > $southmost) {
$lat1 = $southmost;
$lat2 = $northmost;
} else {
$lat1 = $northmost;
$lat2 = $southmost;
}
if ($eastmost > $westmost) {
$lon1 = $westmost;
$lon2 = $eastmost;
} else {
$lon1 = $eastmost;
$lon2 = $westmost;
}
return array($lat1,$lat2,$lon1,$lon2);
}
我得到了这个功能感谢:http: //xoxco.com/clickable/php-getboundingbox
如果您假设边界框在一个方向上是东西走向,而在另一个方向上是南北走向,那么这是一个相对容易解决的问题。你可以独立做纬度和经度。
对于纬度,将点从西向东排序。此时,您必须将列表视为循环缓冲区。您需要测试每个点并找到下一个点最远的点。所以假设十个点 a 0到 a 9,如果 a 4和 a 5最远,则纬度边界框是从5到4。称他们为w和 a e
对于经度,您只需要找到最北端和最南端,称它们为 a n和 a s。
a w和 a e的经度以及 a n和 a s 的纬度定义了边界框。