PHP库:计算给定lat / lng位置的边界框

时间:2010-04-13 08:28:53

标签: php wgs84 bounding

我正在寻找一个PHP库/ PHP脚本,它允许我为给定的中心点(纬度/经度)计算精确的边界框。

使用椭圆体公式(f.ex. WGS84)会很棒。我知道,必须有一个图书馆,但我找不到。

2 个答案:

答案 0 :(得分:7)

//轴承位后的功能是错误的,应该如下:

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

答案 1 :(得分:1)

如果您假设边界框在一个方向上向东西方向移动而在另一个方向上向南北方向延伸,则这是一个相对容易解决的问题。你可以独立做经度和纬度。

对于纬度,从西向东排序点。此时,您必须将列表视为循环缓冲区。您需要测试每个点并找到下一个点最远的点。假设 0 0 ,如果 4 5 与纬度边界最远,则假设10个点 0 框是从 5 轮到 4 。称他们为 w e

对于经度,你只需要找到最北端和最南端的点,称它们为 n s

w e 的经度以及 n s 的纬度定义了边界框。