Python SQLAlchemy:映射PostGIS geom字段

时间:2014-08-20 13:00:57

标签: postgresql orm sqlalchemy postgis

SQLAlchemy将数据库列映射到Python中的对象成员。 For example

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
    name = Column(String(50))
    fullname = Column(String(50))
    password = Column(String(12))

应该使用哪种类型将PostGIS geom column映射到班级成员?

1 个答案:

答案 0 :(得分:6)

据我所知,SQLAlchemy没有内置方法可以做到这一点。但是,GeoAlchemy2是SQLAlchemy的扩展,它增加了对地理空间字段(点,线,多边形等)的支持:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Sequence
from geoalchemy2 import Geometry

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
    name = Column(String(50))
    fullname = Column(String(50))
    password = Column(String(12))
    geom = Column(Geometry('POINT'))

GeoAlchemy2还提供空间查询功能。来自docs

query = session.query(Lake).filter(Lake.geom.ST_Contains('POINT(4 1)'))