【发布时间】:2011-02-07 08:30:07
【问题描述】:
我正在寻找一个 PHP 库/PHP 脚本,它允许我计算给定中心点(纬度/经度)的准确边界框。
使用椭球公式(例如 WGS84)会很棒。我知道,必须有一个图书馆,但我找不到。
【问题讨论】:
我正在寻找一个 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);
}
【讨论】:
如果您假设边界框在一个方向上为东西方向,而在另一个方向上为南北方向,则这是一个相对容易解决的问题。你可以独立做纬度和经度。
对于纬度,将点从西向东排序。此时,您必须将列表视为循环缓冲区。您需要测试每个点并找到下一个点最远的点。所以假设 a0 到 a9 有十个点,如果 a4 和 a5 距离纬度边界最远盒子是从一个5轮到一个4。称它们为 aw 和 ae
对于经度,你只需要找到最北端和最南端,分别称它们为an和as。
aw 和 ae 的经度和 an 和 as 的纬度定义了边界盒子。
【讨论】: