Spatial queries

To find the county and the congressional district, two PostGIS spatial analysis techniques are used—ST_Contains and ST_Intersects. The first query determines if the arena is contained within a county; if not, the result is null (or None in Python):

        county=session.query(County).filter(                        County.geom.ST_Contains(arena.geom)).first()        if county != None:            district=session.query(District).filter(                       District.geom.ST_Intersects(arena.geom)).first()

While ST_Contains could be used for both queries, I wanted to demonstrate that the GeoAlchemy2 ORM allows for access to all PostGIS functions when using Geometry columns. These searches combine the SQLAlchemy filter method with the GeoAlchemy2 ORM to make it possible to return query results ...

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.