Google ndb中GeoPt的奇怪查询比较 [英] Weird query comparison of GeoPt in google ndb

查看:63
本文介绍了Google ndb中GeoPt的奇怪查询比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用GeoProperty查询数据存储区中的实体,但奇怪的是它将比较GeoProperty的第一个参数lat. 如果对lat进行比较,则它将直接返回结果.唯一的例外是纬度相等,然后比较经度. 例如,GeoPt(11,10)< GeoPt(9,20)将返回False,因为前者的纬度不小于后者.但是,后者比前者更大.因此,当我要查询数据存储区中的实体时,这种比较比较麻烦.有解决办法吗?

解决方案

您需要查看NDB的一些空间查询替代方案.有关空间数据库的Wikipedia文章,列出了地理数据库,您必须在AppEngine之外实现并调用.

或者,您可以仅使用Search API,该API 链接引用了dan-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)

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?

解决方案

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)

这篇关于Google ndb中GeoPt的奇怪查询比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆