Skip to content

Commit 06727f1

Browse files
committed
Fix up the tests
1 parent d5a49de commit 06727f1

File tree

1 file changed

+84
-66
lines changed

1 file changed

+84
-66
lines changed

test/utils/SpatialTreeInterface.jl

Lines changed: 84 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -92,37 +92,47 @@ function test_dual_query_functionality(TreeType)
9292
dual_depth_first_search((i, j) -> push!(results, (i, j)), intersects_pred, tree1, tree2)
9393
@test sort(results) == [(1,1), (2,1), (2,2)]
9494
end
95+
@testset "Dual tree query with many boundingboxes" begin
96+
xs = 1:100
97+
ys = 1:100
98+
extent_grid = [Extents.Extent(X=(x, x+1), Y=(y, y+1)) for x in xs, y in ys] |> vec
99+
point_grid = [(x + 0.5, y + 0.5) for x in xs, y in ys] |> vec
95100

96-
@testset "Dual query functionality - every country's polylabel against every country" begin
97-
98-
# Note that this is a perfectly balanced tree query - we don't yet have a test for unbalanced
99-
# trees (but could easily add one, e.g. by getting polylabels of admin-1 or admin-2 regions)
100-
# from Natural Earth, or by using GADM across many countries.
101-
102-
all_countries = NaturalEarth.naturalearth("admin_0_countries", 10)
103-
all_adm0_a3 = all_countries.ADM0_A3
104-
all_geoms = all_countries.geometry
105-
# US minor outlying islands - bug in Polylabel.jl
106-
# A lot of small geoms have this issue, that there will be an error from the queue
107-
# because the cell exists in the queue already.
108-
# Not sure what to do about it, I don't want to check containment every time...
109-
deleteat!(all_adm0_a3, 205)
110-
deleteat!(all_geoms, 205)
111-
112-
geom_tree = TreeType(all_geoms)
113-
114-
polylabels = [Polylabel.polylabel(geom; rtol = 0.019) for geom in all_geoms]
115-
polylabel_tree = TreeType(polylabels)
116-
117-
found_countries = falses(length(polylabels))
101+
extent_tree = TreeType(extent_grid)
102+
point_tree = TreeType(point_grid)
118103

119-
dual_depth_first_search(Extents.intersects, geom_tree, polylabel_tree) do i, j
104+
found_everything = falses(length(extent_grid))
105+
dual_depth_first_search(Extents.intersects, extent_tree, point_tree) do i, j
120106
if i == j
121-
found_countries[i] = true
107+
found_everything[i] = true
122108
end
123109
end
110+
@test all(found_everything)
111+
end
112+
end
124113

125-
@test all(found_countries)
114+
function test_geometry_support(TreeType)
115+
@testset "Geometry support" begin
116+
# Create a tree with 100 points
117+
points = tuple.(1:100, 1:100)
118+
tree = TreeType(points)
119+
120+
# Test basic interface
121+
@test isspatialtree(tree)
122+
@test isspatialtree(typeof(tree))
123+
124+
# Test query functionality
125+
all_pred = x -> true
126+
results = query(tree, all_pred)
127+
@test sort(results) == collect(1:100)
128+
129+
none_pred = x -> false
130+
results = query(tree, none_pred)
131+
@test isempty(results)
132+
133+
search_extent = Extents.Extent(X=(45.0, 55.0), Y=(45.0, 55.0))
134+
results = query(tree, Base.Fix1(Extents.intersects, search_extent))
135+
@test sort(results) == collect(45:55)
126136
end
127137
end
128138

@@ -153,54 +163,62 @@ function test_find_point_in_all_countries(TreeType)
153163
end
154164
end
155165

156-
function test_geometry_support(TreeType)
157-
@testset "Geometry support" begin
158-
# Create a tree with 100 points
159-
points = tuple.(1:100, 1:100)
160-
tree = TreeType(points)
161-
162-
# Test basic interface
163-
@test isspatialtree(tree)
164-
@test isspatialtree(typeof(tree))
165-
166-
# Test query functionality
167-
all_pred = x -> true
168-
results = query(tree, all_pred)
169-
@test sort(results) == collect(1:100)
170-
171-
none_pred = x -> false
172-
results = query(tree, none_pred)
173-
@test isempty(results)
174-
175-
search_extent = Extents.Extent(X=(45.0, 55.0), Y=(45.0, 55.0))
176-
results = query(tree, Base.Fix1(Extents.intersects, search_extent))
177-
@test sort(results) == collect(45:55)
178-
end
179-
end
180-
181166
# Test FlatNoTree implementation
182167
@testset "FlatNoTree" begin
183-
@testset let TreeType = FlatNoTree
184-
test_basic_interface(TreeType)
185-
test_child_indices_extents(TreeType)
186-
test_query_functionality(TreeType)
187-
test_dual_query_functionality(TreeType)
188-
test_geometry_support(TreeType)
189-
test_find_point_in_all_countries(TreeType)
190-
end
168+
test_basic_interface(FlatNoTree)
169+
test_child_indices_extents(FlatNoTree)
170+
test_query_functionality(FlatNoTree)
171+
test_dual_query_functionality(FlatNoTree)
172+
test_geometry_support(FlatNoTree)
173+
test_find_point_in_all_countries(FlatNoTree)
191174
end
192175

193176
# Test STRtree implementation
194177
@testset "STRtree" begin
195-
@testset let TreeType = STRtree
196-
test_basic_interface(TreeType)
197-
test_child_indices_extents(TreeType)
198-
test_query_functionality(TreeType)
199-
test_dual_query_functionality(TreeType)
200-
test_geometry_support(TreeType)
201-
test_find_point_in_all_countries(TreeType)
202-
end
178+
test_basic_interface(STRtree)
179+
test_child_indices_extents(STRtree)
180+
test_query_functionality(STRtree)
181+
test_dual_query_functionality(STRtree)
182+
test_geometry_support(STRtree)
183+
test_find_point_in_all_countries(STRtree)
203184
end
204185

205186

206187

188+
# This testset is not used because Polylabel.jl has some issues.
189+
190+
#=
191+
192+
193+
@testset "Dual query functionality - every country's polylabel against every country" begin
194+
195+
# Note that this is a perfectly balanced tree query - we don't yet have a test for unbalanced
196+
# trees (but could easily add one, e.g. by getting polylabels of admin-1 or admin-2 regions)
197+
# from Natural Earth, or by using GADM across many countries.
198+
199+
all_countries = NaturalEarth.naturalearth("admin_0_countries", 10)
200+
all_adm0_a3 = all_countries.ADM0_A3
201+
all_geoms = all_countries.geometry
202+
# US minor outlying islands - bug in Polylabel.jl
203+
# A lot of small geoms have this issue, that there will be an error from the queue
204+
# because the cell exists in the queue already.
205+
# Not sure what to do about it, I don't want to check containment every time...
206+
deleteat!(all_adm0_a3, 205)
207+
deleteat!(all_geoms, 205)
208+
209+
geom_tree = TreeType(all_geoms)
210+
211+
polylabels = [Polylabel.polylabel(geom; rtol = 0.019) for geom in all_geoms]
212+
polylabel_tree = TreeType(polylabels)
213+
214+
found_countries = falses(length(polylabels))
215+
216+
dual_depth_first_search(Extents.intersects, geom_tree, polylabel_tree) do i, j
217+
if i == j
218+
found_countries[i] = true
219+
end
220+
end
221+
222+
@test all(found_countries)
223+
end
224+
=#

0 commit comments

Comments
 (0)