Sign in

    #@totto2727/geo/rtree

    totto2727/geo/rtree owns the R-tree spatial index for values associated with two-dimensional bounding rectangles, including bulk loading, mutation, rectangle and nearest queries, iteration, and draining.

    #Usage

    Index service areas and return the depot closest to a delivery coordinate:

    ///|
    test "find the nearest depot" {
    let depots = [
    Entry::Entry(
    @type.Rect::Rect(@type.Coord(0.0, 0.0), @type.Coord(1.0, 1.0)),
    "west depot",
    ),
    Entry::Entry(
    @type.Rect::Rect(@type.Coord(10.0, 10.0), @type.Coord(11.0, 11.0)),
    "east depot",
    ),
    ]
    let index = RTree::bulk_load(depots)
    @test.assert_eq(
    index.query_nearest(@type.Coord(9.5, 9.5)),
    Some("east depot"),
    )
    }

    #API

    This README was generated from the share-artifact skill and README template.

    Entry

    pub(all) struct Entry[T] {
    bbox :
    Rect

    value : T
    }

    One entry in an RTree: a bounding rectangle and an arbitrary value.

    Entry::Entry

    fn[T] Entry::Entry(bbox :
    Rect
    , value : T) -> Entry[T]

    Node

    type Node[T]

    RTree

    pub struct RTree[T] {
    root : Node[T]?
    size : Int
    }

    A 2D R-tree. Build with RTree::bulk_load(entries) for static data, or start empty with RTree::new() and insert / remove for dynamic use.

    RTree::bulk_load

    fn[T] RTree::bulk_load(entries : Array[Entry[T]]) -> RTree[T]

    Builds an RTree from the given entries. Returns an empty tree when entries is empty.

    RTree::drain

    fn[T] RTree::drain(self : RTree[T]) -> Array[T]

    Removes and returns every stored value, leaving the tree empty.

    RTree::insert

    fn[T] RTree::insert(self : RTree[T], entry : Entry[T]) -> Unit

    Inserts entry into the tree, splitting nodes and growing the tree as needed. Amortised O(log n).

    RTree::is_empty

    fn[T] RTree::is_empty(self : RTree[T]) -> Bool

    RTree::iter

    fn[T] RTree::iter(self : RTree[T]) -> Iter[T]

    Lazy iterator over every stored value, in arbitrary (depth-first tree) order.

    RTree::locate_at_point

    fn[T] RTree::locate_at_point(self : RTree[T], pt :
    Coord
    ) -> Array[T]

    Returns every value whose bounding rect contains pt. Result order is implementation-defined (depends on tree layout).

    RTree::locate_within_distance

    fn[T] RTree::locate_within_distance(self : RTree[T], pt :
    Coord
    , r : Double) -> Array[T]

    Returns every value whose bounding rect lies within Euclidean distance r of pt (i.e. squared min-distance from pt to bbox is <= r * r). Result order is implementation-defined.

    RTree::nearest_neighbors

    fn[T] RTree::nearest_neighbors(self : RTree[T], pt :
    Coord
    , k : Int?) -> Array[T]

    Returns every stored value sorted by ascending Euclidean distance from pt (measured to each value's bounding rect). When k is Some(n) the result is truncated to the first n items; None returns all.

    RTree::new

    fn[T] RTree::new() -> RTree[T]

    Returns an empty RTree. Use with insert / remove for dynamic use.

    RTree::query_nearest

    fn[T] RTree::query_nearest(self : RTree[T], target :
    Coord
    ) -> T?

    Returns a closest-by-bbox-distance entry to the given coordinate, or None if the tree is empty. When two entries have equal bbox distance the result is implementation-defined.

    Note: the returned "nearest" is by bounding rectangle distance only; callers that need true geometric distance to a complex shape should post-filter by computing the actual distance for the candidate(s) returned by query_rect_intersection of an appropriately enlarged query rect.

    RTree::query_rect_intersection

    fn[T] RTree::query_rect_intersection(self : RTree[T], query :
    Rect
    ) -> Array[T]

    Returns all entries whose bounding rect intersects query.

    RTree::remove

    fn[T : Eq] RTree::remove(self : RTree[T], entry : Entry[T]) -> T?

    Removes the first stored entry whose value equals entry.value and whose leaf intersects entry.bbox. Returns the removed value, or None if no such entry was found. Uses the supplied bbox to prune the search; pass the same Entry value used in the original insert for best results.

    RTree::size

    fn[T] RTree::size(self : RTree[T]) -> Int

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io