You are given a GPS coordinate as a point [latitude, longitude] and a geographic polygon defined by a list of vertices, each a [latitude, longitude] pair. Determine whether the point falls within the polygon.
The polygon is defined by its vertices in order (either clockwise or counterclockwise), and the last vertex is implicitly connected to the first vertex to close the polygon.
Return True if the point is inside the polygon or on its boundary, and False otherwise.
Function Signature
def is_point_in_polygon(point: list[float], polygon: list[list[float]]) -> bool:
Rules
-
Treat the coordinates as points on a flat plane. Each edge is the straight segment between two consecutive vertices in (latitude, longitude) space, plus the closing edge from the last vertex back to the first. Ignore the Earth's curvature and do not wrap longitudes around the 180th meridian.
-
A point that lies on an edge, including a point at a vertex, counts as inside.
-
The polygon can be concave.
-
Every test point either lies on the boundary (up to floating-point rounding of the input) or is at least
1e-6
away from every edge, so the expected answer does not depend on the tolerance you pick for the boundary check.
Constraints
-
3 <= len(polygon) <= 10^4
-
-90 <= latitude <= 90
-
-180 <= longitude <= 180
-
All coordinates are floating-point numbers.
-
The polygon is simple: non-adjacent edges do not intersect, and adjacent edges meet only at their shared vertex.
Examples
Input: point = [1, 1], polygon = [[0, 0], [4, 0], [4, 4], [0, 4]]
Output: True
The polygon is the square with corners (0, 0), (4, 0), (4, 4) and (0, 4). Both coordinates of (1, 1) are strictly between 0 and 4, so the point is inside.
Input: point = [5, 5], polygon = [[0, 0], [4, 0], [4, 4], [0, 4]]
Output: False
Both coordinates of (5, 5) are greater than 4, so the point is outside the same square.
Input: point = [4, 2], polygon = [[0, 0], [4, 0], [4, 4], [0, 4]]
Output: True
The point lies on the edge from (4, 0) to (4, 4): its first coordinate is 4 and its second is between 0 and 4. Boundary points count as inside.