使用 GeoAlchemy2 按距离选择和排序。坏 ST_AsBinary 换行

Selecting and ordering by distance with GeoAlchemy2. Bad ST_AsBinary wrap

我正在尝试 select 并根据商店与 GeoAlchemy2 / PostGIS 的某个点的距离对商店进行排序,但由于某种原因,我不断收到错误消息。

GeoAlchemy2 似乎用 ST_AsBinary 包装东西,但是当我尝试 select 距离时,它会尝试包装距离计算的结果。我不知道如何解决这个问题。

我使用这个 ORM 查询。

distance = (
    Store.coordinates.cast(Geometry)
    .distance_centroid(query_centroid)
    .label('distance')
)

stores = stores.order_by(distance).add_columns(distance)

模型。

class Store(db.Model):
    __tablename__ = 'stores'

    store_id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

    address_details = db.Column(db.String)
    coordinates = db.Column(Geography('POINT'))

我得到的错误。

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) function st_asbinary(double precision) does not exist
LINE 1: ...Binary(stores.coordinates) AS stores_coordinates, ST_AsBinar...
                                                             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
[SQL: 'SELECT stores.store_id AS stores_store_id,
    stores.name AS stores_name,
    stores.address_details AS stores_address_details,
    ST_AsBinary(stores.coordinates) AS stores_coordinates,
    ST_AsBinary(CAST(stores.coordinates AS geometry(GEOMETRY,-1)) <-> ST_GeomFromEWKT(%(param_1)s)) AS distance
    FROM stores ORDER BY distance']13 -46.730347)'}]
[parameters: {'param_1': 'POINT(-23.3569

问题就出在这部分...

ST_AsBinary(
    CAST(stores.coordinates AS geometry(GEOMETRY,-1))
    <->
    ST_GeomFromEWKT(%(param_1)s)
) AS distance 

请注意 ST_AsBinary 如何包裹两点之间的距离,而不是仅仅包裹 geom,例如? (我也不确定在这种情况下它是否应该包裹 geom)

有人可以帮忙吗?我只想知道事情进展到什么程度了。

freenode 的普通用户为我解答了。

GeoAlchemy2 将转换几何类型的列,如果它们在 select 语句中。即使距离表达式的结果是双精度数,而不是几何体,GeoAlchemy2 也不够聪明,无法弄清楚。

该列需要在 ORM 中显式转换。

固定查询:

distance = (
    Store.coordinates.cast(Geometry)
    .distance_centroid(query_centroid)
    .cast(db.Float)
    .label('distance')
)

stores = stores.order_by(distance).add_columns(distance)