【问题标题】:GeoAlchemy ST_DWithin implementationGeoAlchemy ST_D在实施中
【发布时间】:2023-03-14 12:04:02
【问题描述】:

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

以下是相关代码sn-ps:

# 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 

非常感谢您的宝贵时间!

【问题讨论】:

    标签: python sqlalchemy postgis flask-sqlalchemy


    【解决方案1】:

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

    posts = db.session.query(Post).filter(func.ST_DWithin(Post.location, point, 10))
    

    【讨论】:

    • 干得好。我很了解 Postgis,但对 GeoAlchemy 很陌生。我认为您可以接受自己的答案。
    • 我猜距离(10)是米?
    • @cglacet 我认为它以米为单位postgis.net/docs/ST_DWithin.html
    猜你喜欢
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多