很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center .
8年前关闭。
任何人都可以为带有 GeoAlchemy 的 Flask 提供任何示例代码吗?
最佳答案
使用 SQLAlchemy 0.8、Flask-SQLAlchemy 和 Geoalchemy 2:
from app import db
from geoalchemy2.types import Geometry
class Point(db.Model):
"""represents an x/y coordinate location."""
__tablename__ = 'point'
id = db.Column(db.Integer, primary_key=True)
geom = db.Column(Geometry(geometry_type='POINT', srid=4326))
示例查询:
from geoalchemy2.elements import WKTElement
from app import models
def get_nearest(lat, lon):
# find the nearest point to the input coordinates
# convert the input coordinates to a WKT point and query for nearest point
pt = WKTElement('POINT({0} {1})'.format(lon, lat), srid=4326)
return models.Point.query.order_by(models.Point.geom.distance_box(pt)).first()
将结果转换为 x 和 y 坐标的一种方法(转换为 GeoJSON 并提取坐标):
import geoalchemy2.functions as func
import json
from app import db
def point_geom_to_xy(pt):
# extract x and y coordinates from a point geometry
geom_json = json.loads(db.session.scalar(func.ST_AsGeoJSON(pt.geom)))
return geom_json['coordinates']
关于python - 带有 GeoAlchemy 示例代码的 Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4069595/