rtree: Rectangle tree for fast point cloud data query¶
moraine.api.rtree (source). Every name is also moraine.<name> (import moraine as mr).
HilbertRtree
¶
This class provides a read-only Hilbert R-tree for spatial indexing.
See Hilbert_R-tree for more info on the Hilbert R-tree.
This implementation stores the R-tree as an array representation of a binary tree. See Binary_tree for more info on the array representation of a binary tree.
Note that HilbertRtree should not be instantiated directly,
but always through the module-level function build and load.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds_tree
|
ndarray
|
an array representation of a binary tree. shape [n_pages, 4]. Every row is a bounding box of a node: [x0, y0, xm, ym] |
required |
n_points
|
int
|
number of total points |
required |
page_size
|
default: 512
|
number of points in every leaf node. |
512
|
build
classmethod
¶
classmethod to build a HilbertRtree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x coordinates, e.g., lon, shape [n_points,] |
required |
y
|
ndarray
|
y coordinates, e.g., lat, shape [n_points,] |
required |
page_size
|
default: 512
|
number of points in every leaf node. should be same as pc_chunk_size. |
512
|
save
¶
Save the HilbertRtree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
zarr path |
required |
load
classmethod
¶
classmethod to load the saved HilbertRtree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zarr_path
|
str
|
zarr path |
required |
maybe_covered_ranges
¶
Get the ranges that may be within the query box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
tuple | list
|
data query bounding box, [x0, y0, xm, ym] |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
bbox_query
¶
get the index of points that are in the bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
tuple | list
|
data query bounding box, [x0, y0, xm, ym] |
required |
x
|
ndarray | Array
|
x coordinate |
required |
y
|
ndarray | Array
|
y coordinate |
required |
Examples¶
Run when this manual is built; the output follows each snippet.
import numpy as np
import moraine as mr
rng = np.random.default_rng(0)
x, y = rng.random(3000), rng.random(3000) # e.g. longitude, latitude of points
tree = mr.HilbertRtree.build(x, y, page_size=64) # page_size: points per leaf (the chunk size of the point cloud)
idx = tree.bbox_query([0.2, 0.2, 0.4, 0.4], x, y) # indices of the points in the box [x0, y0, xm, ym]
print(len(idx), idx[:5])
print(np.all((x[idx] >= 0.2) & (x[idx] <= 0.4) & (y[idx] >= 0.2) & (y[idx] <= 0.4)))
tree.save(path) / HilbertRtree.load(path) keep the tree as a zarr array next to a point cloud; the viewer uses
it to read only the points on screen.