google ndb 中 GeoPt 的奇怪查询比较

编程入门 行业动态 更新时间:2024-10-28 10:28:13
本文介绍了google ndb 中 GeoPt 的奇怪查询比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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?

推荐答案

您需要查看 NDB 的一些替代方案来进行空间查询.关于空间数据库的维基百科文章有一个地理数据库,您必须在 AppEngine 之外实施并调用.

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.

或者,您可以只使用搜索 API,即 链接 dan-cornilescu 引用:

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)

产生:

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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-17 19:26:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/921635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇怪   google   ndb   GeoPt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!