T
- the type of object stored in the tree.public interface RTreeInterface<T extends RNode>
extends java.util.Collection<T>
Comparators taking an RNode will be called both with the user's data and with internal nodes of the
RTree. You can use instanceof
to discover whether a node is data and do different processing, as
long as that processing is consistent with the required property of a distance metric.
The merFilter predicate used in various methods must return true for the desired data and any internal node containing your desired data. Ensure that your merFilter returns true for any larger Mer that contains your desired Mers. The two most used merFilter predicates for a given targetMer are:
x -> x.interacts(targetMer)
to find any object that might interact with targetMer
x -> x.covers(targetMer)
to find an object whose mer is targetMer, or which interacts with all of a
target object.
Multiples calls to getMer() on an object must return the same Mer for as long as the object is in the tree.
Modifier and Type | Method and Description |
---|---|
boolean |
add(T node)
Insert a new node into an RTree.
|
default void |
forEach(Mer targetMer,
java.util.function.Consumer<? super T> action)
The specified action is applied to every node that touches the targetMer.
|
void |
forEach(java.util.function.Predicate<? super Mer> merFilter,
java.util.function.Consumer<? super T> action)
The specified action is applied to every node that touches the search mer.
|
default <Q extends RNode> |
join(RTreeInterface<Q> rtree1)
Do a join of this RTree with rtree1.
|
default <Q extends RNode> |
join(RTreeInterface<Q> rtree1,
Mer targetMer)
Do a join of this RTree with rtree1.
|
<Q extends RNode> |
join(RTreeInterface<Q> rtree1,
java.util.function.Predicate<? super Mer> merFilter)
Do a join of this RTree with rtree1.
|
default boolean |
removeIf(Mer targetMer,
java.util.function.Predicate<? super T> filter)
Remove all nodes touching targetMer for which filter is true.
|
boolean |
removeIf(java.util.function.Predicate<? super Mer> merFilter,
java.util.function.Predicate<? super T> filter)
Remove all nodes for which merFilter and filter are both true.
|
java.util.stream.Stream<T> |
search(java.util.Comparator<? super RNode> compareFunc)
Search the tree (Mers) in the order specified by the comparator.
|
default java.util.stream.Stream<T> |
search(JGeometry targetGeom,
java.lang.Class<T> classT,
java.util.function.Function<? super T,JGeometry> geomField)
Assuming each node contains a JGeometry, this method returns the data in this tree ordered by proximity
to a target JGeometry.
|
java.util.stream.Stream<T> |
search(java.util.function.ToDoubleFunction<? super RNode> sortMetric)
Search the tree of Mers in the order specified by the metric computed on the Mers of the tree.
|
default java.util.stream.Stream<T> |
searchMer(Mer targetMer)
Return all the objects which touch the search mer.
|
java.util.stream.Stream<T> |
searchMer(java.util.function.Predicate<? super Mer> merFilter)
Return all the objects which satisfy the merFilter.
|
T |
searchNearest(java.util.Comparator<? super RNode> compareFunc)
Return the first data element as specified by the comparator.
|
default T |
searchNearest(JGeometry targetGeom,
java.lang.Class<T> classT,
java.util.function.Function<? super T,JGeometry> geomField)
Assuming each node contains a JGeometry, this method returns the closest node to a target JGeometry
|
T |
searchNearest(java.util.function.ToDoubleFunction<? super RNode> sortMetric)
Search the tree of Mers in the order specified by the metric computed on the Mers of the tree and return
the single smallest item.
|
int |
size()
Gives the size of the tree.
|
boolean add(T node) throws java.lang.UnsupportedOperationException
Note that node.getMer() will be called repeatedly during RTree operations and should be efficient, typically implemented by the node caching a Mer which it returns on each call.
node.isData() must return true.
void forEach(java.util.function.Predicate<? super Mer> merFilter, java.util.function.Consumer<? super T> action)
merFilter
- selects the objects to be processed.action
- the action to apply objects of interest.default void forEach(Mer targetMer, java.util.function.Consumer<? super T> action)
targetMer
- selects the region where the forEach will be appliedaction
- the action to apply objects of interest.boolean removeIf(java.util.function.Predicate<? super Mer> merFilter, java.util.function.Predicate<? super T> filter) throws java.lang.UnsupportedOperationException
Most users should instead use the simpler removeIf(Mer, Predicate).
For example, if tree is an RTree<JGeometry>
, we could remove all geometries smaller than 1000 m^2 that are
completely contained in the Western hemisphere:
Mer westHemi = new Mer(2);
westHemi.extend(-180, -90);
westHemi.extend( 0, +90);
tree.removeIf(x -> westHemi.interacts(x), x -> westHemi.covers(x) && x.area(0.001) < 1000);
Note that westHemi.covers(x)
violates the merFilter requirements and must be put in the filter predicate
instead.
merFilter
- restricts the search areafilter
- the test for determining whether to remove a nodejava.lang.UnsupportedOperationException
- if remove is not supporteddefault boolean removeIf(Mer targetMer, java.util.function.Predicate<? super T> filter) throws java.lang.UnsupportedOperationException
targetMer
- restricts the search areafilter
- the test for determining whether to remove a nodejava.lang.UnsupportedOperationException
- if remove is not supportedint size()
java.util.stream.Stream<T> searchMer(java.util.function.Predicate<? super Mer> merFilter)
List<T> touches = tree.searchmer(x -> targetMer.interacts(x)).collect(Collectors.toList());
merFilter
- the search mermerFilter
- selector for the desired area of the treedefault java.util.stream.Stream<T> searchMer(Mer targetMer)
targetMer
- the search merjava.util.stream.Stream<T> search(java.util.Comparator<? super RNode> compareFunc)
To order the priority queue, compareFunc will be called multiple times per node visited. If your
comparison function is expensive, consider using search(ToDoubleFunction)
instead.
compareFunc
- indicates which Mer or leaf is closer to the target. See note on distance metric in
the javadoc for this interface.default java.util.stream.Stream<T> search(JGeometry targetGeom, java.lang.Class<T> classT, java.util.function.Function<? super T,JGeometry> geomField)
Example usage: Print the five customers closest to (85, 45) class CustomerData implements RNode {
private String label;
private JGeometry geom;
}
RTreeInterface<CustomerData> customerRTree = new RStarTree<>(2, Arrays.stream(customerList));
customerRTree.search(new JGeometry(85, 45, 0), CustomerData.class, node -> node.geom).limit(5)
.forEach(cust -> System.out.println(cust.label));
targetGeom
- the target of the searchclassT
- Specifies the concrete class of the type in the tree.geomField
- A functiont that returns the JGeometry from the stored datatype.default T searchNearest(JGeometry targetGeom, java.lang.Class<T> classT, java.util.function.Function<? super T,JGeometry> geomField)
Example usage: Print the customer closest to (85, 45) class CustomerData implements RNode {
private String label;
private JGeometry geom;
}
RTreeInterface<CustomerData> customerRTree = new RStarTree<>(2, Arrays.stream(customerList));
System.out.println(customerRTree.searchNearest(new JGeometry(85, 45, 0), CustomerData.class, node -> node.geom));
targetGeom
- the target of the searchclassT
- Specifies the concrete class of the type in the tree.geomField
- A functiont that returns the JGeometry from the stored datatype.T searchNearest(java.util.Comparator<? super RNode> compareFunc)
To order the priority queue, compareFunc will be called multiple times per node visited. If your
comparison function is expensive, consider using searchNearest(ToDoubleFunction)
instead.
compareFunc
- indicates which Mer or leaf is closest to the target. See note on distance metric
in the javadoc for this interface.java.util.stream.Stream<T> search(java.util.function.ToDoubleFunction<? super RNode> sortMetric)
Distances of Double.POSITIVE_INFINITY will not be searched or returned. Data nodes of distance Double.NEGATIVE_INFINITY are returned immediately as they are found and may reduce the search effort.
sortMetric
- indicates the "distance" to the target as a double value. See note on distance metric
in the javadoc for this interface.T searchNearest(java.util.function.ToDoubleFunction<? super RNode> sortMetric)
Distances of Double.POSITIVE_INFINITY will not be searched or returned. A data node with distance Double.NEGATIVE_INFINITY will be returned immediately if found.
sortMetric
- indicates the "distance" to the target as a double value. See note on distance metric
in the javadoc for this interface.<Q extends RNode> java.util.stream.Stream<Pair<T,Q>> join(RTreeInterface<Q> rtree1, java.util.function.Predicate<? super Mer> merFilter) throws java.lang.UnsupportedOperationException, java.lang.ClassCastException
Q
- the type of the joined rtreertree1
- the tree to joinmerFilter
- restricts the area of the joinjava.lang.UnsupportedOperationException
- if unable to perform the requested type of join.java.lang.ClassCastException
- if rtree1 is not same type as this RTreeInterfacedefault <Q extends RNode> java.util.stream.Stream<Pair<T,Q>> join(RTreeInterface<Q> rtree1, Mer targetMer) throws java.lang.UnsupportedOperationException, java.lang.ClassCastException
Q
- the type of the joined rtreertree1
- the tree to jointargetMer
- the region to joinjava.lang.UnsupportedOperationException
- if unable to perform the requested type of join.java.lang.ClassCastException
- if rtree1 is not same type as this RTreeInterfacedefault <Q extends RNode> java.util.stream.Stream<Pair<T,Q>> join(RTreeInterface<Q> rtree1) throws java.lang.UnsupportedOperationException, java.lang.ClassCastException
Q
- the type of the joined rtreertree1
- the tree to joinjava.lang.UnsupportedOperationException
- if unable to perform the requested type of join.java.lang.ClassCastException
- if rtree1 is not same type as this RTreeInterface