0

I want to query for "nearby" datastore objects in GAE. To achieve, this I was planning to index ndb.GeoPt property in datastore entities. And Whenever a query is made for nearby objects, I will simply find four corner points of a square : (lat_min,lon_min),(lat_max,lon_min),(lat_min,lon_max),(lat_max,lon_max) and comparing ndb.GeoPt property to find if it lie in this square. Is this possible using indexing? How will I make a query to compare ndb.GeoPt property with the above 4 GeoPts.

Thanks in Advance.

The Coder
  • 121
  • 1
  • 7
  • I think that you better to use search API in this case. – Dmytro Sadovnychyi Oct 05 '15 at 06:49
  • 1
    Search API is costly and even I don't want that much accuracy. I would have definitely used it if i needed to search content through documents. I think its better to do it by using ndb.GeoPt property (if possible) , otherwise, indexing both latitude and longitude is best. – The Coder Oct 05 '15 at 14:26

1 Answers1

2

You could do it by generating the bounding box and saving it in your datastore object as a string, then nearby things will all have the same bounding box record and you can just look for that string.

This code: GeoBox

Can be used like so:

In [0]: compute("36.752", "-122.39532", 2, 5)
Out[1]: '36.80|-122.40|36.75|-122.35'

In [2]: compute("36.752", "-122.39533", 2, 5)
Out[3]: '36.80|-122.40|36.75|-122.35'

The first coordinate set is the west/north-most corner of the bounding box. The second coordinate set is the east/south-most corner of the bounding box.

We get the same answer for a slightly different position (-122.39532 vs -122.39533) at those settings (resolution=2,slice=5). So 'nearby' records are those that also contain the same 'geobox string' as your current record, or your current query evaluates to that geobox string and a simple equality search will then get those 'nearby' records for you.

That code can also compute the set of surrounding bounding boxes via compute_set.

In [4]: compute_set("37.78452", "-122.39532", 6, 25)
Out[5]: 
['37.784500|-122.395350|37.784475|-122.395325',
 '37.784500|-122.395325|37.784475|-122.395300',
 '37.784500|-122.395300|37.784475|-122.395275',
 '37.784525|-122.395350|37.784500|-122.395325',
 '37.784525|-122.395325|37.784500|-122.395300',
 '37.784525|-122.395300|37.784500|-122.395275',
 '37.784550|-122.395350|37.784525|-122.395325',
 '37.784550|-122.395325|37.784525|-122.395300',
 '37.784550|-122.395300|37.784525|-122.395275']

The other code in that project demonstrates usage.

Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36