Python SQLAlchemy: displaying PostGIS geometry field
SQLAlchemy maps database columns to objects in 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))
What type should I use to map a PostgIS geom column to a class member?
+3
source to share
1 answer
As far as I know, there is no built-in way to do this in SQLAlchemy. However GeoAlchemy2 is a SQLAlchemy extension that adds support for geospatial fields (points, lines, polygons, etc.):
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 also provides functionality for spatial queries. From the docs :
query = session.query(Lake).filter(Lake.geom.ST_Contains('POINT(4 1)'))
+4
source to share