您需要查看 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)