### Install SpatialIndexing.jl Source: https://github.com/alyst/spatialindexing.jl/blob/master/docs/source/index.md Use the Julia package manager to add the SpatialIndexing package to your environment. ```julia using Pkg; Pkg.add("SpatialIndexing") ``` -------------------------------- ### RTree Creation and Bulk Loading Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Demonstrates how to create an R-tree and efficiently load data into it, either with a custom element conversion function or from pre-defined Minimum Bounding Rectangles (MBRs). ```APIDOC ## RTree Creation and Bulk Loading ### Description This section shows how to initialize an R-tree and populate it with data. It covers two main approaches: bulk loading with a custom element conversion function and bulk loading from a collection of MBRs. ### Method `RTree{T, N}(...)` constructor, `SI.load!`, `enumerate` ### Parameters - `leaf_capacity` (Int) - Optional - Maximum number of elements in a leaf node. - `branch_capacity` (Int) - Optional - Maximum number of children in a branch node. - `data` - Iterable collection of data to load. - `convertel` (Function) - A function to convert raw data into `SpatialElem` objects. - `mbrs` - Iterable collection of MBRs. ### Request Example ```julia using SpatialIndexing const SI = SpatialIndexing # Create empty tree tree = RTree{Float64, 2}(Int, String, leaf_capacity=50, branch_capacity=50) # Bulk load with element converter function # Assuming 'data' is a collection of elements that can be converted # SI.load!(tree, data, convertel = x -> SI.SpatialElem(x[1], x[2], x[3])) # Bulk load from enumerated MBRs tree2 = RTree{Float64, 2}(Int, String, leaf_capacity=10, branch_capacity=10) # Example MBRs mbrs = [SI.Rect((x, y), (x+1, y+1)) for x in 1:100, y in 1:100] SI.load!(tree2, enumerate(vec(mbrs)), convertel = x -> SI.SpatialElem(x[2], x[1], "item_$(x[1])")) ``` ### Response - `tree` (RTree) - The populated R-tree. - `tree2` (RTree) - The second populated R-tree. ``` -------------------------------- ### Create and Bulk Load R-Tree Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Demonstrates creating an empty R-tree and then bulk loading data using a custom element converter function. Also shows an alternative bulk load from enumerated MBRs. ```julia tree = RTree{Float64, 2}(Int, String, leaf_capacity=50, branch_capacity=50) # Bulk load with element converter function SI.load!(tree, data, convertel = x -> SI.SpatialElem(x[1], x[2], x[3])) length(tree) # => 10000 ``` ```julia # Alternative: bulk load from enumerated MBRs tree2 = RTree{Float64, 2}(Int, String, leaf_capacity=10, branch_capacity=10) mbrs = [SI.Rect((x, y), (x+1, y+1)) for x in 1:100, y in 1:100] SI.load!(tree2, enumerate(vec(mbrs)), convertel = x -> SI.SpatialElem(x[2], x[1], "item_$(x[1])")) ``` -------------------------------- ### Bulk load data into an R-tree with load! Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Efficiently loads a large dataset into an empty R-tree using the OMT algorithm for better tree organization compared to sequential insertions. ```julia using SpatialIndexing const SI = SpatialIndexing # Prepare data as tuples of (mbr, id, value) data = [ (SI.Rect((i*1.0, i*1.0), (i*1.0+0.5, i*1.0+0.5)), i, "elem_$i") for i in 1:10000 ] # load!(tree, data) # Example usage, requires an empty tree ``` -------------------------------- ### SimpleSpatialIndex Usage Notes Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Provides usage notes for SimpleSpatialIndex, a basic vector-based spatial index. It highlights its O(1) insertion and O(N) search complexity, recommending it only for very small datasets or benchmarking purposes. ```julia using SpatialIndexing const SI = SpatialIndexing # SimpleSpatialIndex is a basic fallback for small datasets or benchmarking # Note: RTree is recommended for most use cases # The SimpleSpatialIndex stores elements in a simple vector # Insertion is O(1) but search is O(N) - linear scan # Only use when: # - Number of elements is very small (<100) # - You need baseline performance comparisons # RTree is vastly superior for: # - Larger datasets # - Frequent spatial queries # - Dynamic insertions and deletions ``` -------------------------------- ### Create and use Spatial Primitives (Point, Rect) Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Defines N-dimensional points and rectangles for spatial operations. Supports area, perimeter, intersection, containment, and combination. ```julia using SpatialIndexing const SI = SpatialIndexing # Create 2D points point1 = SI.Point((1.0, 2.0)) point2 = SI.Point((5.0, 8.0)) # Create rectangles from low/high tuples rect1 = SI.Rect((0.0, 0.0), (10.0, 10.0)) # Rectangle from (0,0) to (10,10) rect2 = SI.Rect((5.0, 5.0), (15.0, 15.0)) # Create rectangle from a point (zero-area rectangle) point_rect = SI.Rect(point1) # Geometric operations SI.area(rect1) # => 100.0 SI.perimeter(rect1) # => 40.0 SI.center(rect1) # => Point((5.0, 5.0)) SI.intersects(rect1, rect2) # => true SI.contains(rect1, point1) # => true SI.overlap_area(rect1, rect2) # => 25.0 SI.combine(rect1, rect2) # => Rect((0.0, 0.0), (15.0, 15.0)) SI.intersect(rect1, rect2) # => Rect((5.0, 5.0), (10.0, 10.0)) ``` -------------------------------- ### Create a 2D R-tree with custom parameters Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Constructs an N-dimensional R-tree with specified coordinate and value types. Customize leaf/branch capacity and tree variant (Linear, Quadratic, Star). ```julia using SpatialIndexing # Create a 2D R-tree storing String values with Int identifiers tree = RTree{Float64, 2}(Int, String, leaf_capacity=100, branch_capacity=100, variant=RTreeStar) # Options: RTreeLinear, RTreeQuadratic, RTreeStar (default) # Create a 3D R-tree without element identifiers tree_3d = RTree{Float64, 3}(String, leaf_capacity=50, branch_capacity=50) # Create a tree with custom fill/split factors custom_tree = RTree{Float64, 2}(Int, String, fill_factor=0.7, # Target node fill after splitting split_factor=0.4, # Max size difference after split reinsert_factor=0.3) # Threshold for node reinsertion ``` -------------------------------- ### SimpleSpatialIndex Documentation Source: https://github.com/alyst/spatialindexing.jl/blob/master/docs/source/simple.md This section provides documentation for the SimpleSpatialIndex, detailing its structure and performance characteristics. ```APIDOC ## SimpleSpatialIndex ### Description `SimpleSpatialIndex` stores all data elements in a vector. So, while insertion of new data takes constant time, the time of spatial searches grows linearly with the number of elements. This spatial index is intended as a reference implementation for benchmarking and not recommended for production usage. ### Usage ```julia using SpatialIndexing # Example usage (conceptual, as specific methods are not detailed in the source) # Assuming SimpleSpatialIndex has methods for insertion and search index = SimpleSpatialIndex() # insert!(index, data) # search(index, region) ``` ``` -------------------------------- ### Spatial Primitives Source: https://github.com/alyst/spatialindexing.jl/blob/master/docs/source/regions.md This section covers the core spatial data structures: Region, Point, and Rect. ```APIDOC ## Spatial Primitives ### Description Defines the fundamental geometric shapes used for spatial indexing. ### Types - **Region**: Represents a general spatial region. - **Point**: Represents a point in space. - **Rect**: Represents a rectangular area. ``` -------------------------------- ### RTree Variants in SpatialIndexing.jl Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Demonstrates the creation and basic usage of different R-tree variants: RTreeLinear, RTreeQuadratic, and RTreeStar. Each variant offers different performance characteristics for insertion and querying. ```julia using SpatialIndexing const SI = SpatialIndexing # RTreeLinear - Linear cost split algorithm (fastest insertion) linear_tree = RTree{Float64, 2}(Int, String, variant=SI.RTreeLinear) # RTreeQuadratic - Quadratic cost split algorithm (balanced) quadratic_tree = RTree{Float64, 2}(Int, String, variant=SI.RTreeQuadratic) # RTreeStar - R*-tree with forced reinsertion (best query performance) star_tree = RTree{Float64, 2}(Int, String, variant=SI.RTreeStar, reinsert_factor=0.3) # 30% of nodes reinserted on overflow # Compare performance for your use case trees = [ ("Linear", RTree{Float64,2}(Int, String, variant=SI.RTreeLinear)), ("Quadratic", RTree{Float64,2}(Int, String, variant=SI.RTreeQuadratic)), ("Star", RTree{Float64,2}(Int, String, variant=SI.RTreeStar)) ] data = [(SI.Rect((rand()*100, rand()*100), (rand()*100+1, rand()*100+1)), i, "v") for i in 1:1000] for (name, tree) in trees for (mbr, id, val) in data insert!(tree, mbr, id, val) end println("$name tree height: $(SI.height(tree))") end ``` -------------------------------- ### Insert and Subtract Elements in RTree Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Demonstrates inserting elements into an R-tree and then removing them using rectangular regions. Use this for dynamic updates of spatial data. ```julia for i in 1:50, j in 1:50 x, y = Float64(i), Float64(j) insert!(tree, SI.Rect((x, y), (x+0.5, y+0.5)), i*100+j, "elem") end println("Before subtract: $(length(tree)) elements") # => 2500 # Remove all elements in a rectangular region SI.subtract!(tree, SI.Rect((10.0, 10.0), (30.0, 30.0))) println("After subtract: $(length(tree)) elements") # Elements outside region remain # Subtract with a point region (removes exact matches only) SI.subtract!(tree, SI.Rect((1.0, 1.0), (1.5, 1.5))) # Multiple subtractions SI.subtract!(tree, SI.Rect((40.0, 40.0), (50.0, 50.0))) ``` -------------------------------- ### Base Query Functions Source: https://github.com/alyst/spatialindexing.jl/blob/master/docs/source/query.md Provides access to base Julia functions used in conjunction with spatial queries. ```APIDOC ## Base Query Functions ### Description These are standard Julia functions that are often used when working with spatial query results or data structures. ### Functions - **Base.findfirst**: Finds the first element in a collection that satisfies a condition. Useful for checking if any spatial object meets query criteria. - **Base.isempty**: Checks if a collection (like a query result set) is empty. Useful for determining if a spatial query returned any results. ``` -------------------------------- ### Spatial Indexing Core Types and Operations Source: https://github.com/alyst/spatialindexing.jl/blob/master/docs/source/abstract.md Overview of the primary spatial indexing structures, traits, and mutation methods. ```APIDOC ## Spatial Indexing Core Types ### Description Core types and traits used for spatial indexing operations. ### Types - **SpatialIndex** - The primary spatial index structure. - **SpatialElem** - Represents an element within the spatial index. - **SpatialIndexException** - Exception type for spatial indexing errors. ### Traits - **SpatialIndexing.HasMBR** - Trait indicating an object has a Minimum Bounding Rectangle. - **SpatialIndexing.HasID** - Trait indicating an object has an associated ID. ## Mutation Operations ### Description Methods for modifying spatial index contents. ### Methods - **SpatialIndexing.subtract!(index, item)** - Removes an item from the spatial index. - **SpatialIndexing.load!(index, items)** - Bulk loads items into the spatial index. ``` -------------------------------- ### Spatial Queries Source: https://github.com/alyst/spatialindexing.jl/blob/master/docs/source/rtree.md Methods for performing spatial queries against an R-tree index. ```APIDOC ## Spatial Queries ### Description Methods to query the R-tree for elements based on spatial regions. ### Methods - `findfirst(tree, reg, [id])`: Finds the first element in the tree within the specified region `reg`. - `contained_in(tree, reg)`: Returns elements contained within the specified region `reg`. - `intersects_with(tree, reg)`: Returns elements that intersect with the specified region `reg`. ``` -------------------------------- ### Iterate Over All Elements in RTree Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Shows how to iterate through all elements stored in an R-tree using standard Julia for loops and iterator functions like collect, filter, map, and sum. This is useful for processing all spatial data. ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) for i in 1:100 insert!(tree, SI.Rect((Float64(i), Float64(i)), (Float64(i)+1, Float64(i)+1)), i, "item_$i") end # Iterate all elements for elem in tree println("ID: $(SI.id(elem)), Value: $(elem.val)") end # Use with standard iterator functions all_elems = collect(tree) println("Total elements: $(length(all_elems))") # Filter elements large_coords = filter(elem -> SI.mbr(elem).low[1] > 50.0, collect(tree)) # Map over elements ids = map(elem -> SI.id(elem), collect(tree)) # Count elements matching condition count = sum(1 for elem in tree if SI.mbr(elem).low[1] < 25.0) ``` -------------------------------- ### Finding Elements with findfirst Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Locates an element in the R-tree using its exact Minimum Bounding Rectangle (MBR) and an optional ID. Returns the containing leaf node and element position, or `nothing` if not found. ```APIDOC ## Finding Elements with findfirst ### Description The `findfirst` function searches for an element within the R-tree based on its exact MBR. It can optionally filter by a specific element ID. If found, it returns a tuple containing the leaf node and the position of the element within that leaf. If no matching element is found, it returns `nothing`. ### Method `findfirst(tree, mbr, id)` ### Parameters - `tree` (RTree) - The R-tree to search within. - `mbr` (Rect) - The Minimum Bounding Rectangle to match. - `id` (Any) - Optional - The specific ID of the element to find. ### Request Example ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) insert!(tree, SI.Rect((0.0, 0.0), (1.0, 1.0)), 1, "first") insert!(tree, SI.Rect((5.0, 5.0), (6.0, 6.0)), 2, "second") insert!(tree, SI.Rect((0.0, 0.0), (1.0, 1.0)), 3, "same location") # Find by exact MBR (returns first match) result = findfirst(tree, SI.Rect((0.0, 0.0), (1.0, 1.0))) if result !== nothing leaf, pos = result elem = leaf[pos] println("Found: ", elem.val) # => "first" or "same location" end # Find by MBR and specific ID result_with_id = findfirst(tree, SI.Rect((0.0, 0.0), (1.0, 1.0)), 3) if result_with_id !== nothing leaf, pos = result_with_id println("Found: ", leaf[pos].val) # => "same location" end # Element not found findfirst(tree, SI.Rect((100.0, 100.0), (101.0, 101.0))) # => nothing ``` ### Response - `result` (Tuple{Node, Int} or Nothing) - A tuple containing the leaf node and the position of the element, or `nothing` if not found. - `elem` (SpatialElem) - The found spatial element. ``` -------------------------------- ### Spatial Primitive Operations Source: https://github.com/alyst/spatialindexing.jl/blob/master/docs/source/regions.md Provides functions for manipulating and querying spatial primitives. ```APIDOC ## Spatial Primitive Operations ### Description This section details common operations performed on spatial primitives. ### Functions - **empty**: Creates an empty spatial primitive. - **isvalid**: Checks if a spatial primitive is valid. - **center**: Calculates the center of a spatial primitive. - **area**: Computes the area of a spatial primitive. - **perimeter**: Computes the perimeter of a spatial primitive. - **intersects**: Checks if two spatial primitives intersect. - **contains**: Checks if a spatial primitive contains another. - **touches**: Checks if two spatial primitives touch. - **in**: Checks if a point is within a spatial primitive (alias for `contains`). - **overlap_area**: Calculates the area of overlap between two spatial primitives. - **combined_area**: Calculates the total area covered by the union of two spatial primitives. - **enlargement**: Calculates the enlargement needed to contain another primitive. - **combine**: Combines two spatial primitives into a single larger primitive. - **intersect**: Computes the intersection of two spatial primitives. ``` -------------------------------- ### Spatial Query Functions Source: https://github.com/alyst/spatialindexing.jl/blob/master/docs/source/query.md Core functions for performing spatial queries, checking for containment and intersection. ```APIDOC ## Spatial Query Functions ### Description These functions allow you to query spatial data structures to find objects that are contained within a region or intersect with a given shape. ### Functions - **contained_in(object, region)**: Checks if the `object` is entirely contained within the `region`. - **intersects_with(object, region)**: Checks if the `object` has any spatial overlap with the `region`. ``` -------------------------------- ### Create and use SpatialElem Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Represents a spatial data element with a Minimum Bounding Rectangle (MBR), optional ID, and a value. Implements HasMBR and HasID traits. ```julia using SpatialIndexing const SI = SpatialIndexing # SpatialElem with ID and value elem = SI.SpatialElem( SI.Rect((1.0, 2.0), (3.0, 4.0)), # MBR 42, # ID (Int) "my data" # Value (String) ) # Access properties SI.mbr(elem) # => Rect((1.0, 2.0), (3.0, 4.0)) SI.id(elem) # => 42 elem.val # => "my data" # SpatialElem without ID (K=Nothing) elem_no_id = SI.SpatialElem( SI.Rect((0.0, 0.0), (1.0, 1.0)), nothing, "anonymous data" ) ``` -------------------------------- ### Insert elements into an R-tree Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Adds elements to an R-tree using `insert!`. Elements can be defined by MBR, point, ID, and value. Supports inserting pre-constructed SpatialElem objects. ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) # Insert with rectangle, ID, and value insert!(tree, SI.Rect((0.0, 0.0), (1.0, 1.0)), 1, "first element") insert!(tree, SI.Rect((5.0, 5.0), (6.0, 6.0)), 2, "second element") # Insert with point (creates zero-area MBR) insert!(tree, SI.Point((3.0, 3.0)), 3, "point element") # Insert pre-constructed SpatialElem elem = SI.SpatialElem(SI.Rect((10.0, 10.0), (11.0, 11.0)), 4, "fourth") insert!(tree, elem) # Check tree state length(tree) # => 4 isempty(tree) # => false SI.height(tree) # Tree height (depends on capacity) SI.mbr(tree) # Overall bounding rectangle of all elements ``` -------------------------------- ### Spatial Query Types Source: https://github.com/alyst/spatialindexing.jl/blob/master/docs/source/query.md Defines the core types used for spatial queries, including the iterator, query kind, and query match. ```APIDOC ## Spatial Query Types ### Description These types are fundamental to performing and interpreting spatial queries within the library. ### Types - **SpatialQueryIterator**: An iterator that yields results of a spatial query. - **QueryKind**: An enumeration defining the type of spatial query (e.g., intersection, containment). - **QueryMatch**: Represents a single match found during a spatial query, typically including the object and its relationship to the query region. ``` -------------------------------- ### Find Element by Exact MBR Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Locates an element by its exact Minimum Bounding Rectangle (MBR) and optionally its ID. Returns the containing leaf node and element position, or nothing if not found. Useful for precise element retrieval. ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) insert!(tree, SI.Rect((0.0, 0.0), (1.0, 1.0)), 1, "first") insert!(tree, SI.Rect((5.0, 5.0), (6.0, 6.0)), 2, "second") insert!(tree, SI.Rect((0.0, 0.0), (1.0, 1.0)), 3, "same location") # Find by exact MBR (returns first match) result = findfirst(tree, SI.Rect((0.0, 0.0), (1.0, 1.0))) if result !== nothing leaf, pos = result elem = leaf[pos] println("Found: $(elem.val)") # => "first" or "same location" end # Find by MBR and specific ID result_with_id = findfirst(tree, SI.Rect((0.0, 0.0), (1.0, 1.0)), 3) if result_with_id !== nothing leaf, pos = result_with_id println("Found: $(leaf[pos].val)") # => "same location" end # Element not found findfirst(tree, SI.Rect((100.0, 100.0), (101.0, 101.0))) # => nothing ``` -------------------------------- ### Querying Elements Contained in a Region Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Retrieves all elements whose Minimum Bounding Rectangles (MBRs) are completely within a specified query region. Supports iteration and collection. ```APIDOC ## Querying Elements Contained in a Region ### Description The `contained_in` function efficiently finds and returns an iterator for all spatial elements whose MBRs are entirely enclosed within a given query rectangle. This is useful for spatial selections where complete containment is required. ### Method `contained_in(tree, query_rect)` ### Parameters - `tree` (RTree) - The R-tree to query. - `query_rect` (Rect) - The rectangular region to search within. ### Request Example ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) for i in 1:100 x, y = Float64(i % 10), Float64(i ÷ 10) insert!(tree, SI.Rect((x, y), (x+0.5, y+0.5)), i, "elem_$i") end # Query region query_rect = SI.Rect((2.0, 2.0), (7.0, 7.0)) # Iterate over contained elements for elem in contained_in(tree, query_rect) println("Contained: id=$(SI.id(elem)), mbr=$(SI.mbr(elem))") end # Collect all contained elements contained_elements = collect(contained_in(tree, query_rect)) println("Found ", length(contained_elements), " elements contained in query region") # Check if any elements exist in region isempty(tree, query_rect) # => false if any elements in region ``` ### Response - `contained_in` (Iterator{SpatialElem}) - An iterator yielding `SpatialElem` objects that are fully contained within the `query_rect`. - `contained_elements` (Array{SpatialElem}) - An array containing all `SpatialElem` objects fully contained within the `query_rect`. ``` -------------------------------- ### RTree Data Operations Source: https://github.com/alyst/spatialindexing.jl/blob/master/docs/source/rtree.md Methods for managing data within an R-tree structure, including insertion, deletion, and bulk loading. ```APIDOC ## RTree Data Operations ### Description Methods to modify the contents of an RTree instance. ### Methods - `insert!(tree, item)`: Inserts an element into the R-tree. - `delete!(tree, item)`: Removes an element from the R-tree. - `load!(tree, data)`: Performs bulk-loading of data using the Overlap-minimizing Top-down (OMT) approach. - `subtract!(tree, reg)`: Removes all data elements contained within the specified region `reg`. ``` -------------------------------- ### Query Elements Contained in a Region Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Retrieves all elements whose MBRs are completely within a specified query region. Returns an iterator, which can be collected into a list. Also provides a check for emptiness within the region. ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) for i in 1:100 x, y = Float64(i % 10), Float64(i ÷ 10) insert!(tree, SI.Rect((x, y), (x+0.5, y+0.5)), i, "elem_$i") end # Query region query_rect = SI.Rect((2.0, 2.0), (7.0, 7.0)) # Iterate over contained elements for elem in contained_in(tree, query_rect) println("Contained: id=$(SI.id(elem)), mbr=$(SI.mbr(elem))") end # Collect all contained elements contained_elements = collect(contained_in(tree, query_rect)) println("Found $(length(contained_elements)) elements contained in query region") # Check if any elements exist in region isempty(tree, query_rect) # => false if any elements in region ``` -------------------------------- ### Delete Element by MBR and ID Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Removes a specific element from the R-tree using its exact MBR and ID. The tree rebalances automatically after deletion. Supports deletion by MBR only (first match) and by Point. Attempting to delete a non-existent element throws a KeyError. ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) insert!(tree, SI.Rect((0.0, 0.0), (1.0, 1.0)), 1, "first") insert!(tree, SI.Rect((5.0, 5.0), (6.0, 6.0)), 2, "second") insert!(tree, SI.Rect((0.0, 0.0), (1.0, 1.0)), 3, "duplicate mbr") println("Before delete: $(length(tree)) elements") # => 3 # Delete by MBR and ID delete!(tree, SI.Rect((5.0, 5.0), (6.0, 6.0)), 2) println("After delete: $(length(tree)) elements") # => 2 # Delete by MBR only (removes first match) delete!(tree, SI.Rect((0.0, 0.0), (1.0, 1.0))) println("After second delete: $(length(tree)) elements") # => 1 # Delete with Point tree2 = RTree{Float64, 2}(Int, String) insert!(tree2, SI.Point((3.0, 3.0)), 1, "point") delete!(tree2, SI.Point((3.0, 3.0)), 1) # Attempting to delete non-existent element throws KeyError try delete!(tree, SI.Rect((999.0, 999.0), (1000.0, 1000.0))) catch e println("Error: $e") # KeyError end ``` -------------------------------- ### Querying Elements Intersecting a Region Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Retrieves all elements whose Minimum Bounding Rectangles (MBRs) intersect or overlap with a specified query region, including partial overlaps. Returns an iterator. ```APIDOC ## Querying Elements Intersecting a Region ### Description The `intersects_with` function returns an iterator over all elements whose MBRs have any overlap with the specified query region. This includes elements that are fully contained, partially overlap, or completely contain the query region. ### Method `intersects_with(tree, query_rect)` ### Parameters - `tree` (RTree) - The R-tree to query. - `query_rect` (Rect) - The rectangular region to check for intersections. ### Request Example ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) # Insert elements in a grid pattern for i in 1:10, j in 1:10 x, y = Float64(i), Float64(j) insert!(tree, SI.Rect((x, y), (x+1.5, y+1.5)), i*10+j, "cell_$(i)_$(j)") end # Query for intersecting elements (includes partial overlaps) query_rect = SI.Rect((3.5, 3.5), (6.5, 6.5)) intersecting = collect(intersects_with(tree, query_rect)) println("Found ", length(intersecting), " intersecting elements") # Process intersecting elements for elem in intersects_with(tree, query_rect) mbr = SI.mbr(elem) overlap = SI.overlap_area(mbr, query_rect) println("Element ", SI.id(elem), ": overlap area = ", overlap) end ``` ### Response - `intersects_with` (Iterator{SpatialElem}) - An iterator yielding `SpatialElem` objects that intersect with the `query_rect`. ``` -------------------------------- ### Deleting Elements Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Removes an element from the R-tree using its exact MBR and an optional ID. The tree automatically rebalances after deletion. Supports deletion by MBR and ID, MBR only, or by Point. ```APIDOC ## Deleting Elements ### Description The `delete!` function provides a way to remove specific elements from the R-tree. It can identify elements by their exact MBR, optionally combined with their ID. If an ID is not provided, the first element matching the MBR is removed. The tree structure is maintained and rebalanced after each deletion. ### Method `delete!(tree, mbr, id)` `delete!(tree, mbr)` `delete!(tree, point, id)` ### Parameters - `tree` (RTree) - The R-tree from which to delete. - `mbr` (Rect) - The Minimum Bounding Rectangle of the element to delete. - `id` (Any) - Optional - The specific ID of the element to delete. - `point` (Point) - The point associated with the element to delete. ### Request Example ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) insert!(tree, SI.Rect((0.0, 0.0), (1.0, 1.0)), 1, "first") insert!(tree, SI.Rect((5.0, 5.0), (6.0, 6.0)), 2, "second") insert!(tree, SI.Rect((0.0, 0.0), (1.0, 1.0)), 3, "duplicate mbr") println("Before delete: ", length(tree), " elements") # => 3 # Delete by MBR and ID delete!(tree, SI.Rect((5.0, 5.0), (6.0, 6.0)), 2) println("After delete: ", length(tree), " elements") # => 2 # Delete by MBR only (removes first match) delete!(tree, SI.Rect((0.0, 0.0), (1.0, 1.0))) println("After second delete: ", length(tree), " elements") # => 1 # Delete with Point tree2 = RTree{Float64, 2}(Int, String) insert!(tree2, SI.Point((3.0, 3.0)), 1, "point") delete!(tree2, SI.Point((3.0, 3.0)), 1) # Attempting to delete non-existent element throws KeyError try delete!(tree, SI.Rect((999.0, 999.0), (1000.0, 1000.0))) catch e println("Error: ", e) # KeyError end ``` ### Response - The tree is modified in-place. No explicit return value is documented for successful deletion, but the tree's length decreases. ``` -------------------------------- ### Query Elements Intersecting a Region Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Finds all elements whose MBRs intersect (overlap) with a given query region, including partial overlaps. Useful for spatial queries where elements might only partially fall within the search area. Allows calculating overlap area. ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) # Insert elements in a grid pattern for i in 1:10, j in 1:10 x, y = Float64(i), Float64(j) insert!(tree, SI.Rect((x, y), (x+1.5, y+1.5)), i*10+j, "cell_$(i)_$(j)") end # Query for intersecting elements (includes partial overlaps) query_rect = SI.Rect((3.5, 3.5), (6.5, 6.5)) intersecting = collect(intersects_with(tree, query_rect)) println("Found $(length(intersecting)) intersecting elements") # Process intersecting elements for elem in intersects_with(tree, query_rect) mbr = SI.mbr(elem) overlap = SI.overlap_area(mbr, query_rect) println("Element $(SI.id(elem)): overlap area = $overlap") end ``` -------------------------------- ### Subtract Region from R-Tree Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Efficiently removes all elements located within a specified region from the R-tree. This operation is optimized for bulk deletions. ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) ``` -------------------------------- ### Subtracting a Region Source: https://context7.com/alyst/spatialindexing.jl/llms.txt Removes all elements within a specified region from the R-tree, performing efficient bulk deletions. ```APIDOC ## Subtracting a Region ### Description The `subtract!` function is designed for efficient removal of multiple elements from the R-tree. It takes a region (defined by a rectangle) and deletes all elements whose MBRs fall within that region. This is optimized for bulk removal operations. ### Method `subtract!(tree, region)` ### Parameters - `tree` (RTree) - The R-tree from which to remove elements. - `region` (Rect) - The rectangular region defining the area from which elements should be removed. ### Request Example ```julia using SpatialIndexing const SI = SpatialIndexing tree = RTree{Float64, 2}(Int, String) # Assume tree is populated with elements here... # Define a region to subtract region_to_subtract = SI.Rect((1.0, 1.0), (5.0, 5.0)) # Remove all elements within the specified region subtract!(tree, region_to_subtract) ``` ### Response - The tree is modified in-place. Elements within the specified `region` are removed. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.