Postgresql 使用地球炼金术2按距离选择和排序。坏的ST_AsBinary wrap

Postgresql 使用地球炼金术2按距离选择和排序。坏的ST_AsBinary wrap,postgresql,python-3.x,sqlalchemy,postgis,geoalchemy2,Postgresql,Python 3.x,Sqlalchemy,Postgis,Geoalchemy2,我正试图通过GeoAlchemy2/PostGIS按到某个点的距离来选择和订购商店,但由于某些原因,我一直收到一个错误 似乎地球炼金术2用ST_作为二进制来包装东西,但当我尝试选择距离时,它尝试包装距离计算的结果。我不知道如何解决这个问题 我使用这个ORM查询 distance = ( Store.coordinates.cast(Geometry) .distance_centroid(query_centroid) .label('distance') ) stor

我正试图通过GeoAlchemy2/PostGIS按到某个点的距离来选择和订购商店,但由于某些原因,我一直收到一个错误

似乎地球炼金术2用ST_作为二进制来包装东西,但当我尝试选择距离时,它尝试包装距离计算的结果。我不知道如何解决这个问题

我使用这个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
sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)函数st_asbinary(双精度)不存在
第1行:…二进制(stores.coordinates)作为stores\u坐标,ST\u作为二进制。。。
^
提示:没有与给定名称和参数类型匹配的函数。您可能需要添加显式类型转换。
[SQL:'选择stores.store\u id作为stores\u store\u id,
stores.name作为stores\u name,
stores.address\u详细信息作为stores\u address\u详细信息,
ST_AsBinary(stores.coordinates)作为stores_坐标,
ST_AsBinary(CAST(将坐标存储为几何体(几何体,-1))ST_geomefromewkt(%(参数1)s))作为距离
从门店按距离订购']13-46.730347'}]
[参数:{'param_1':'点(-23.3569)
问题就在这一部分

ST_AsBinary(
    CAST(stores.coordinates AS geometry(GEOMETRY,-1))
    <->
    ST_GeomFromEWKT(%(param_1)s)
) AS distance 
ST_AsBinary(
强制转换(将坐标存储为几何体(几何体,-1))
ST_GeomFromEWKT(%(参数1)s)
)作为距离
注意ST_AsBinary如何包装两点之间的距离,而不是仅包装几何图形,例如?(我也不确定在这种情况下它是否应该包装几何图形)


有人能帮忙吗?我只想知道事情有多远。

freenode的一个普通用户为我回答了这个问题

GeoAlchemy2将转换类型为Geometry的列(如果它们在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)