public class RTree<T extends Shape>
extends java.lang.Object
implements java.lang.Iterable<T>, java.io.Closeable
bulkLoadWrite(byte[], int, int, int, DataOutput, boolean)
method. To restore the tree from disk, use the readFields(DataInput)
methods. To do queries against the tree, use the search(Shape, ResultCollector)
,
knn(double, double, int, ResultCollector2)
or
spatialJoin(RTree, RTree, ResultCollector2)
methods.Modifier and Type | Field and Description |
---|---|
static int |
NodeSize
Size of a node.
|
static int |
TreeHeaderSize
Size of tree header on disk.
|
Constructor and Description |
---|
RTree() |
Modifier and Type | Method and Description |
---|---|
void |
bulkLoadWrite(byte[] element_bytes,
int offset,
int len,
int degree,
java.io.DataOutput dataOut,
boolean fast_sort)
Builds the RTree given a serialized list of elements.
|
static int |
calculateStorageOverhead(int elementCount,
int degree)
Calculate the storage overhead required to build an RTree for the given
number of nodes.
|
static int |
calculateTreeStorage(int elementCount,
int degree) |
void |
close() |
static double |
fastLog(int x) |
static double |
fastPow(double a,
double b) |
static int |
findBestDegree(int bytesAvailable,
int recordCount)
Find the best (minimum) degree that can index the given number of records
such that the whole tree structure can be stored in the given bytes
available.
|
static int |
getBlockCapacity(long blockSize,
int degree,
int recordSize)
Given a block size, record size and a required tree degree, this function
calculates the maximum number of records that can be stored in this
block taking into consideration the overhead needed by node structure.
|
int |
getElementCount()
Returns total number of elements
|
static int |
getHeaderSize(java.io.DataInput in)
Returns the total size of the header (including the index) in bytes.
|
Rectangle |
getMBR()
Returns the MBR of the root
|
java.util.Iterator<T> |
iterator() |
int |
knn(double qx,
double qy,
int k,
ResultCollector2<T,java.lang.Double> output)
k nearest neighbor query
|
static int |
log2Floor(int x)
Find log to the base 2 quickly
|
static Rectangle[] |
packInRectangles(GridInfo gridInfo,
Point[] sample)
Create rectangles that together pack all points in sample such that
each rectangle contains roughly the same number of points.
|
static int |
powInt(int base,
int exponent) |
T |
readElement(int i)
Reads and returns the element with the given index
|
void |
readFields(java.io.DataInput in) |
int |
search(Shape query,
ResultCollector<T> output)
Performs a range query over this tree using the given query range.
|
protected int |
search(Shape query_shape,
ResultCollector<T> output,
int start,
int end)
Searches the RTree starting from the given start position.
|
void |
setStockObject(T stockObject) |
static int |
skipHeader(java.io.InputStream in)
Reads and skips the header of the tree returning the total number of
bytes skipped from the stream.
|
static int |
skipToEOL(byte[] bytes,
int startOffset)
Skip bytes until the end of line
|
static <S1 extends Shape,S2 extends Shape> |
spatialJoin(RTree<S1> R,
RTree<S2> S,
ResultCollector2<S1,S2> output) |
protected static <S1 extends Shape,S2 extends Shape> |
spatialJoinDisk(RTree<S1> R,
RTree<S2> S,
ResultCollector2<S1,S2> output)
Performs a spatial join between records in two R-trees
|
protected static <S1 extends Shape,S2 extends Shape> |
spatialJoinMemory(RTree<S1> R,
RTree<S2> S,
ResultCollector2<S1,S2> output) |
void |
write(java.io.DataOutput out) |
public static final int TreeHeaderSize
public static final int NodeSize
public void bulkLoadWrite(byte[] element_bytes, int offset, int len, int degree, java.io.DataOutput dataOut, boolean fast_sort)
TextSerializable.fromText(Text)
and build the tree. Also writes the
created tree to the disk directly.element_bytes
- - serialization of all elements separated by new linesoffset
- - offset of the first byte to use in elements_byteslen
- - number of bytes to use in elements_bytesdegree
- - Degree of the R-tree to build in terms of number of children per
nodedataOut
- - output stream to write the result to.fast_sort
- - setting this to true
allows the method to run
faster by materializing the offset of each element in the list
which speeds up the comparison. However, this requires an
additional 16 bytes per element. So, for each 1M elements, the
method will require an additional 16 M bytes (approximately).public void write(java.io.DataOutput out) throws java.io.IOException
java.io.IOException
public void readFields(java.io.DataInput in) throws java.io.IOException
java.io.IOException
public static int skipHeader(java.io.InputStream in) throws java.io.IOException
in
- java.io.IOException
public static int getHeaderSize(java.io.DataInput in) throws java.io.IOException
in
- java.io.IOException
public int getElementCount()
public Rectangle getMBR()
public T readElement(int i)
i
- java.io.IOException
public void setStockObject(T stockObject)
public static Rectangle[] packInRectangles(GridInfo gridInfo, Point[] sample)
samples
- gridInfo
- - Used as a hint for number of rectangles per row or columnpublic static int skipToEOL(byte[] bytes, int startOffset)
bytes
- startOffset
- public java.util.Iterator<T> iterator()
public static int getBlockCapacity(long blockSize, int degree, int recordSize)
blockSize
- degree
- recordSize
- protected int search(Shape query_shape, ResultCollector<T> output, int start, int end) throws java.io.IOException
query_mbr
- output
- start
- - where to start searchingend
- - where to end searching. Only used when start is an offset of
an object.java.io.IOException
public int search(Shape query, ResultCollector<T> output)
query
- - The query rectangle to use (TODO make it any shape not just rectangle)output
- - Shapes found are reported to this output. If null, results are not reportedpublic int knn(double qx, double qy, int k, ResultCollector2<T,java.lang.Double> output)
qx
- qy
- k
- output
- protected static <S1 extends Shape,S2 extends Shape> int spatialJoinMemory(RTree<S1> R, RTree<S2> S, ResultCollector2<S1,S2> output) throws java.io.IOException
java.io.IOException
protected static <S1 extends Shape,S2 extends Shape> int spatialJoinDisk(RTree<S1> R, RTree<S2> S, ResultCollector2<S1,S2> output) throws java.io.IOException
R
- S
- output
- java.io.IOException
- SuppresWarnings("resource") is used because we create LineReaders on the
internal data stream of both R and S. We do not want to close the
LineReader because it will subsequently close the internal data stream
of R and S which is something we want to avoid because both R and S are
not created by this function and it should not free these resources.public static <S1 extends Shape,S2 extends Shape> int spatialJoin(RTree<S1> R, RTree<S2> S, ResultCollector2<S1,S2> output) throws java.io.IOException
java.io.IOException
public static int calculateStorageOverhead(int elementCount, int degree)
public static int log2Floor(int x)
x
- public static int powInt(int base, int exponent)
public static double fastLog(int x)
public static double fastPow(double a, double b)
public static int findBestDegree(int bytesAvailable, int recordCount)
bytesAvailable
- recordCount
- public static int calculateTreeStorage(int elementCount, int degree)
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException