Thanks to visit codestin.com
Credit goes to github.com

Skip to content
hadmir edited this page Oct 31, 2017 · 9 revisions

RTree java class is implementation of R-Tree spatial index for indexing rectangles and querying intersection of rectangles inside this index with any given rectangle. This implementation is specially suited for large datasets, where creating Java object for each and every indexed spatial object is not an option.

All indexed rectangles are stored in two int[] arrays, which is easy to persist (in file, for example), and also memory efficient.

We can insert geometry features (streets, parks, ...) using bounding rectangles of these features, into RTree, and then we can find all objects that intersects with given query rectangle.

RTree has two possible states, default is read/write mode, where you can both insert new rectangles and do queries. Second possibility is to turn RTree object into "compact" mode, which is read-only, you cannot insert new objects, but it is more compact in memory (and therefore also creates smaller file when RTree object is stored on disk).

Global rectangle:

Global rectangle defines global MIN and MAX coordinates of any object inside RTree. Coordinates outside these limits will be clipped when new objects are added to RTree. Global rectangle is defined in the beginning of Rtree.java class. By default is prepared for geographic coordinates (-180, 180) and (-90,90), multiplied by 1 million (integer):

final static public int GLOBAL_MIN_X = -180000000;
final static public int GLOBAL_MIN_Y = -90000000;
final static public int GLOBAL_MAX_X = 180000000;
final static public int GLOBAL_MAX_Y = 90000000;

API:

public RTree(int maxRectCount, int maxObjCount); Constructor, allocating given indexing tiles count and objects count. RTree is automatically increasing size of its arrays when needed, so you can start with (10000, 100000) size, for example.

public RTree(int[] rectsInp, int[] objsInp, boolean compactReadOnly); Constructor, for rtree stored in binary file. Binary file is read into rectsInp and objsInp arrays, while compactReadOnly parameter defines if RTree was stored in "compact" or "default" mode.

public int[] getRectArr(); public int getRectCount(); public int[] getObjArr(); public int getObjCount(); Returns both arrays needed, if you want to persist this RTree instance on disk. Later you can fill both int[] arrays from disk, and use appropriate RTree constructor to initialize object.

public void turnToCompactMode() Turns Rtree into read-only, "Compact" mode. After turning to compact mode, you cannot add more rectangles into Rtree, you can only do queries. Size of both allocated arrays is smaller, however, in compact mode. This is usefull for tasks where all objects are added to RTree in first phase, and later only queries are used.

public void addObj(int id, int minX, int minY, int maxX, int maxY) Adds single rectangle identified by id to RTree. Rectangle is defined by min/max coordinates. Coordinates are integers, so for geographic latitude and longitude coordinates, multiply values by 1 million (1000000). All coordinates must be inside "global rectangle" (see above).

public ArrayList<Integer> getIntersectingObjs(int minX, int minY, int maxX, int maxY, boolean removeDuplicates) Query function that returns ids of all objects intersecting or overlapping query rectangle. Parameter removeDuplicates defines if you want to remove duplicate occurences of same id in results (true), or caller will take care of it (false).

Clone this wiki locally