【问题标题】:finding nearest locations in GeoAlchemy and PostGIS在 GeoAlchemy 和 PostGIS 中查找最近的位置
【发布时间】:2020-09-02 03:31:12
【问题描述】:

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

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)

不幸的是,我的函数一直返回空列表。我对 GeoAlchemy 不是很熟悉,因为这是我第一次使用它。任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: sqlalchemy postgis geoalchemy2


    【解决方案1】:

    在 Postgis 中,坐标必须先表示为经度,再表示为纬度。

    您需要交换输入中的坐标

    geolocation: SRID=4326;POINT(34.59629822 135.6029968) 应该变成geolocation: SRID=4326;POINT(135.6029968 34.59629822)

    【讨论】:

    • 我不这么认为。我切换了订单,它仍然没有返回任何内容。此外,以下 SQL 查询有效: SELECT * FROM public.location ORDER BY geolocation st_setsrid(st_makepoint(38.957431,-77.401142),4326) LIMIT 6;
    【解决方案2】:

    我能够解决这个问题。长话短说,因为我使用的是 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)
    

    【讨论】:

      猜你喜欢
      • 2020-11-06
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      相关资源
      最近更新 更多