与GeoAlchemy示例代码的瓶

编程入门 行业动态 更新时间:2024-10-21 18:37:27
本文介绍了与GeoAlchemy示例代码的瓶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用SQLAlchemy 0.8,Flask-SQLAlchemy和Geoalchemy 2:任何人都可以为Gears提供任何示例代码?

from app import db from geoalchemy2.types import几何 class Point(db .Model): $ bb代表x / y坐标位置。 __tablename__ ='point' id = db .Column(db.Integer,primary_key = True) geom = db.Column(Geometry(geometry_type ='POINT',srid = 4326))

示例查询:

from geoalchemy2.elements import WKTElement 从应用程序导入模型 def get_nearest(lat,lon):#查找输入坐标的最近点#将输入坐标转换为WKT点并查询最近格式(lon,lat),srid = 4326)返回models.Point.query.order_by(models.Point.geom 。 distance_box(pt))。first()

将结果转换为x和y坐标转换为GeoJSON并提取坐标):

将geoalchemy2.functions作为func 导入json import db def point_geom_to_xy(pt):#从点几何提取x和y坐标 geom_json = json.loads(db.session.scalar(func.ST_AsGeoJSON pt.geom))) return geom_json ['coordinates']

Can anyone provide any sample code for Flask with GeoAlchemy?

解决方案

Using SQLAlchemy 0.8, Flask-SQLAlchemy and 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))

Sample query:

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()

One way of converting the result to x and y coordinates (convert to GeoJSON and extract coordinates):

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']

更多推荐

与GeoAlchemy示例代码的瓶

本文发布于:2023-10-13 10:17:27,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   代码   GeoAlchemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!