2

I am having the hardest time querying a GeoPt field with Objectify.

I made sure there's an @Index annotation about the GeoPt field.

However, the statement below doesn't seem to work

ofy().load()
     .type(GeoStat.class)
     .filter("geoPt.latitude >=", neLat)
     .limit(10);

Is querying GeoPt field possible in Objectify?

Johnny Wu
  • 1,297
  • 15
  • 31

1 Answers1

5

The datastore does not support querying a GeoPt in this way. If you wish to query on latitude, index it separately (possibly writing to a private field in an @OnSave method). However, if you're trying to perform a geospatial query (ie, contained in a rectangle), note that you cannot perform two inequality filters (latitude in bounds, longitude in bounds).

The good news is that very recently google added the ability to perform geospatial queries directly to the datastore:

https://cloud.google.com/appengine/docs/java/datastore/geosearch

GeoPt center = new GeoPt(latitude, longitude);
double radius = valueInMeters;
Filter f = new StContainsFilter("geoPt", new Circle(center, radius));
ofy().load().type(GeoStat.class).filter(f);

Note, that geospatial search for datastore is currently an Alpha release, that is closed for invitations. So, while you can get the geospatial search to work locally, it still cannot be used in production. See: Google App Engine › GeoSpatial Alpha Invite

stickfigure
  • 13,458
  • 5
  • 34
  • 50
  • This looks like it should work, but Android Studio "cannot resolve StContainsFilter" – learner Nov 28 '15 at 15:33
  • I don't know which version your App Engine plugin you are using learner, but I was with the version 1.9.18 and Android Studio was not resolving the StContaisFilter. I updated it to `dependencies { classpath 'com.google.appengine:gradle-appengine-plugin:1.9.30' }` and it worked. Go to your backend gradle and check your version. – jluiz20 Feb 03 '16 at 02:07
  • I get an error on the .filter() method: ofy().load().type(GeoStat.class).filter(f) - filter() takes a String, did you mean filterKey(Object value)? – Alexander Ciesielski Feb 05 '16 at 18:06
  • This is the overload you are looking for: `public Query filter(Filter filter);` – stickfigure Feb 09 '16 at 20:09
  • Did geosearch with datastore die or something? I can't find it in the documentation anymore. The link you posted in your answer doesn't contain it anymore. Just wondering if there is any update on this since it has been a year. – Micro Dec 12 '16 at 19:36