python - GeoAlchemy ST_DWithin 实现

标签 python sqlalchemy postgis flask-sqlalchemy

嗨!我需要使用 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 

非常感谢您的宝贵时间!

最佳答案

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

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

关于python - GeoAlchemy ST_DWithin 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23981056/

相关文章:

python - Pylons/SQLAlchemy 和 MySQL 的导入错误

postgresql - 通过在 postgresql 上声明变量来选择查询

hibernate 空间 : "No Dialect mapping for JDBC type: 3000"

sql - Postgres - 如何在插入时自动调用 ST_SetSRID(ST_MakePoint(lng, lat), 4326)?

python - 具有动态输入的 SQLAlchemy with_entities

python - 测试kivy编写的应用程序时如何与UI交互?

python - 如何为轴添加第二行标签

python - 为什么 "1000000000000000 in range(1000000000000001)"在 Python 3 中如此之快?

python - SQLAlchemy 连接来自同一个表的多个列

python - 为什么我不能调用 del [ :] on a dict?