The documentation for Google Cloud Datastore details the Geographical point data type [1]. While there is no strictly corresponding class in the Java library, there is a LatLng
class [2] that seems to be the canonical equivalent.
While I can store the value just fine, and I can query for exact locations by supplying a fully constructed LatLng
instance like this:
ofy.load().type(Site.class) // Site is a custom class, whose latLng field is a LatLng-type
.filter("latLng", LatLng.of(26.329,127.744))
.first()
.now()
I can't seem to do inexact queries natively, at least not on the Console. Though I can kind of do something like it in Objectify:
ofy.load().type(Site.class)
.filter("latLng >", LatLng.of(26.0,127.0))
.filter("latLng <", LatLng.of(90,127.750))
.first()
.now()
My question is: is there native/first-class support for geospatial queries in the Datastore? Or is this sort of coordinate geometry-"hackery" the only accepted way of doing such searches/queries?
[1] https://cloud.google.com/datastore/docs/concepts/entities#geographical_point [2] https://googleapis.dev/java/google-cloud-clients/0.111.0-alpha/com/google/cloud/datastore/LatLng.html
So it looks like Datastore does, in fact, handle the LatLng
type natively, allowing you to do inexact queries over it. All the various restrictions of inexact queries apply (there can be only one inexactly-queried attribute, it must be indexed, etc.), and the index size for the type (16 bytes) suggests an internal representation of a pair of double
s, and experimentation seems to suggest the query operates by filtering latitudes first, then longitudes.
However, it is currently handled incorrectly, and a bounding box-query, as the one in the opening question, also returns points that satisfy only one of the constraints (in my case, the latitude of some points satisfied the constraint but not the longitude).
I've reported the issue to Google via the issue tracker, and they have reproduced it internally, forwarding it to the Datastore team for resolution. I will update this answer once I hear back from them.