Package com.google.common.geometry
Class S2PointIndex<Data>
- java.lang.Object
-
- com.google.common.geometry.S2PointIndex<Data>
-
@GwtCompatible public final class S2PointIndex<Data> extends java.lang.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 < points.size(); i++) { index.add(points.get(i), i); } S2ClosestPointQuery query = new S2ClosestPointQuery<>(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":
S2Iterator
> it = index.iterator(); it.seek(targetId.rangeMin()); for (; !it.done() && it.compareTo(targetId.rangeMax()) <= 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
S2PointIndex.Entry<Data>
An S2Iterator-compatible pair of S2Point with associated client data of a given type.
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<S2PointIndex.Entry<Data>>
entries
private boolean
sorted
-
Constructor Summary
Constructors Constructor Description S2PointIndex()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(S2PointIndex.Entry<Data> entry)
Adds a new entry to the index.void
add(S2Point point, Data data)
Asadd(Entry)
, but more convenient.static <Data> S2PointIndex.Entry<Data>
createEntry(S2Point point, Data data)
Convenience method to create an index entry from the given point and data value.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.int
numPoints()
Returns the number of points in the index.boolean
remove(S2PointIndex.Entry<Data> entry)
Removes the given entry from the index, and returns whether the given entry was present and removed.boolean
remove(S2Point point, Data data)
Asremove(Entry)
, but more convenient.void
reset()
Resets the index to its original empty state.
-
-
-
Field Detail
-
entries
private final java.util.List<S2PointIndex.Entry<Data>> entries
-
sorted
private boolean sorted
-
-
Method Detail
-
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)
Asadd(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)
Asremove(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.
-
-