Shapely is an offshoot of the GIS-Python project that provides spatial geometry functions independent of any geo-enabled database. In particular, it makes python point-in-polygon calculations very easy.
Creating a Polygon
from shapely.geometry import Polygon poly = Polygon(((0, 0), (0, 1), (1, 1), (1, 0)))
from shapely.geometry import MultiPoint # coords is a list of (x, y) tuples poly = MultiPoint(coords).convex_hull
Now that you have a
polygon, determining whether a
point is inside it is very easy. There’s 2 ways to do it.
In addition to point-in-polygon, you can also determine whether shapely geometries overlap each other.
poly.contains(poly) can be used to determine if one polygon is completely within another polygon. For partial overlaps, you can use the intersects method, or call intersection to get the overlapping area as a polygon.