Question

I'm developing an application on app engine where I store location information in the datastore. I'm trying to fetch all the points (lat, long) within a specified radius of a given point. Similar to this StackOverflow question on SQL. I would like to know if the same can be achieved with GQL. (similar to this article I found)

Looking forward to your suggestions,

Was it helpful?

Solution

Not with GQL, but you can do this using the Search API, though this means you need to separately index your entities as searchable documents and is much more expensive as well ($0.06/100k for a datastore query versus $0.6/10k for a complex search query, which all geopoint search queries are).

EDIT TO ANSWER QUESTION IN THE COMMENT: It seems to me like the example does solve your problem:

"distance(survey_marker, geopoint(35.2, 40.5)) < 100"

Replace the 100 with 500 and the coordinates in geopoint(35.2, 40.5) with the coordinates of the center of your circle, and survey_marker with the name of your indexed GeoPt property.

So if you indexed your documents like this:

from google.appengine.api import search 

query = #something
index = search.Index(name="myIndex")
for entity in query:
    my_document = search.Document(
        doc_id = entity.key,
        fields=[
           search.GeoField(name='location', value=search.GeoPoint(entity.point))
           ])
    index.put(document)

You would write your query like so:

coords = (lat, long)
query_string = "distance(location, geopoint(%s, %s)) < 500" % (lat, long)
index.search(query.string)

Hope this helps.

OTHER TIPS

500m is pretty small! But at least it means that unless you are a polar explorer you can ignore the curvature of the earth. In such a case, you may want to break the GeoPt apart into two FloatProperty fields to make "plain" indexing easier to do -- you could then make a quick set of bpunding-box queries, lat greater-than and lat less-than, long greater-than and long less-than, to isolate most of the GeoPts you desire (pi/4's worth if things are generally evenly-distributed), then just filter the list yourself doing the appropriate trigonometry tests on each candidate point.

(I have never tried indexing GeoPts, maybe you can do the queries on the subfields directly)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top