I am using python and I have defined the latitudes and longitudes (in degrees) of a polygon on the map.
My goal is to check if a generic point P
of coordinates
Here is a possible solution to my problem.
np.array([[Lon_A, Lat_A], [Lon_B, Lat_B], [Lon_C, Lat_C]])
polygon.contains(point)
to test if point is inside (True
) or outside (False
) the polygon.Here is the missing part of the code:
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
lons_lats_vect = np.column_stack((lons_vect, lats_vect)) # Reshape coordinates
polygon = Polygon(lons_lats_vect) # create polygon
point = Point(y,x) # create point
print(polygon.contains(point)) # check if polygon contains point
print(point.within(polygon)) # check if a point is in the polygon
Note: the polygon does not take into account great circles, therefore it is necessary to split the edges into many segments thus increasing the number of vertices.
E.g. print(Polygon([(0, 0), (1, 0), (1, 1)]).contains(Point(0, 0)))
will fail
So one can use
print(polygon.touches(point)) # check if point lies on border of polygon