sqlalchemy中的点类型?

时间:2016-05-15 00:03:41

标签: postgresql flask sqlalchemy flask-sqlalchemy

我发现这与Postgres中的Point类型有关:http://www.postgresql.org/docs/current/interactive/datatype-geometric.html

是否有SQLAlchemy版本?

我以这种方式存储价值:(40.721959482,-73.878993913)

3 个答案:

答案 0 :(得分:3)

您可以使用geoalchemy2 whis是sqlalchemy的扩展名,也可以与flask-sqlalchemy一起使用。

from sqlalchemy import Column
from geoalchemy2 import Geometry
# and import others

class Shop(db.Model):
    # other fields
    coordinates = Column(Geometry('POINT'))

答案 1 :(得分:1)

我发现这是对穆罕默德答案的一点修改。

是的我需要通过geo / sqlalchemy

添加一个点列
from sqlalchemy import Column
from geoalchemy2 import Geometry
# and import others

class Shop(db.Model):
    # other fields
    coordinates = Column(Geometry('POINT'))

在我的PostGIS / Gres和PGloader方面,因为我从csv加载格式化我的纬度和经度点为:"(40.721959482, -73.878993913)"我需要在vim中为我的所有条目做一个宏(为了强制该列遵循在PostGIS中创建点的方式,我强调了这一点,因此我将csv列转换为point(40.721959482 -73.878993913),然后在创建表时设置数据类型为geometry的location列( ()location geometry(point)

答案 2 :(得分:0)

您可以扩展UserDefinedType以达到您想要的效果。

Here's an example I found非常接近你想要的子类UserDefinedType

请注意,Mohammad Amin's answer仅在您的要点是地理点(纬度和经度限制)时才有效。如果要表示平面上的任何点,则不适用。此外,在这种情况下,您需要安装PostGIS扩展,我鼓励您使用地理点,因为它提供了大量的功能和额外的功能。