【发布时间】: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