SQLAlchemy Spatical функции для поиска по долготе / широте - PullRequest
0 голосов
/ 19 мая 2019

Не могли бы вы помочь мне написать правильный фильтр SQLAlchemy для поиска по долготе и широте?

Это таблица postgres:

\d geo_table;
         Materialized view "public.geo_table"
 Column |          Type           | Collation | Nullable | Default 
--------+-------------------------+-----------+----------+---------
 id | text                    |           |          | 
 geog   | geography(Polygon,4283) |           |          | 
 name   | text                    |           |          | 

Мне нужно реализовать тот же выбор с помощью SQLAlchemy:

SELECT ce_pid, name FROM geo_table WHERE ST_Within(ST_SetSRID(ST_POINT(149.14668176,-35.32202098),4283), geog::geometry);
from sqlalchemy import func


class SpatialModel(Base):
    __tablename__ = 'geo_table'

    id = Column(String, primary_key=True)
    name = Column(String)
    geog = Column(Geometry('Point', 4283))


def get_area(longitude, latitude)
    with closing(CustomSession()) as session:
        return session.query(SpatialModel).filter(func.ST_Within(SpatialModel.geog, func.ST_POINT(longitude, latitude, 4283)))
...