GeoAlchemy的ST_DWithin实现
你好!我需要用GeoAlchemy来实现一个查询,获取所有靠近某个点的点(比如说,在10米的范围内)。我在PostGIS数据库中使用地理字段来存储这些点。从SQLAlchemy的文档中,我了解到我需要使用ST_DWithin这个函数,但我不太确定如何在我的Flask应用中实现这个函数。
以下是相关的代码片段:
# models.py
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
location = db.Column(Geography(geometry_type='POINT', srid=4326))
#routes.py
from models import Post
@app.route('/get_coordinates')
lat = request.args.get('lat', None)
lng = request.args.get('lng', None)
if lat and lng:
point = WKTElement('POINT({0} {1})'.format(lng, lat), srid=4326)
# Here i should make the query to get all Posts that are near the Point
非常感谢你的时间!
1 个回答
10
经过一番研究,我成功地实现了以下查询 :)
posts = db.session.query(Post).filter(func.ST_DWithin(Post.location, point, 10))