PYTHON POINT-IN-POLYGON WITH SHAPELY
1. 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)))
2. point-in-polygon
Now that you have a polygon, determining whether a point is inside it is very easy. There’s 2 ways to do it.
point.within(polygon)
polygon.contains(point)
3. 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.
strong@foreverstrong:~$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from shapely.geometry import Polygon
>>> poly1 = Polygon([(0,1,1),(1,-1,1),(-1,-1,1)])
>>> poly2 = Polygon([(0,1,0),(1,-1,0),(-1,-1,0)])
>>> poly1.intersects(poly2)
True
>>>
>>> poly1.equals(poly2)
True
>>>
>>> exit()
strong@foreverstrong:~$
A third z coordinate value may be used when constructing instances, but has no effect on geometric analysis. All operations are performed in the x-y plane.