Package com.google.common.geometry
Class S2PointIndex<Data>
java.lang.Object
com.google.common.geometry.S2PointIndex<Data>
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(Listpoints, 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
An S2Iterator-compatible pair of S2Point with associated client data of a given type. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(S2PointIndex.Entry<Data> entry) Adds a new entry to the index.void
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.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
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
Asremove(Entry)
, but more convenient.void
reset()
Resets the index to its original empty state.
-
Field Details
-
entries
-
sorted
private boolean sorted
-
-
Constructor Details
-
S2PointIndex
public S2PointIndex()
-
-
Method Details
-
numPoints
public int numPoints()Returns the number of points in the index. -
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
Asadd(Entry)
, but more convenient. -
add
Adds a new entry to the index. Invalidates all iterators; clients must create new ones. -
remove
Asremove(Entry)
, but more convenient. -
remove
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
Convenience method to create an index entry from the given point and data value.
-