Class RTree<T>

  • Type Parameters:
    T - Value type.
    All Implemented Interfaces:
    Index<T>, RectangleIndex<T>, RectangleIndexMM<T>

    public class RTree<T>
    extends Object
    implements RectangleIndex<T>, RectangleIndexMM<T>
    R*Tree implementation based on the paper from Beckmann, N.; Kriegel, H. P.; Schneider, R.; Seeger, B. (1990). "The R*-tree: an efficient and robust access method for points and rectangles". Revised R*Tree (for DBMS, see conclusion?) -- RR*Tree "A Revised R*-tree in Comparison with Related Index Structures" Norbert Beckmann; Bernhard Seeger Interesting comparison: http://www.boost.org/doc/libs/1_58_0/libs/geometry/doc/html/geometry/spatial_indexes/introduction.html
    Author:
    ztilmann
    • Constructor Detail

      • RTree

        protected RTree​(int dims)
        Create an RTree. By default it is an R*tree.
        Parameters:
        dims - dimensionality
    • Method Detail

      • createRStar

        public static <T> RTree<T> createRStar​(int dims)
      • getDims

        public int getDims()
        Specified by:
        getDims in interface Index<T>
        Returns:
        the number of dimensions
      • size

        public int size()
        Specified by:
        size in interface Index<T>
        Returns:
        the number of entries
      • clear

        public void clear()
        Description copied from interface: Index
        Clear all entries.
        Specified by:
        clear in interface Index<T>
      • insert

        public void insert​(double[] point,
                           T value)
      • insert

        public void insert​(double[] keyMin,
                           double[] keyMax,
                           T value)
        Insert a rectangle.
        Specified by:
        insert in interface RectangleIndex<T>
        Specified by:
        insert in interface RectangleIndexMM<T>
        Parameters:
        keyMin - min
        keyMax - max
        value - value
      • insert

        public void insert​(Entry<T> e)
        Insert an entry.
        Parameters:
        e - the entry
      • load

        public void load​(Entry<T>[] entries)
      • remove

        public Object remove​(double[] point)
      • remove

        public T remove​(double[] min,
                        double[] max)
        Remove an entry.
        Specified by:
        remove in interface RectangleIndex<T>
        Parameters:
        min - min
        max - max
        Returns:
        the value of the entry or null if the entry was not found
      • remove

        public boolean remove​(double[] min,
                              double[] max,
                              T value)
        Remove an entry.
        Specified by:
        remove in interface RectangleIndexMM<T>
        Parameters:
        min - min
        max - max
        value - value
        Returns:
        the value of the entry or null if the entry was not found
      • removeIf

        public boolean removeIf​(double[] min,
                                double[] max,
                                Predicate<RectangleEntry<T>> condition)
        Remove the first entry matching the geometry and condition.
        Specified by:
        removeIf in interface RectangleIndexMM<T>
        Parameters:
        min - min
        max - max
        condition - Condition for deletion.
        Returns:
        `true` iff an entry was found and removed
      • update

        public T update​(double[] lo1,
                        double[] up1,
                        double[] lo2,
                        double[] up2)
        Update the position of an entry.
        Specified by:
        update in interface RectangleIndex<T>
        Parameters:
        lo1 - old min
        up1 - old max
        lo2 - new min
        up2 - new max
        Returns:
        the value, or null if the entries was not found
      • update

        public boolean update​(double[] lo1,
                              double[] up1,
                              double[] lo2,
                              double[] up2,
                              T value)
        Update the position of an entry.
        Specified by:
        update in interface RectangleIndexMM<T>
        Parameters:
        lo1 - old min
        up1 - old max
        lo2 - new min
        up2 - new max
        value - only entry with the given value is updated
        Returns:
        `true` iff the entry was found and relocated
      • contains

        public boolean contains​(double[] min,
                                double[] max,
                                T value)
        Description copied from interface: RectangleIndexMM
        Lookup an entry, using exact match.
        Specified by:
        contains in interface RectangleIndexMM<T>
        Parameters:
        min - minimum corner
        max - maximum corner
        value - the value
        Returns:
        `true` if an entry was found, otherwise `false`.
      • queryExact

        public T queryExact​(double[] min,
                            double[] max)
        Description copied from interface: RectangleIndex
        Lookup an entry, using exact match.
        Specified by:
        queryExact in interface RectangleIndex<T>
        Parameters:
        min - minimum corner
        max - maximum corner
        Returns:
        the value of the entry or null if the entry was not found
      • queryRectangle

        public RTreeIterator<T> queryRectangle​(double[] min,
                                               double[] max)
        Description copied from interface: RectangleIndexMM
        Lookup an entry, using exact match.
        Specified by:
        queryRectangle in interface RectangleIndexMM<T>
        Parameters:
        min - minimum corner
        max - maximum corner
        Returns:
        an iterator over all entries at with the exact given rectangle
      • queryIntersect

        public RTreeIterator<T> queryIntersect​(double[] min,
                                               double[] max)
        Specified by:
        queryIntersect in interface RectangleIndex<T>
        Specified by:
        queryIntersect in interface RectangleIndexMM<T>
        Parameters:
        min - Lower left corner of the query window
        max - Upper right corner of the query window
        Returns:
        All rectangles that intersect with the query rectangle.
      • query1NN

        public RectangleEntryDist<T> query1NN​(double[] center)
        Description copied from interface: RectangleIndex
        Finds the nearest neighbor. This uses euclidean 'edge distance'. Other distance types can only be specified directly on the index implementations.
        Specified by:
        query1NN in interface RectangleIndex<T>
        Specified by:
        query1NN in interface RectangleIndexMM<T>
        Parameters:
        center - center point
        Returns:
        the nearest neighbor
      • queryKNN

        public RTreeQueryKnn2<T> queryKNN​(double[] center,
                                          int k)
        Description copied from interface: RectangleIndex
        Finds the nearest neighbor. This uses euclidean 'edge distance', i.e. the distance to the edge of rectangle. Distance is 0 is the rectangle overlaps with the search point. Other distance types can only be specified directly on the index implementations.
        Specified by:
        queryKNN in interface RectangleIndex<T>
        Specified by:
        queryKNN in interface RectangleIndexMM<T>
        Parameters:
        center - center point
        k - number of neighbors
        Returns:
        list of nearest neighbors
      • queryKNN

        public RTreeQueryKnn2<T> queryKNN​(double[] center,
                                          int k,
                                          RectangleDistanceFunction dist)
        Description copied from interface: RectangleIndexMM
        Finds the nearest neighbor. This uses a custom distance function for distances to rectangles.
        Specified by:
        queryKNN in interface RectangleIndexMM<T>
        Parameters:
        center - center point
        k - number of neighbors
        dist - distance function
        Returns:
        list of nearest neighbors
      • queryRangedNearestNeighbor

        public Iterable<RectangleEntryDist<T>> queryRangedNearestNeighbor​(double[] center,
                                                                          RectangleDistanceFunction dist,
                                                                          RectangleDistanceFunction closestDist,
                                                                          Filter filter)
        This method returns an Iterable which returns the nodes by a combined range and nearest number search. The Iterator supports the Iterator.remove() method.
        Parameters:
        center - Target position passed as parameter to the distance functions. Can be null if your distance function supports it (like RectangleDistanceFunction.RectangleDist).
        dist - Distance function used to compare entries (example: RectangleDistanceFunction.EDGE or RectangleDistanceFunction.CENTER)
        closestDist - Distance of the best point in a given rectangle (example: RectangleDistanceFunction.EDGE but *not* RectangleDistanceFunction.CENTER)
        filter - Filter to limit the results for range queries (example: new Filter.RectangleIntersectFilter(min, max))
        Returns:
        An Iterable which lazily calculates the nearest neighbors.
      • toStringTree

        public String toStringTree()
        Specified by:
        toStringTree in interface Index<T>
        Returns:
        a full string output of the tree structure with all entries
      • getStats

        public RTree.RTreeStats getStats()
        Specified by:
        getStats in interface Index<T>
        Returns:
        Collect and return some index statistics. Note that indexes are not required to fill all fields. Also, individual indexes may use subclasses with additional fields.
      • getDepth

        public int getDepth()
        Specified by:
        getDepth in interface Index<T>
      • getNodeCount

        public int getNodeCount()
        Specified by:
        getNodeCount in interface Index<T>
      • getRoot

        protected org.tinspin.index.rtree.RTreeNode<T> getRoot()