Class S2PointIndex<Data>

java.lang.Object
com.google.common.geometry.S2PointIndex<Data>

@GwtCompatible public final class S2PointIndex<Data> extends Object
S2PointIndex maintains an index of points sorted by leaf S2CellId. Each point has some associated client-supplied data, such as an index or object the point was taken from, useful to map query results back to another data structure.

The class supports adding or removing points dynamically, and provides a seekable iterator interface for navigating the index.

You can use this class in conjunction with S2ClosestPointQuery to find the closest index points to a given query point. For example:

 void test(List points, S2Point target) {
   // The generic type allows auxiliary data to be attached to each point
   // In this case, attach the original index of the point.
   S2PointIndex index = new S2PointIndex();
   for (int i = 0; i invalid input: '<' points.size(); i++) {
     index.add(points.get(i), i);
   }
   S2ClosestPointQuery query = new S2ClosestPointQueryinvalid input: '<'>(index);
   query.findClosestPoint(target);
   if (query.num_points() > 0) {
     // query.point(0) is the closest point (result 0).
     // query.distance(0) is the distance to the target.
     // query.data(0) is the auxiliary data (the array index set above).
     doSomething(query.point(0), query.data(0), query.distance(0));
   }
 }
 

Alternatively, you can access the index directly using the iterator interface. For example, here is how to iterate through all the points in a given S2CellId "targetId":

 S2Iteratorinvalid input: '<'S2PointIndex.Entry> it = index.iterator();
 it.seek(targetId.rangeMin());
 for (; !it.done() invalid input: '&'invalid input: '&' it.compareTo(targetId.rangeMax()) invalid input: '<'= 0; it.next()) {
   doSomething(it.entry());
 }
 

Points can be added or removed from the index at any time by calling add() or remove(), but doing so invalidates existing iterators. New iterators must be created.

This class is not thread-safe.

  • Field Details

  • Constructor Details

    • S2PointIndex

      public S2PointIndex()
  • Method Details

    • numPoints

      public int numPoints()
      Returns the number of points in the index.
    • iterator

      public S2Iterator<S2PointIndex.Entry<Data>> iterator()
      Returns a new iterator over the cells of this index, after sorting entries by cell ID if any modifications have been made since the last iterator was created.
    • add

      public void add(S2Point point, Data data)
      As add(Entry), but more convenient.
    • add

      public void add(S2PointIndex.Entry<Data> entry)
      Adds a new entry to the index. Invalidates all iterators; clients must create new ones.
    • remove

      public boolean remove(S2Point point, Data data)
      As remove(Entry), but more convenient.
    • remove

      public boolean remove(S2PointIndex.Entry<Data> entry)
      Removes the given entry from the index, and returns whether the given entry was present and removed. Both the "point" and "data" fields must match the point to be removed. Invalidates all iterators; clients must create new ones.
    • reset

      public void reset()
      Resets the index to its original empty state. Invalidates all iterators; clients must create new ones.
    • createEntry

      public static <Data> S2PointIndex.Entry<Data> createEntry(S2Point point, Data data)
      Convenience method to create an index entry from the given point and data value.