问题描述
我有很多包含 geoPoints 的实体存储在 Google 的数据存储中.现在我需要根据发送到 Google Cloud 函数的位置来获取最近的 10 个位置.
我看到,Google 的 App Engine 中有一个 distance() 函数,但在 Google Cloud Functions 中没有可比性,甚至无法在数据库中计算任何内容.
I have a lot of Entities containing geoPoints stored in Google's Datastore.Now I need to get the 10 nearest locations based on a Location sent to a Google Cloud Function.
I saw, that there is a distance() function in Google's App Engine, but nothing comparable in Google Cloud Functions, not even the possible to calculate anything in the Database.
是否可以仅使用 Google Cloud Functions 从 Datastore 获取最近的 10 个位置,还是我需要为此使用不同的数据库?
Is it possible to get the 10 nearest Locations from Datastore only using Google Cloud Functions or do I need to use a different Database for that ?
最好的问候,
帕斯卡
Best Regards,
Pascal
推荐答案
我也有类似的需求,我用基于网格的聚类方案解决了.
I had a similar need, and I solved it with a grid-based clustering scheme.
基本上,我创建了一个计算字符串属性,它是纬度 & 的字符串连接.去掉小数的经度.
Essentially I created a Computed String Property that was the string concatenation of the latitude & longitude with the decimals chopped off.
如果一个实体有 obj.latitude = 37.123456
&obj.longitude = 45.234567
然后 obj.grid_id="37:45"
If an entity had obj.latitude = 37.123456
& obj.longitude = 45.234567
then obj.grid_id="37:45"
在执行搜索时,我确定搜索纬度的网格 &经度以及其他 8 个周围网格,并查询驻留在这 9 个网格中的所有实体.
When performing a search, I determine the grid of the search latitude & longitude as well as the 8 other surrounding grids and queried for all entities that resided in those 9 grids.
# for search latitude = 37.456 & longitude = 45.67
query = SomeModel.query(SomeModel.grid_id.IN([
'36:44', '36:45', '36:46',
'37:44', '37:45', '37:46',
'38:44', '38:45', '38:46',
]))
然后你会在代码中找到最近的 10 个.
You would then find the 10 nearest in code.
根据您的需要,您可能希望让网格 id 包含小数位 (obj.grid_id="37.1:45.2"
) 或降低它们的精确度 (obj.grid_id="30:40"
)
Depending on your needs you may want to make grids id include decimal positions (obj.grid_id="37.1:45.2"
) or make them less precise(obj.grid_id="30:40"
)
这可能对您有用,也可能不适用,具体取决于您的数据点分布,在这种情况下,Zebs 建议使用 R-Tree 更可靠,但这很容易实现并满足我的需求.
This may or may not work for you depending on the distribution of you data points, in which case Zebs suggestion to use R-Tree is more robust, but this was simple to implement and sufficed my needs.
这篇关于在 Datastore 中搜索 10 个最近的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!