Skip to Content
Mastering Geospatial Analysis with Python
book

Mastering Geospatial Analysis with Python

by Silas Toms, Paul Crickard, Eric van Rees
April 2018
Beginner to intermediate content levelBeginner to intermediate
440 pages
11h 36m
English
Packt Publishing
Content preview from Mastering Geospatial Analysis with Python

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) ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Geospatial Analysis with Python

Learning Geospatial Analysis with Python

Joel Lawhead

Publisher Resources

ISBN: 9781788293334Supplemental Content