Search 10 nearest Locations in Datastore

后端 未结 3 685
离开以前
离开以前 2021-01-19 00:03

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.<

3条回答
  •  再見小時候
    2021-01-19 00:38

    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.

    If an entity had obj.latitude = 37.123456 & obj.longitude = 45.234567 then obj.grid_id="37:45"

    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',
    ]))
    

    You would then find the 10 nearest in code.

    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")

    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.

提交回复
热议问题