问题描述
限时送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:关键词]
发布评论