Class S2ClosestPointQuery<T>


  • @GwtCompatible
    public final class S2ClosestPointQuery<T>
    extends Object
    Given a set of points stored in an S2PointIndex, S2ClosestPointQuery provides methods that find the closest point(s) to a given query point.

    Example usage:

     void test(List points, List targets) {
       // The template argument allows auxiliary data to be attached to each point (in this case, the
       // array index).
       S2PointIndex index = new S2PointIndex<>();
       for (int i = 0; i < points.size(); i++) {
         index.add(points.get(i), i);
       }
       S2ClosestPointQuery query = new S2ClosestPointQuery<>(index);
       query.setMaxPoints(15);
       for (S2Point target : targets) {
         for (Result result : query.findClosestPoints(target)) {
           // result.entry().point() is one of the found closest points.
           // result.entry().data() is the auxiliary data (the "points" array index).
           // result.distance() is the distance to the target point.
           doSomething(target, result.entry().point(), result.entry().data(), result.distance());
         }
       }
     }

    You can find either the k closest points, or all points within a given radius, or both (i.e., the k closest points up to a given maximum radius). E.g. to find all the points within 5 kilometers, call query.setMaxDistance(S1Angle.fromEarthDistance(5000));.

    You can also restrict the results to an arbitrary S2Region via setRegion(S2Region).

    The implementation is designed to be very fast for both small and large point sets.

    This class is not thread-safe. In particular, setters must not be called during queries.

    • Constructor Detail

      • S2ClosestPointQuery

        public S2ClosestPointQuery​(S2PointIndex<T> index)
        Construct a new query for the given index. Must call reset() before using the query, if the index has been modified since the query was constructed.