Skip to content

Commit 0b17714

Browse files
committed
Add sweat tests for combinations of type mismatches
1 parent fbdd81a commit 0b17714

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using Test
2+
import GeometryOps as GO
3+
import GeoInterface as GI
4+
import LibGEOS as LG
5+
import ArchGDAL as AG
6+
using ..TestHelpers
7+
8+
9+
p1 = GI.Polygon([GI.LinearRing([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)])])
10+
p1_bothcrs = GI.Polygon([GI.LinearRing([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]; crs = 1)], crs = 1)
11+
p1_topcrs = GI.Polygon([GI.LinearRing([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)])], crs = 1)
12+
p1_extent = GO.tuples(p1_bothcrs; calc_extent = true)
13+
p1_bothcrs_extent = GO.tuples(p1_bothcrs; calc_extent = true)
14+
p1_multi = GI.MultiPolygon([p1])
15+
p1_multi_bothcrs = GI.MultiPolygon([p1_bothcrs]; crs = 1)
16+
p1_multi_topcrs = GI.MultiPolygon([p1_topcrs]; crs = 1)
17+
p1_multi_extent = GO.tuples(p1_multi_bothcrs; calc_extent = true)
18+
p1_multi_bothcrs_extent = GO.tuples(p1_multi_bothcrs; calc_extent = true)
19+
p1_multi_topcrs_extent = GO.tuples(p1_multi_topcrs; calc_extent = true)
20+
21+
p2 = GO.transform(x -> x .+ (0.5, 0.0), p1)
22+
p2_bothcrs = GO.transform(x -> x .+ (0.5, 0.0), p1_bothcrs)
23+
p2_topcrs = GO.transform(x -> x .+ (0.5, 0.0), p1_topcrs)
24+
p2_extent = GO.tuples(p2_bothcrs; calc_extent = true)
25+
p2_bothcrs_extent = GO.tuples(p2_bothcrs; calc_extent = true)
26+
p2_multi = GI.MultiPolygon([p2])
27+
p2_multi_bothcrs = GI.MultiPolygon([p2_bothcrs]; crs = 1)
28+
p2_multi_topcrs = GI.MultiPolygon([p2_topcrs]; crs = 1)
29+
p2_multi_extent = GO.tuples(p2_multi_bothcrs; calc_extent = true)
30+
p2_multi_bothcrs_extent = GO.tuples(p2_multi_bothcrs; calc_extent = true)
31+
p2_multi_topcrs_extent = GO.tuples(p2_multi_topcrs; calc_extent = true)
32+
33+
p1s = zip(["p1", "p1_bothcrs", "p1_topcrs", "p1_extent", "p1_multi", "p1_multi_bothcrs", "p1_multi_topcrs", "p1_multi_extent", "p1_multi_bothcrs_extent", "p1_multi_topcrs_extent"], [p1, p1_bothcrs, p1_topcrs, p1_extent, p1_multi, p1_multi_bothcrs, p1_multi_topcrs, p1_multi_extent, p1_multi_bothcrs_extent, p1_multi_topcrs_extent])
34+
p2s = zip(["p2", "p2_bothcrs", "p2_topcrs", "p2_extent", "p2_multi", "p2_multi_bothcrs", "p2_multi_topcrs", "p2_multi_extent", "p2_multi_bothcrs_extent", "p2_multi_topcrs_extent"], [p2, p2_bothcrs, p2_topcrs, p2_extent, p2_multi, p2_multi_bothcrs, p2_multi_topcrs, p2_multi_extent, p2_multi_bothcrs_extent, p2_multi_topcrs_extent])
35+
36+
function _test_falseiferror(f, args...; kwargs...)
37+
try
38+
f(args...; kwargs...)
39+
return true
40+
catch e
41+
return false
42+
end
43+
end
44+
45+
@testset "Type mismatches" begin
46+
for (fname, func) in zip(["intersection", "union", "difference"], [GO.intersection, GO.union, GO.difference])
47+
@testset "$fname" begin
48+
for ((p1_name, p1_geom), (p2_name, p2_geom)) in Iterators.product(p1s, p2s)
49+
@testset_implementations "$fname $p1_name x $p2_name" begin
50+
result = _test_falseiferror(func, $p1_geom, $p2_geom; target = GI.PolygonTrait)
51+
@test result
52+
end
53+
end
54+
end
55+
end
56+
end
57+
58+
@testset "Specifically ArchGDAL vs other things" begin
59+
60+
# Create simple test polygons as GI.Polygon
61+
coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)]
62+
gi_poly1 = GI.Polygon([GI.LinearRing(coords)])
63+
64+
# Create a second overlapping polygon
65+
coords2 = [(0.5, 0.0), (1.5, 0.0), (1.5, 1.0), (0.5, 1.0), (0.5, 0.0)]
66+
gi_poly2 = GI.Polygon([GI.LinearRing(coords2)])
67+
68+
# Create MultiPolygons
69+
gi_multi1 = GI.MultiPolygon([gi_poly1])
70+
gi_multi2 = GI.MultiPolygon([gi_poly2])
71+
72+
73+
# Convert to ArchGDAL geometries
74+
ag_poly1 = GI.convert(AG, gi_poly1)
75+
ag_poly2 = GI.convert(AG, gi_poly2)
76+
ag_multi1 = GI.convert(AG, gi_multi1)
77+
ag_multi2 = GI.convert(AG, gi_multi2)
78+
79+
# Convert to LibGEOS geometries
80+
lg_poly1 = GI.convert(LG, gi_poly1)
81+
lg_poly2 = GI.convert(LG, gi_poly2)
82+
lg_multi1 = GI.convert(LG, gi_multi1)
83+
lg_multi2 = GI.convert(LG, gi_multi2)
84+
85+
# Test matrix: all combinations (Polygons and MultiPolygons)
86+
geom_types = [
87+
("GI.Polygon", gi_poly1, gi_poly2),
88+
("GI.MultiPolygon", gi_multi1, gi_multi2),
89+
("AG.Polygon", ag_poly1, ag_poly2),
90+
("AG.MultiPolygon", ag_multi1, ag_multi2),
91+
("LG.Polygon", lg_poly1, lg_poly2),
92+
("LG.MultiPolygon", lg_multi1, lg_multi2),
93+
]
94+
95+
operations = [
96+
("intersection", GO.intersection),
97+
("union", GO.union),
98+
("difference", GO.difference),
99+
]
100+
101+
for (op_name, op_func) in operations
102+
@testset "$op_name" begin
103+
for (type1_name, poly1a, poly1b) in geom_types
104+
for (type2_name, poly2a, poly2b) in geom_types
105+
@testset "$type1_name x $type2_name" begin
106+
result = _test_falseiferror(op_func, poly1a, poly2a; target = GI.PolygonTrait)
107+
@test result
108+
end
109+
end
110+
end
111+
end
112+
end
113+
114+
@testset "Intersection with polygon with crs v/s without crs" begin
115+
@test GI.crs(first(GO.intersection(gi_poly_crs, gi_poly2; target = GI.PolygonTrait))) == GI.crs(gi_poly_crs)
116+
@test GI.crs(first(GO.intersection(ag_poly1, gi_poly_crs; target = GI.PolygonTrait))) == GI.crs(gi_poly_crs)
117+
end
118+
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ using Test
33
using SafeTestsets
44

55
include("helpers.jl")
6-
76
@testset "Core" begin
87
@safetestset "Algorithm" begin include("core/algorithm.jl") end
98
@safetestset "Manifold" begin include("core/manifold.jl") end
@@ -36,6 +35,7 @@ end
3635
@safetestset "Cut" begin include("methods/clipping/cut.jl") end
3736
@safetestset "Intersection Point" begin include("methods/clipping/intersection_points.jl") end
3837
@safetestset "Polygon Clipping" begin include("methods/clipping/polygon_clipping.jl") end
38+
@safetestset "Clipping Type mismatches" begin include("methods/clipping/type_mismatches.jl") end
3939
# Transformations
4040
@safetestset "Embed Extent" begin include("transformations/extent.jl") end
4141
@safetestset "Reproject" begin include("transformations/reproject.jl") end

0 commit comments

Comments
 (0)