Search 10 nearest Locations in Datastore

后端 未结 3 686
离开以前
离开以前 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:20

    We run a geospatial-heavy service on AppEngine.

    Our solution is to store the locations on Memcache and doing the calculations directly instead of relying on the database.

    This obviously depends on the amount of locations, but if you are clever about how you store the locations, you can search very quickly.

    R-Trees are a very good example: https://en.wikipedia.org/wiki/R-tree

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-19 00:40

    please have a look at the following post

    Geospatial Query at Google App Engine Datastore

    Unfortunately it is not possible to get the nearest location from the google cloud datastore itself. You have to implement your own logic or you have to use a different database

    0 讨论(0)
提交回复
热议问题