Python Point-in-Polygon with Shapely

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

First, you need to create a polygon. If you already have an ordered list of coordinate points that define a closed ring, you can create a Polygon directly, like so:

from shapely.geometry import Polygon
poly = Polygon(((0, 0), (0, 1), (1, 1), (1, 0)))

But what if you just have a bunch of points in no particular order? Then you can create a MultiPoint geometry and get the convex hull polygon.

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.

  1. point.within(polygon)
  2. polygon.contains(point)

point should be an instance of the Point class, and poly is of course an instance of Polygon. within and contains are the converse of each other, so whichever method you use is entirely up to you.

Overlapping Polygons

In addition to point-in-polygon, you can also determine whether shapely geometries overlap each other. poly.within(poly) and 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.

There’s a lot more you can do with this very useful python geometry package, so take a look at the Shapely Manual as well as some usage examples.