Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Library: Calculate a bounding box for a given lat/lng location

I'm looking for a PHP Library / PHP Script that allows me to calculate an accurate bounding box for a given center point (lat/lon).

Using an ellipsoid formula (f.ex. WGS84) would be great. I know, there have to be a library but I'm not able to find one.

like image 410
brain Avatar asked Jan 22 '23 06:01

brain


2 Answers

Function is wrong after the // bearings bit, it should be as follows:

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);
}

I got this function thanks to: http://xoxco.com/clickable/php-getboundingbox

like image 88
susheel Avatar answered Feb 07 '23 08:02

susheel


This is a relative easy problem to solve if you assume the bounding box runs east-west in one direction and north-south in the other. You can do latitude and longitude independently.

For latitude, sort the points west to east. At that point you have to treat the list as a circular buffer. You need to test each point and find the one with the most distant next point. So assume ten points a0 to a9, if a4 and a5 are most distant the latitude bounding box is from a5 round to a4. Call them aw and ae

For longitude, you just need to find the northenmost and southernmost points, call them an and as.

The longitudes of aw and ae and latitudes of an and as define the bounding box.

like image 38
cletus Avatar answered Feb 07 '23 08:02

cletus