What are the different kinds of geo spatial queries we can run against items in cache? To start, I need to find the closest item in cache to a given Point.
Yo!
GridGain (as well as H2 database does) supports operator && in SQL which means "intersection of bounding boxes". This operator is rather primitive but it can use geospatial indexes to work fast, so that any complex subsequent filtering can be done on a minor subset of entries.
As a reference you can use the following unit test.
In that unit test you will see the following entity:
private static class EnemyCamp implements Serializable {
/** */
@GridCacheQuerySqlField(index = true)
private Geometry coords;
/** */
@GridCacheQuerySqlField
private String name;
}
which is indexed by field "coords" of type com.vividsolutions.jts.geom.Geometry. We can put a number of such entities to a cache and query a list of entries whose coords (coords here is a point and thus it's bounding box is the same point) has intersection with bounding box of the given polygon.
GridCache<Integer, EnemyCamp> cache = ...;
WKTReader r = new WKTReader();
cache.put(0, new EnemyCamp(r.read("POINT(25 75)"), "A"));
cache.put(1, new EnemyCamp(r.read("POINT(70 70)"), "B"));
cache.put(2, new EnemyCamp(r.read("POINT(70 30)"), "C"));
cache.put(3, new EnemyCamp(r.read("POINT(75 25)"), "D"));
GridCacheQuery<Map.Entry<Integer, EnemyCamp>> qry = cache.queries().createSqlQuery(EnemyCamp.class,
"coords && ?");
Collection<Map.Entry<Integer, EnemyCamp>> res = qry.execute(r.read("POLYGON((5 70, 5 80, 30 80, 30 70, 5 70))")).get();
After we have result returned by this query we can filter it further in java code for example using method Geometry.isWithinDistance
Have fun!