Point in polygon

One of the most common problems is trying to determine whether a point is in the polygon. To solve this problem with PostGIS, you can use ST_Contains or ST_Intersects.

St_Contains takes two geometries and determines whether the first contains the second. 

The order matters—a contains b, which is the opposite of ST_Within, which uses the order b, a.

By using contains, no part of geometry b can be outside of geometry a. The following code solves a point in polygon (PIP) problem:

isin=Point(-106.558743,35.318618)cur.execute("SELECT ST_Contains(polygon.location,ST_GeomFromText('{}'))              FROM poly polygon WHERE polygon.id=1".format(isin.wkt))cur.fetchall()

The previous code creates a point and then uses ST_Contains(polygon,point) ...

Get Mastering Geospatial Analysis with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.