Skip to content

Commit e9fb515

Browse files
committed
generic tests for STI with FlatNoTree and STR
1 parent aa8ea43 commit e9fb515

File tree

1 file changed

+93
-16
lines changed

1 file changed

+93
-16
lines changed

test/utils/SpatialTreeInterface.jl

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,53 @@ using Test
33
using GeometryOps.SpatialTreeInterface
44
using GeometryOps.SpatialTreeInterface: isspatialtree, isleaf, getchild, nchild, child_indices_extents, node_extent
55
using GeometryOps.SpatialTreeInterface: query, do_query, do_dual_query
6+
using GeometryOps.SpatialTreeInterface: FlatNoTree
67
using Extents
78
using GeoInterface: GeoInterface as GI
9+
using SortTileRecursiveTree: STRtree
810

9-
using GeometryOps.SpatialTreeInterface: FlatNoTree
10-
11-
# Test FlatNoTree implementation
12-
@testset "FlatNoTree" begin
13-
# Test with a simple vector of extents
14-
extents = [
15-
Extents.Extent(X=(0.0, 1.0), Y=(0.0, 1.0)),
16-
Extents.Extent(X=(1.0, 2.0), Y=(1.0, 2.0)),
17-
Extents.Extent(X=(2.0, 3.0), Y=(2.0, 3.0))
18-
]
19-
tree = FlatNoTree(extents)
20-
11+
# Generic test functions for spatial trees
12+
function test_basic_interface(TreeType)
2113
@testset "Basic interface" begin
14+
# Create a simple tree with one extent
15+
extents = [Extents.Extent(X=(0.0, 1.0), Y=(0.0, 1.0))]
16+
tree = TreeType(extents)
17+
2218
@test isleaf(tree)
2319
@test isspatialtree(tree)
2420
@test isspatialtree(typeof(tree))
2521
end
22+
end
2623

24+
function test_child_indices_extents(TreeType)
2725
@testset "child_indices_extents" begin
26+
# Create a tree with three extents
27+
extents = [
28+
Extents.Extent(X=(0.0, 1.0), Y=(0.0, 1.0)),
29+
Extents.Extent(X=(1.0, 2.0), Y=(1.0, 2.0)),
30+
Extents.Extent(X=(2.0, 3.0), Y=(2.0, 3.0))
31+
]
32+
tree = TreeType(extents)
33+
2834
# Test that we get the correct indices and extents
2935
indices_extents = collect(child_indices_extents(tree))
3036
@test length(indices_extents) == 3
3137
@test indices_extents[1] == (1, extents[1])
3238
@test indices_extents[2] == (2, extents[2])
3339
@test indices_extents[3] == (3, extents[3])
3440
end
41+
end
3542

43+
function test_query_functionality(TreeType)
3644
@testset "Query functionality" begin
45+
# Create a tree with three extents
46+
extents = [
47+
Extents.Extent(X=(0.0, 1.0), Y=(0.0, 1.0)),
48+
Extents.Extent(X=(1.0, 2.0), Y=(1.0, 2.0)),
49+
Extents.Extent(X=(2.0, 3.0), Y=(2.0, 3.0))
50+
]
51+
tree = TreeType(extents)
52+
3753
# Test query with a predicate that matches all
3854
all_pred = x -> true
3955
results = query(tree, all_pred)
@@ -49,14 +65,16 @@ using GeometryOps.SpatialTreeInterface: FlatNoTree
4965
results = query(tree, Base.Fix1(Extents.intersects, search_extent))
5066
@test sort(results) == [1, 2] # Should match first two extents
5167
end
68+
end
5269

70+
function test_dual_query_functionality(TreeType)
5371
@testset "Dual query functionality" begin
54-
# Create two trees for dual query testing
55-
tree1 = FlatNoTree([
72+
# Create two trees with overlapping extents
73+
tree1 = TreeType([
5674
Extents.Extent(X=(0.0, 1.0), Y=(0.0, 1.0)),
5775
Extents.Extent(X=(1.0, 2.0), Y=(1.0, 2.0))
5876
])
59-
tree2 = FlatNoTree([
77+
tree2 = TreeType([
6078
Extents.Extent(X=(0.5, 1.5), Y=(0.5, 1.5)),
6179
Extents.Extent(X=(1.5, 2.5), Y=(1.5, 2.5))
6280
])
@@ -65,7 +83,7 @@ using GeometryOps.SpatialTreeInterface: FlatNoTree
6583
all_pred = (x, y) -> true
6684
results = Tuple{Int, Int}[]
6785
do_dual_query((i, j) -> push!(results, (i, j)), all_pred, tree1, tree2)
68-
@test sort(results) == [(1,1), (1,2), (2,1), (2,2)]
86+
@test length(results) == 4 # 2 points in tree1 * 2 points in tree2
6987

7088
# Test dual query with a specific predicate
7189
intersects_pred = (x, y) -> Extents.intersects(x, y)
@@ -75,3 +93,62 @@ using GeometryOps.SpatialTreeInterface: FlatNoTree
7593
end
7694
end
7795

96+
function test_geometry_support(TreeType)
97+
@testset "Geometry support" begin
98+
# Create a tree with 100 points
99+
points = tuple.(1:100, 1:100)
100+
tree = TreeType(points)
101+
102+
# Test basic interface
103+
@test isleaf(tree)
104+
@test isspatialtree(tree)
105+
@test isspatialtree(typeof(tree))
106+
107+
# Test child indices and extents
108+
indices_extents = collect(child_indices_extents(tree))
109+
@test length(indices_extents) == 100
110+
111+
# Check first and last points
112+
first_idx, first_extent = indices_extents[1]
113+
last_idx, last_extent = indices_extents[100]
114+
115+
@test first_idx == 1
116+
@test last_idx == 100
117+
@test first_extent == Extents.Extent(X=(1.0, 1.0), Y=(1.0, 1.0))
118+
@test last_extent == Extents.Extent(X=(100.0, 100.0), Y=(100.0, 100.0))
119+
120+
# Test query functionality
121+
all_pred = x -> true
122+
results = query(tree, all_pred)
123+
@test sort(results) == collect(1:100)
124+
125+
none_pred = x -> false
126+
results = query(tree, none_pred)
127+
@test isempty(results)
128+
129+
search_extent = Extents.Extent(X=(45.0, 55.0), Y=(45.0, 55.0))
130+
results = query(tree, Base.Fix1(Extents.intersects, search_extent))
131+
@test sort(results) == collect(45:55)
132+
end
133+
end
134+
135+
# Test FlatNoTree implementation
136+
@testset "FlatNoTree" begin
137+
test_basic_interface(FlatNoTree)
138+
test_child_indices_extents(FlatNoTree)
139+
test_query_functionality(FlatNoTree)
140+
test_dual_query_functionality(FlatNoTree)
141+
test_geometry_support(FlatNoTree)
142+
end
143+
144+
# Test STRtree implementation
145+
@testset "STRtree" begin
146+
test_basic_interface(STRtree)
147+
test_child_indices_extents(STRtree)
148+
test_query_functionality(STRtree)
149+
test_dual_query_functionality(STRtree)
150+
test_geometry_support(STRtree)
151+
end
152+
153+
154+

0 commit comments

Comments
 (0)