与GeoAlchemy示例代码的瓶 [英] Flask with GeoAlchemy sample code

查看:323
本文介绍了与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示例代码的瓶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆