2

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,

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
sengar19
  • 45
  • 1
  • 5

2 Answers2

4

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.

Roman Levin
  • 461
  • 3
  • 18
  • thanks @Roman Levin, can search api may be used to achive this particular radius functionality: [link](http://housing.co.in/bangalore/buy/flats-near=koramangala:12.931656:77.622696:2900;apartment-types=3bhk;sort-by=distance:true?gclid=CPeg6YOs0rgCFfF34godVFcAEA) . I am storing particular coordinates in datastore which will be mapped on map according to the user's geolocation under 500 meter radius and the radius is static unlike the above link where it is dynamic...thanks again man :) .. looking forward for ur reply :) – sengar19 Aug 29 '13 at 17:03
  • Thanks man ,,, now i got the clear picture, it was very helpful :) – sengar19 Sep 01 '13 at 17:10
1

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)

bjorke
  • 3,295
  • 1
  • 16
  • 20
  • ya dude i tried that but i am getting the circumference coordinates of the circle with midpoint as my location... but thanks for answering man,,, :) – sengar19 Sep 01 '13 at 17:14