GeoAlchemy2 store point and query results

假装没事ソ 提交于 2019-12-06 09:20:07

In both GeoAlchemy and GeoAlchemy2 you need to specify the geometries in the well-known text format called WKT or Well-known text, or the Well-known binary format. For a point the syntax is 'POINT(X Y)', thus 'POINT(-0.666085 51.42553)' notice that the longitude comes first, then latitude.

The shapely module contains useful functions for handling geometries outside relational databases, along with easy conversions between Python geometry classes and WKT, WKB formats.

Here's how you do it:

this region table is defined as:

regionTable = Table('region', metadata, Column('region_id', Integer, Sequence('region_region_id_seq'), primary_key=True), Column('type_cd', String(30)), Column('region_nm', String(255)), Column('geo_loc', Geography ) )

how to query it:

(give me all regions within 50 miles of my current location..)

sqlstring = select([regionTable], func.ST_DWithin(regionTable.c.geo_loc, 'POINT(-74.78886216922375 40.32829276931833)', 1609*50 ))

result = connection.execute(sqlstring)

            for row in result:
                    print "region name:", row['region_nm']
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!