Weird query comparison of GeoPt in google ndb

匆匆过客 提交于 2019-12-11 02:12:40

问题


I try to query entities in datastore with GeoProperty, but the strange thing is it will compare GeoProperty's first argument, lat. If lat is compared, then it will directly return the result. The only exception is latitude is equal, then the longitude is then compared. For example, GeoPt(11, 10) < GeoPt(9, 20) will return False because former latitude is not smaller than latter. However, latter is bigger than former. SO this kind of comparison bother me when I want to query the entities in datastore. Any solution?


回答1:


You'd like to query your Datastore for entries which location is within a particular walking time from your user's location. This walking time can be roughly converted to a distance. This would allow you to use the Search API's distance special function which seems to fit your use case.

So let's say each store entry has a store_location field holding a geopoint and your user coordinates are (11, 10), you can search for stores within a 100 meters radius from your user by using the following search query:

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



回答2:


You'll need to look at some alternatives to NDB for spatial queries. The Wikipedia article on Spatial database has a list of Geodatabases, which you'd have to implement outside of AppEngine and call to.

Alternatively, you can just use the Search API, which is the link dan-cornilescu referenced:

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)

which yields:

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)


来源:https://stackoverflow.com/questions/49253141/weird-query-comparison-of-geopt-in-google-ndb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!