Sqlalchemy 在地炼金术和PostGIS中寻找最近的位置

Sqlalchemy 在地炼金术和PostGIS中寻找最近的位置,sqlalchemy,postgis,geoalchemy2,Sqlalchemy,Postgis,Geoalchemy2,我试图做一个简单的查询,在那里我可以找到离用户最近的位置(在我的示例中,我使用的是机场)。数据库记录如下所示: id: 2249 city: Osaka country: Japan location_type_id: 16 geolocation: SRID=4326;POINT(34.59629822 135.6029968) name: Yao Airport @classmethod def find_nearest_locations(cls, data): self =

我试图做一个简单的查询,在那里我可以找到离用户最近的位置(在我的示例中,我使用的是机场)。数据库记录如下所示:

id: 2249
city: Osaka
country: Japan
location_type_id: 16
geolocation: SRID=4326;POINT(34.59629822 135.6029968)
name: Yao Airport
@classmethod
  def find_nearest_locations(cls, data):
    self = cls(data)
    return db.session.query(LocationModel.location_type_id==16). \
      order_by(Comparator.distance_centroid(LocationModel.geolocation,
          func.Geometry(func.ST_GeographyFromText(self.__format_geolocation())))).limit(self.result_quantity)
我的数据库查询如下所示:

id: 2249
city: Osaka
country: Japan
location_type_id: 16
geolocation: SRID=4326;POINT(34.59629822 135.6029968)
name: Yao Airport
@classmethod
  def find_nearest_locations(cls, data):
    self = cls(data)
    return db.session.query(LocationModel.location_type_id==16). \
      order_by(Comparator.distance_centroid(LocationModel.geolocation,
          func.Geometry(func.ST_GeographyFromText(self.__format_geolocation())))).limit(self.result_quantity)
不幸的是,我的函数一直返回空列表。我对地球炼金术不是很熟悉,因为这是我第一次使用它。任何帮助都将非常感激


谢谢。

在Postgis中,坐标必须先表示为经度,然后表示为纬度

您需要交换输入中的坐标


地理定位:SRID=4326;点(34.59629822 135.6029968)
应变为
地理位置:SRID=4326;点(135.6029968 34.59629822)

我能够解决问题。长话短说,因为我使用的是Flask SQLAlchemy而不是常规SQLAlchemy,所以我必须搜索LocationModel,而不是db.session.query。代码如下所示

@classmethod
  def find_nearest_locations(cls, data):
    self = cls(data)
    return LocationModel.query.filter(LocationModel.location_type_id==self.location_type_id). \
      order_by(Comparator.distance_centroid(LocationModel.geolocation,
          func.Geometry(func.ST_GeographyFromText(self.__format_geolocation())))).limit(self.result_quantity)

我认为情况并非如此。我把订单换掉了,但它仍然一无所获。此外,以下SQL查询工作:通过geolocation st_SetGrid(st_makepoint(38.957431,-77.401142),4326)从public.location ORDER中选择*6;