GeoAlchemy2存储点和查询结果 [英] GeoAlchemy2 store point and query results

查看:90
本文介绍了GeoAlchemy2存储点和查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与以前的版本相比,GeoAlchemy2的文档似乎功能不全.

The documentation on GeoAlchemy2 doesn't seem fully featured (as compared to the pervious version).

我有一个模特:

class AddressCode(Base):
    __tablename__ = 'address_codes'
    id = Column(Integer, primary_key=True)
    code = Column(Unicode(34))
    geometry = Column(Geometry('POINT'))

我想存储经/纬度数据,我试图将其保存在上述模型中,例如

And I want to store lat/long data, which I tried to save in the above model, example

"51.42553,-0.666085"

哪个给我错误:

"Parse error at position 9 within Geometry (the "," char")

有人能弄清楚我在哪里出问题了吗?

Anyone able to shed some light on where I am going wrong here?

关于这个主题,我将如何执行查询说..

Also on the subject, how would I peform a query to say..

显示最近的20位用户:

Show nearest 20 users:

class AddressCode(Base):
    __tablename__ = 'address_codes'
    id = Column(Integer, primary_key=True)
    name = Column(Unicode(34))
    geometry = Column(Geometry('POINT'))

有什么感觉吗?

geom_var = "51.42553,-0.666085"
Session.query(User).filter(func.ST_DWithin, 20, geom_var).all()

推荐答案

在GeoAlchemy和GeoAlchemy2中,您都需要以众所周知的文本格式

In both GeoAlchemy and GeoAlchemy2 you need to specify the geometries in the well-known text format called WKT or Well-known text, or the Well-known binary format. For a point the syntax is 'POINT(X Y)', thus 'POINT(-0.666085 51.42553)' notice that the longitude comes first, then latitude.

shapely 模块包含有用的函数,这些函数可用于处理关系数据库之外的几何图形,以及之间的轻松转换Python几何类和WKT,WKB格式.

The shapely module contains useful functions for handling geometries outside relational databases, along with easy conversions between Python geometry classes and WKT, WKB formats.

这篇关于GeoAlchemy2存储点和查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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