The Coder
The Coder

Reputation: 121

Indexing ndb.GeoPt Property in Google App Engine

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.

Upvotes: 0

Views: 455

Answers (1)

Paul Collingwood
Paul Collingwood

Reputation: 9116

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.

Upvotes: 2

Related Questions