Package ucar.nc2.dt.ugrid.rtree
Class RTree
java.lang.Object
ucar.nc2.dt.ugrid.rtree.RTree
- All Implemented Interfaces:
Serializable,SpatialIndex
This is a lightweight RTree implementation, specifically designed for the following features (in order of importance):
- Fast intersection query performance. To achieve this, the RTree uses only main memory to store entries. Obviously this will only improve performance if there is enough physical memory to avoid paging.
- Low memory requirements.
- Fast add performance.
The main reason for the high speed of this RTree implementation is the avoidance of the creation of unnecessary objects, mainly achieved by using primitive collections from the COLT library.
NOTE: Node.findEntry() is not implemented and recalculateMBR() is not implemented. These are used in node deletion, therefore nodes cannot be deleted at this time. 06-05-09- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic longstatic longstatic longstatic longstatic longstatic long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(LatLonPolygon2D r, int id) see com.asascience.data.rtree.SpatialIndex#add(Rectangle2D, int)cern.colt.list.IntArrayListConvenience method for contains search where the IntProcedure is handled internally.voidsee com.asascience.data.rtree.SpatialIndex#contains(LatLonRectangle2D, IntProcedure)booleandelete(LatLonPolygon2D r, int id) Deletes a polygon from the spatial indexsee com.asascience.data.rtree.SpatialIndex#getBounds()static doublegetEnlargementArea(double[] src, double[] addition) Produces a union of the two rectangles, and determines how muchsrcwould have to be enlarged to encompass this union.static doublegetEnlargementArea(LatLonRectangle2D src, LatLonRectangle2D addition) Produces a union of the two rectangles, and determines how muchsrcwould have to be enlarged to encompass this union.intGet the highest used node's ID.getNode(int index) Get a node object, given the ID of the node.intstatic StringBuildergetNodeMap(RTree rtree, int nodeId) Recursively builds a string representation of the given rtree starting at nodeId.static double[]getRectMax(LatLonRectangle2D rect) Builds a 2 length double array containing the maximum x and y values from the given rectangle.static double[]getRectMin(LatLonRectangle2D rect) Builds a 2 length double array containing the minimum x and y values from the given rectangle.intGet the root node's IDintGet the tree height (a.k.a.see com.asascience.data.rtree.SpatialIndex#getVersion()voidinit(Properties props) Initialize implementation dependent properties of the RTree.cern.colt.list.IntArrayListConvenience method for intersects search where the IntProcedure is handled internally.voidFinds all polygons that intersect the passed polygon.intintintConvenience method for nearestNeighbor search where the IntProcedure is handled internally.intnearest(LatLonPoint2D p, double searchRadius) Convenience method for nearestNeighbor search where the IntProcedure is handled internally.voidnearest(LatLonPoint2D p, IntProcedure v) Finds the entry (polygon) which is closest to the given point, and invokes apply() on the givenIntProcedurefor that entry.voidnearest(LatLonPoint2D p, IntProcedure v, double searchRadius) Finds the entry (polygon) which is closest to the given point, and invokes apply() on the givenIntProcedurefor that entry.voidnearestNeighbors(LatLonPoint2D p, IntProcedure v, double searchRadius) Finds the entries which are within the circular search area given by the pointpand the radiussearchRadius.Next invoke apply() on the givenIntProcedurefor each entry found.intsize()see com.asascience.data.rtree.SpatialIndex#size()toString()Constructs a string representation of thisRTreeas a tab-indented node list.static voidwriteFullNodeMap(BufferedWriter writer, RTree rtree, int nodeId)
-
Field Details
-
addTime
public static long addTime -
splitTime
public static long splitTime -
pickSeedsTime
public static long pickSeedsTime -
pickNextTime
public static long pickNextTime -
chooseTime
public static long chooseTime -
adjustTime
public static long adjustTime
-
-
Constructor Details
-
RTree
public RTree()Constructor. Use init() method to initialize parameters of the RTree. -
RTree
-
-
Method Details
-
init
Initialize implementation dependent properties of the RTree. Currently implemented properties are:
- MaxNodeEntries This specifies the maximum number of entries in a node. The default value is 10, which is used if the property is not specified, or is less than 2.
- MinNodeEntries This specifies the minimum number of entries in a node. The default value is half of the MaxNodeEntries value (rounded down), which is used if the property is not specified or is less than 1.
- Specified by:
initin interfaceSpatialIndex- Parameters:
props- The set of properties used to initialize the spatial index.
-
add
see com.asascience.data.rtree.SpatialIndex#add(Rectangle2D, int)- Specified by:
addin interfaceSpatialIndex- Parameters:
r- The polygon to add to the spatial index.id- The ID of the polygon to add to the spatial index. The result of adding more than one polygon with the same ID is undefined.
-
delete
Description copied from interface:SpatialIndexDeletes a polygon from the spatial index- Specified by:
deletein interfaceSpatialIndex- Parameters:
r- The polygon to delete from the spatial indexid- The ID of the polygon to delete from the spatial index- Returns:
trueif the polygon was deleted,falseif the polygon was not found, or the polygon was found but with a different ID
-
nearest
-
nearest
Convenience method for nearestNeighbor search where the IntProcedure is handled internally. Uses the passed search radius.- Parameters:
p- the query point.searchRadius- the searchRadius to use in the units of the loaded polygons- Returns:
- the index of the nearest neighbor to the input point. returns -1 if no points are found within the searchRadius.
-
nearest
-
nearest
Convenience method for nearestNeighbor search where the IntProcedure is handled internally. Uses the default search radius of 0.5 units.- Parameters:
p- the query point.- Returns:
- the index of the nearest neighbor to the input point. returns -1 if no points are found within the search radius.
-
nearest
Finds the entry (polygon) which is closest to the given point, and invokes apply() on the givenIntProcedurefor that entry. Any entries found with a distance greater than 0.5 will not be returned. Also, if two or more entries are equidistant from the given point,apply()will only be invoked for the point closest by centroid.- Parameters:
p- The point for which this method finds the nearest neighborsv- The IntProcedure whose execute() method is is called for each nearest neighbor.searchRadius- The furthest distance away from a polygon to search. Polygons further than this will not be found. This should be as small as possible to minimize the search time. Use Double.POSITIVE_INFINITY to guarantee that the nearest polygon is found, no matter how far away, although this will slow down the algorithm.
-
nearest
Finds the entry (polygon) which is closest to the given point, and invokes apply() on the givenIntProcedurefor that entry. Any entries found with a distance greater than 0.5 will not be returned. Also, if two or more entries are equidistant from the given point,apply()will only be invoked for the point closest by centroid.- Parameters:
p- The point for which this method finds the nearest neighborsv- The IntProcedure whose execute() method is is called for each nearest neighbor.
-
intersects
Convenience method for intersects search where the IntProcedure is handled internally. Returns only those cells that are completely contained within therpolygon.- Parameters:
r- the bounding polygon to search within- Returns:
- a list of indexes partially or completely contained within the bounding polygon
-
intersects
Description copied from interface:SpatialIndexFinds all polygons that intersect the passed polygon.- Specified by:
intersectsin interfaceSpatialIndex- Parameters:
r- The polygon for which this method finds intersecting polygons.v- The IntProcedure whose execute() method is is called for each intersecting polygon.
-
contains
Convenience method for contains search where the IntProcedure is handled internally. Returns only those cells that are completely contained within therpolygon.- Parameters:
r- the bounding polygon to search within- Returns:
- a list of indexes completely contained within the bounding polygon
-
contains
see com.asascience.data.rtree.SpatialIndex#contains(LatLonRectangle2D, IntProcedure)- Specified by:
containsin interfaceSpatialIndex- Parameters:
r- The polygon for which this method finds contained polygons.v- The visitor whose visit() method is is called for each contained polygon.
-
size
public int size()see com.asascience.data.rtree.SpatialIndex#size()- Specified by:
sizein interfaceSpatialIndex
-
getBounds
see com.asascience.data.rtree.SpatialIndex#getBounds()- Specified by:
getBoundsin interfaceSpatialIndex
-
getVersion
see com.asascience.data.rtree.SpatialIndex#getVersion()- Specified by:
getVersionin interfaceSpatialIndex
-
getTreeHeight
public int getTreeHeight()Get the tree height (a.k.a. # of levels)- Returns:
- the height of the tree
-
getNode
Get a node object, given the ID of the node. -
getNodeCount
public int getNodeCount()- Returns:
- Get the number of nodes in this RTree. This is not the total number of entries. Entry total is obtained via
size()
-
getHighestUsedNodeId
public int getHighestUsedNodeId()Get the highest used node's ID. This may not be the same number as size(), since node deletion does not reorient the remaining nodes. -
getRootNodeId
public int getRootNodeId()Get the root node's ID -
nearestNeighbors
Finds the entries which are within the circular search area given by the pointpand the radiussearchRadius.Next invoke apply() on the givenIntProcedurefor each entry found.- Specified by:
nearestNeighborsin interfaceSpatialIndex- Parameters:
p- The point for which this method finds the nearest neighborsv- The IntProcedure whose execute() method is is called for each nearest neighbor.searchRadius- The distance frompto look for entries
-
getRectMin
Builds a 2 length double array containing the minimum x and y values from the given rectangle. The 0 index of this array contains the minimum x value, and the 1 index contains the minimum y value.- Parameters:
rect- a LatLonRectangle2D object- Returns:
- a 2 length
doublearray, containing x and y coordinates.
-
getRectMax
Builds a 2 length double array containing the maximum x and y values from the given rectangle. The 0 index of this array contains the max x value, and the 1 index contains the max y value.- Parameters:
rect- a LatLonRectangle2D object- Returns:
- a 2 length
doublearray, containing x and y coordinates.
-
getEnlargementArea
Produces a union of the two rectangles, and determines how muchsrcwould have to be enlarged to encompass this union. The original source rectangles will remain unchanged.
Ifsrcfully containsadditionthis will always return 0, sincesrcwould not have to be enlarged.- Parameters:
src- a source rectangleaddition- the rectangle which will be added to the source- Returns:
- the area which
src1would have to be enlarged to, in order to encompass the union of itself and another rectangle (src2)
-
getEnlargementArea
public static double getEnlargementArea(double[] src, double[] addition) Produces a union of the two rectangles, and determines how muchsrcwould have to be enlarged to encompass this union. The original source rectangles will remain unchanged.
Ifsrcfully containsadditionthis will always return 0, sincesrcwould not have to be enlarged.- Parameters:
src- a source rectangleaddition- the rectangle which will be added to the source- Returns:
- the area which
src1would have to be enlarged to, in order to encompass the union of itself and another rectangle (src2)
-
toString
Constructs a string representation of thisRTreeas a tab-indented node list. If thisRTreecontains many entries this string may be rather long. -
getNodeMap
Recursively builds a string representation of the given rtree starting at nodeId. Starting at node id rtree.getRootNodeId() is recommended.- Parameters:
rtree- the rtree to traversenodeId- the starting node for the tree building- Returns:
- a string representation of the given rtree
-
writeFullNodeMap
-