Menu

How to check if a coordinate resides inside a certain area (circle, polygon)?

Help
Master
2021-10-12
2021-10-12
  • Master

    Master - 2021-10-12

    Hi, Suppose I want to find out if a given latitude, longitude is inside of an area like a circle or a polygon. How could I go about this? would simply writing something like below do the trick?

    from geographiclib.geodesic import Geodesic
    
    def get_distance(gps_coordinate1, gps_coordinate2):
        """Calculates the distance between two GPS coordinates in meters.
    
        Args:
            gps_coordinate1 (tuple(float, float)): latitude and longitude of point A
            gps_coordinate2 (tuple(float, float)): latitude and longitude of point B
    
        Returns:
            float: distance in meters
        """
        return Geodesic.WGS84.Inverse(gps_coordinate1[0],
                                      gps_coordinate1[1], 
                                      gps_coordinate2[0], 
                                      gps_coordinate2[1])['s12']
    

    and you use it like this:

    center_point = (38.21260337,-98.20744415)
    test_point = (38.21257849,-98.20689602)
    radius = 5 # in meters 
    
    if get_distance(center_point, test_point) <= radius:
        print('inside of a 5m radius')
    else:
        print('outside of a 5m radius')
    

    my other question is, will this be a great circle distance ?
    I'm actually trying to calculate the distance for a small quadcopter that flies around 100/150 meters above the ground. I'm not sure if I need to be calculating the great circle distance for it or whether the code given above calculates the great circle distance itself!
    I'd really appreciate any kind of help in this
    thanks a lot. really appreciate it

     

    Last edit: Master 2021-10-12
  • Charles Karney

    Charles Karney - 2021-10-12

    This code computes the geodesic distance on the ellipsoid. Generally it is the geodesic distance you want to use. (For a sphere the geodesic distance is the same as the great circle distance.) So your example tests whether a point is inside a geodesic circle of radius 5 m.

    Of course for very short distances (like 5 m), you can use other methods, basically approximating the surface of the earth by a plane.

     
  • Master

    Master - 2021-10-12

    Thanks a lot. concerning

    Generally it is the geodesic distance you want to use.

    So there is not any catches in scenarios like this right? whether I'm dealing with a 5 meters radius or a much larger area, I can always safely go with the Geodesic class and be fine?

    Of course for very short distances (like 5 m), you can use other methods, basically approximating the surface of the earth by a plane.

    like what? could you kindly name some of them? a snippet of code would also be of great help and appreciated a lot.

     

Anonymous
Anonymous

Add attachments
Cancel