首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GeoAlchemy ST_DWithin实现

GeoAlchemy ST_DWithin实现
EN

Stack Overflow用户
提问于 2014-06-01 14:47:18
回答 1查看 1.9K关注 0票数 6

嗨!我需要使用GeoAlchemy实现一个查询,以获得所有靠近给定点的点(例如,在10米半径内)。为了存储点,我在我的PostGIS数据库中使用地理字段。从SQLAlchemy文档中,我发现我需要使用ST_DWithin函数,但我不太确定如何在我的Flask应用程序中实现这个函数。

下面是相关的代码片段:

代码语言:javascript
复制
# 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 

非常感谢您的时间!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-01 17:11:32

经过一番研究,我成功地实现了以下查询:)

代码语言:javascript
复制
posts = db.session.query(Post).filter(func.ST_DWithin(Post.location, point, 10))
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23981056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档