Search code examples
pythonpython-3.xlatitude-longitude

Latitude and Longitude Lying in a Rectangular Area


While 2 corner points were given of an area, rectangular in this case, I was trying to find whether a custom given coordinates are going to be within that rectangle or not. However, the magic is the Earth being a sphere. I believe, even though it looks like a rectangle, it is really not as it bends down from the sides. So, my question is that how to find whether a given Latitude/Longitude is lying within another created Rectangle or not?

I have written a custom Python script to get the lower & upper corners of the rectangle in latitude and longitude already, which returns values as following:

(['35.805949', '25.552255'], ['42.278201', '44.817695'])

What is the algorithm lying behind this idea?


Solution

  • The projection geometry simply denies the earth's curvature. This is trivial plan geometry. Let you corners be (w, n) and (e, s). To find whether (x, y) is in the rectangle, use a trivial bounds check:

    if w <= x <= e and
       s <= y <= n:
    

    You may need to adjust the comparisons if you use some other coordinate system; I'm using the standard Cartesian mapping.