Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix type stability of clipping methods #74

Merged
merged 8 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/methods/clipping/clipping_processor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ function _add_holes_to_polys!(::Type{T}, return_polys, hole_iterator) where T
if !on_ext && !out_ext # hole is completly within exterior
push!(curr_poly.geom, new_hole)
else # hole is partially within and outside of polygon's exterior
new_polys = difference(curr_poly_ext, new_hole_poly, T; target = GI.PolygonTrait)
new_polys = difference(curr_poly_ext, new_hole_poly, T; target=GI.PolygonTrait())
n_new_polys = length(new_polys) - 1
# replace original -> can't have a hole
curr_poly.geom[1] = GI.getexterior(new_polys[1])
Expand Down Expand Up @@ -549,7 +549,7 @@ function _combine_holes!(::Type{T}, new_hole, curr_poly, return_polys) where T
old_hole_poly = GI.Polygon([old_hole])
if intersects(new_hole_poly, old_hole_poly)
# If the holes intersect, combine them into a bigger hole
hole_union = union(new_hole_poly, old_hole_poly, T; target = GI.PolygonTrait)[1]
hole_union = union(new_hole_poly, old_hole_poly, T; target = GI.PolygonTrait())[1]
push!(remove_idx, k + 1)
new_hole = GI.getexterior(hole_union)
new_hole_poly = GI.Polygon([new_hole])
Expand Down
6 changes: 3 additions & 3 deletions src/methods/clipping/difference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import GeoInterface as GI, GeometryOps as GO

poly1 = GI.Polygon([[[0.0, 0.0], [5.0, 5.0], [10.0, 0.0], [5.0, -5.0], [0.0, 0.0]]])
poly2 = GI.Polygon([[[3.0, 0.0], [8.0, 5.0], [13.0, 0.0], [8.0, -5.0], [3.0, 0.0]]])
diff_poly = GO.difference(poly1, poly2; target = GI.PolygonTrait)
diff_poly = GO.difference(poly1, poly2; target = GI.PolygonTrait())
GI.coordinates.(diff_poly)

# output
Expand All @@ -27,7 +27,7 @@ GI.coordinates.(diff_poly)
```
"""
function difference(
geom_a, geom_b, ::Type{T} = Float64; target::Type{Target} = Nothing,
geom_a, geom_b, ::Type{T} = Float64; target::Target = nothing,
) where {T <: AbstractFloat, Target <: Union{Nothing, GI.AbstractTrait}}
return _difference(Target, T, GI.trait(geom_a), geom_a, GI.trait(geom_b), geom_b)
end
rafaqz marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -65,7 +65,7 @@ function _difference(
if GI.nhole(poly_a) != 0 || GI.nhole(poly_b) != 0
_add_holes_to_polys!(T, polys, GI.gethole(poly_a))
for hole in GI.gethole(poly_b)
new_polys = intersection(GI.Polygon([hole]), poly_a, T; target = GI.PolygonTrait)
new_polys = intersection(GI.Polygon([hole]), poly_a, T; target = GI.PolygonTrait())
if length(new_polys) > 0
append!(polys, new_polys)
end
Expand Down
6 changes: 3 additions & 3 deletions src/methods/clipping/intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import GeoInterface as GI, GeometryOps as GO

line1 = GI.Line([(124.584961,-12.768946), (126.738281,-17.224758)])
line2 = GI.Line([(123.354492,-15.961329), (127.22168,-14.008696)])
inter_points = GO.intersection(line1, line2; target = GI.PointTrait)
inter_points = GO.intersection(line1, line2; target = GI.PointTrait())
GI.coordinates.(inter_points)

# output
Expand All @@ -27,7 +27,7 @@ GI.coordinates.(inter_points)
```
"""
function intersection(
geom_a, geom_b, ::Type{T} = Float64; target::Type{Target} = Nothing,
geom_a, geom_b, ::Type{T} = Float64; target::Target = nothing,
) where {T <: AbstractFloat, Target <: Union{Nothing, GI.AbstractTrait}}
return _intersection(Target, T, GI.trait(geom_a), geom_a, GI.trait(geom_b), geom_b)
end
Expand Down Expand Up @@ -177,4 +177,4 @@ function _intersection_point(::Type{T}, (a1, a2)::Tuple, (b1, b2)::Tuple) where
nothing, nothing
end
return point, fracs
end
end
6 changes: 3 additions & 3 deletions src/methods/clipping/union.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import GeoInterface as GI, GeometryOps as GO

p1 = GI.Polygon([[(0.0, 0.0), (5.0, 5.0), (10.0, 0.0), (5.0, -5.0), (0.0, 0.0)]])
p2 = GI.Polygon([[(3.0, 0.0), (8.0, 5.0), (13.0, 0.0), (8.0, -5.0), (3.0, 0.0)]])
union_poly = GO.union(p1, p2; target = GI.PolygonTrait)
union_poly = GO.union(p1, p2; target = GI.PolygonTrait())
GI.coordinates.(union_poly)

# output
Expand All @@ -27,7 +27,7 @@ GI.coordinates.(union_poly)
```
"""
function union(
geom_a, geom_b, ::Type{T} = Float64; target::Type{Target} = Nothing,
geom_a, geom_b, ::Type{T} = Float64; target::Target = nothing,
) where {T <: AbstractFloat, Target <: Union{Nothing, GI.AbstractTrait}}
_union(Target, T, GI.trait(geom_a), geom_a, GI.trait(geom_b), geom_b)
end
Expand Down Expand Up @@ -82,4 +82,4 @@ function _union(
) where {Target, T}
throw(ArgumentError("Union between $trait_a and $trait_b with target $Target isn't implemented yet."))
return nothing
end
end
4 changes: 2 additions & 2 deletions test/methods/clipping/polygon_clipping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ test_pairs = [
const ϵ = 1e-10
# Compare clipping results from GeometryOps and LibGEOS
function compare_GO_LG_clipping(GO_f, LG_f, p1, p2)
GO_result_list = GO_f(p1, p2; target = GI.PolygonTrait)
GO_result_list = GO_f(p1, p2; target = GI.PolygonTrait())
LG_result_geom = LG_f(p1, p2)
if LG_result_geom isa LG.GeometryCollection
poly_list = LG.Polygon[]
Expand Down Expand Up @@ -156,4 +156,4 @@ end

@testset "Intersection" begin test_clipping(GO.intersection, LG.intersection, "intersection") end
@testset "Union" begin test_clipping(GO.union, LG.union, "union") end
@testset "Difference" begin test_clipping(GO.difference, LG.difference, "difference") end
@testset "Difference" begin test_clipping(GO.difference, LG.difference, "difference") end
Loading