Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a lat lon within a bounding box?

I have the top left lat/lon and bottom right lat/lon for my bounding box.

How do I determine whether a given lat/lon is within that bounding box?

Here is the bounding box I'm working with: Top left lat: 51.780586 Top left lon: -0.727844 Bottom right lat: 51.260196 Bottom right lon: 0.590515

My point is: Lat: 51.508039 Lon: -0.128069

I'm going round circles with this at the moment, any help would be appreciated.

like image 711
user1283225 Avatar asked Dec 15 '22 02:12

user1283225


1 Answers

From your description the top left and bottom right corners are generally referred to as the Northwest (NW) and Southeast (SE) corners of a bounding box. Determining if a point is inside is simply checking if the latitude and longitude are within the outer latitude and longitude of the bounding box. Below is psuedo code (where NW.Lat is the latitude of the NW corner, etc).

If ( ( Lat <= NW.Lat && Lat >= SE.Lat ) &&
     ( Lon >= NW.Lon && Lon <= SE.Lon ) )
{
    // The point is in the box
}
like image 170
Andrew - OpenGeoCode Avatar answered Jan 02 '23 00:01

Andrew - OpenGeoCode