Search code examples
javagoogle-app-engineobjectifygeopoints

Querying Geopt with Objectify


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?


Solution

  • 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