嗨!我需要使用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 非常感谢您的时间!
发布于 2014-06-01 17:11:32
经过一番研究,我成功地实现了以下查询:)
posts = db.session.query(Post).filter(func.ST_DWithin(Post.location, point, 10))https://stackoverflow.com/questions/23981056
复制相似问题