【问题标题】:Weird query comparison of GeoPt in google ndbgoogle ndb 中 GeoPt 的奇怪查询比较
【发布时间】:2018-03-13 09:58:17
【问题描述】:

我尝试使用 GeoProperty 查询数据存储区中的实体,但奇怪的是它会比较 GeoProperty 的第一个参数 lat。 如果比较lat,则直接返回结果。唯一的例外是纬度相等,然后比较经度。 例如,GeoPt(11, 10)

【问题讨论】:

  • 比较 GeoPt 的目的是什么?这真的没有什么意义。您是否试图找到另一个点西南的点?
  • @JeffO'Neill 我正在尝试根据用户坐标在一定距离内查询商店,这样我就可以在特定时间步行到那些商店。
  • @DanCornilescu 这似乎很有用,但我现在不知道如何在数据存储上应用它。我会找时间弄清楚的。
  • 不能,数据存储不支持这样的查询。

标签: google-app-engine app-engine-ndb google-app-engine-python


【解决方案1】:

您需要查看 NDB 的一些替代方案来进行空间查询。 Spatial database 上的 Wikipedia 文章有一个 Geodatabases 列表,您必须在 AppEngine 之外实现并调用它。

或者,您可以只使用搜索 API,即 the linkdan-cornilescu 引用:

import webapp2
from google.appengine.api import search

class MainHandler(webapp2.RequestHandler):
    def get(self):

        stores_idx = search.Index(name='stores')

        store_a = search.Document(
            doc_id='store_a',
            fields=[search.GeoField(name='LOC', value=search.GeoPoint(32, -112))]
        )

        store_b = search.Document(
            doc_id='store_b',
            fields=[search.GeoField(name='LOC', value=search.GeoPoint(32, -111))]
        )

        stores_idx.put(store_a)
        stores_idx.put(store_b)

        # Search for stores kinda close (-112 vs -112.1), and not so close

        results_100 = stores_idx.search(
            "distance(LOC, geopoint(32, -112.1)) < 100"
        )

        results_100000 = stores_idx.search(
            "distance(LOC, geopoint(32, -112.1)) < 100000"
        )

        results_1000000 = stores_idx.search(
            "distance(LOC, geopoint(32, -112.1)) < 1000000"
        )


        self.response.write(
"""
%s stores within 100 meters of (32, -112.1) <br/>
%s stores within 100,000 meters of (32, -112.1) <br/>
%s stores within 1,000,000 meters of (32, -112.1) <br/>
""" % (
    len(list(results_100)),
    len(list(results_100000)),
    len(list(results_1000000)),
)
)

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

产生:

0 stores within 100 meters of (32, -112.1)
1 stores within 100,000 meters of (32, -112.1)
2 stores within 1,000,000 meters of (32, -112.1)

【讨论】:

    【解决方案2】:

    您想查询您的数据存储区,以了解哪个位置在您的用户位置的特定步行时间内的条目。这个步行时间可以大致换算成距离。这将允许您使用似乎适合您的用例的 Search API 的距离特殊功能。

    假设每个商店条目都有一个包含地理点的 store_location 字段,并且您的用户坐标为 (11, 10),您可以使用以下搜索查询搜索距离用户 100 米半径内的商店:

    query = "distance(store_location, geopoint(11, 10)) < 100"
    

    【讨论】:

      猜你喜欢
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      相关资源
      最近更新 更多