Skip to content

Commit 864877e

Browse files
authored
Merge pull request #7 from asinghvi17/patch-2
Implement the AbstractTrees interface
2 parents eab32f2 + 4a9edf8 commit 864877e

File tree

6 files changed

+109
-1
lines changed

6 files changed

+109
-1
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ authors = ["Max Freudenberg <maximilian.freudenberg@uni-goettingen.de> and contr
44
version = "0.1.1"
55

66
[deps]
7+
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
78
Extents = "411431e0-e8b7-467b-b5e0-f676ba4f2910"
89
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
910

1011
[compat]
11-
julia = "1.10"
12+
AbstractTrees = "0.4.5"
1213
Extents = "0.1.0"
1314
GeoInterface = "1"
15+
julia = "1.6"
1416

1517
[extras]
1618
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

src/SortTileRecursiveTree.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module SortTileRecursiveTree
22

33
using Extents
44
import GeoInterface as GI
5+
import AbstractTrees
56

67

78
"""
@@ -156,6 +157,9 @@ function query!(query_result::Vector{Int}, node::STRLeafNode, extent::Extent)
156157
end
157158

158159

160+
161+
include("abstracttrees.jl")
162+
159163
export STRtree, query
160164

161165
end

src/abstracttrees.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
AbstractTrees.children(tree::STRtree) = AbstractTrees.children(tree.rootnode)
2+
AbstractTrees.parent(tree::STRtree) = nothing
3+
4+
AbstractTrees.nodevalue(tree::STRtree) = AbstractTrees.nodevalue(tree.rootnode)
5+
6+
# Implement the interface for general STRNodes
7+
8+
AbstractTrees.children(node::STRNode) = node.children
9+
AbstractTrees.nodevalue(node::STRNode) = Extents.extent(node)
10+
11+
# Implement the interface for STRLeafNodes
12+
AbstractTrees.children(node::STRLeafNode) = STRLeafNode[] # no children for a leaf node
13+
AbstractTrees.nodevalue(node::STRLeafNode) = Extents.extent(node)
14+
15+
16+
# Define the traits from AbstractTrees
17+
AbstractTrees.ParentLinks(::Type{<: STRtree}) = AbstractTrees.ImplicitParents()
18+
AbstractTrees.ParentLinks(::Type{<: STRNode}) = AbstractTrees.ImplicitParents()
19+
AbstractTrees.ParentLinks(::Type{<: STRLeafNode}) = AbstractTrees.ImplicitParents()
20+
21+
AbstractTrees.SiblingLinks(::Type{<: STRtree}) = AbstractTrees.ImplicitSiblings()
22+
AbstractTrees.SiblingLinks(::Type{<: STRNode}) = AbstractTrees.ImplicitSiblings()
23+
AbstractTrees.SiblingLinks(::Type{<: STRLeafNode}) = AbstractTrees.ImplicitSiblings()
24+
25+
AbstractTrees.ChildIndexing(::Type{<: STRtree}) = AbstractTrees.IndexedChildren()
26+
AbstractTrees.ChildIndexing(::Type{<: STRNode}) = AbstractTrees.IndexedChildren()
27+
# We don't define this trait for STRLeafNodes, because they have no children.
28+
29+
# Type stability fixes
30+
31+
AbstractTrees.NodeType(::Type{<:Union{STRNode, STRLeafNode, STRtree}}) = AbstractTrees.NodeTypeUnknown()

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[deps]
2+
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
23
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
34
Extents = "411431e0-e8b7-467b-b5e0-f676ba4f2910"
45
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"

test/abstracttrees.jl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
using Test
3+
using AbstractTrees
4+
using SortTileRecursiveTree
5+
using SortTileRecursiveTree: STRtree, STRNode, STRLeafNode
6+
using Extents
7+
import GeoInterface as GI
8+
9+
@testset "AbstractTrees interface" begin
10+
# Create a simple test tree structure
11+
# Level 1: root with extent (0,0) -> (10,10)
12+
# Level 2: two children with extents (0,0)->(5,5) and (5,5)->(10,10)
13+
# Level 3: leaf nodes
14+
15+
geom1 = GI.MultiPoint([GI.Point((0.0, 0.0)), GI.Point((2.5, 2.5))])
16+
geom2 = GI.MultiPoint([GI.Point((2.5, 2.5)), GI.Point((5.0, 5.0))])
17+
geom3 = GI.MultiPoint([GI.Point((5.0, 5.0)), GI.Point((7.5, 7.5))])
18+
geom4 = GI.MultiPoint([GI.Point((7.5, 7.5)), GI.Point((10.0, 10.0))])
19+
20+
tree = STRtree([geom1, geom1, geom2, geom2, geom3, geom3, geom4, geom4]; nodecapacity=2)
21+
22+
@testset "Basic Tree Structure" begin
23+
# Test children access
24+
@test length(children(tree)) == 2
25+
@test length(children(first(children(tree)))) == 2
26+
@test length(children(last(children(tree)))) == 2
27+
@test all(x -> isa(x, STRLeafNode), children(first(children(tree))))
28+
@test all(x -> isa(x, STRLeafNode), children(last(children(tree))))
29+
end
30+
31+
@testset "Node Values" begin
32+
# Test that nodevalue returns proper extents
33+
@test nodevalue(tree) isa Extent
34+
@test nodevalue(first(children(tree))) isa Extent
35+
@test nodevalue(first(children(last(children(tree))))) isa Extent
36+
end
37+
38+
@testset "Tree Traits" begin
39+
# Test ParentLinks trait
40+
@test ParentLinks(STRtree) == ImplicitParents()
41+
@test ParentLinks(STRNode) == ImplicitParents()
42+
@test ParentLinks(STRLeafNode) == ImplicitParents()
43+
44+
# Test SiblingLinks trait
45+
@test SiblingLinks(STRtree) == ImplicitSiblings()
46+
@test SiblingLinks(STRNode) == ImplicitSiblings()
47+
@test SiblingLinks(STRLeafNode) == ImplicitSiblings()
48+
49+
# Test ChildIndexing trait
50+
@test ChildIndexing(STRtree) == IndexedChildren()
51+
@test ChildIndexing(STRNode) == IndexedChildren()
52+
end
53+
54+
@testset "Tree Traversal Iterators" begin
55+
# Test that we can traverse the tree
56+
nodes = collect(PreOrderDFS(tree))
57+
@test length(nodes) == 7 # 1 root + 2 internal nodes + 4 leaves
58+
59+
leaves = collect(Leaves(tree))
60+
@test length(leaves) == 4
61+
@test all(x -> x isa STRLeafNode, leaves)
62+
end
63+
64+
@testset "Node Type Stability" begin
65+
@test NodeType(STRtree) == NodeTypeUnknown()
66+
@test NodeType(STRNode) == NodeTypeUnknown()
67+
@test NodeType(STRLeafNode) == NodeTypeUnknown()
68+
end
69+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ import GeoInterface as GI
3434
@test points[query_result] == points[:,1]
3535
@test query(tree, Extent(X=(0, 0.5), Y=(0, 0.5))) == []
3636
end
37+
@testset "AbstractTrees interface" begin; include("abstracttrees.jl"); end
3738
end

0 commit comments

Comments
 (0)