Skip to content

Commit 5390016

Browse files
committed
fix crs on intersection
1 parent ff98edd commit 5390016

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/methods/clipping/clipping_processor.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,12 @@ A list of GeoInterface polygons is returned from this function.
630630
Note: `poly_a` and `poly_b` are temporary inputs used for debugging and can be removed
631631
eventually.
632632
=#
633-
function _trace_polynodes(alg::FosterHormannClipping{M, A}, ::Type{T}, a_list, b_list, a_idx_list, f_step, poly_a, poly_b) where {T, M, A}
633+
function _trace_polynodes(alg::FosterHormannClipping{M, A}, ::Type{T}, a_list, b_list, a_idx_list, f_step, poly_a, poly_b; crs = mutual_crs(poly_a, poly_b)) where {T, M, A}
634634
n_a_pts, n_b_pts = length(a_list), length(b_list)
635635
total_pts = n_a_pts + n_b_pts
636636
n_cross_pts = length(a_idx_list)
637-
return_polys = Vector{_get_poly_type(T)}(undef, 0)
637+
638+
return_polys = Vector{_get_poly_type(T, crs)}(undef, 0)
638639
# Keep track of number of processed intersection points
639640
visited_pts = 0
640641
processed_pts = 0
@@ -689,15 +690,16 @@ function _trace_polynodes(alg::FosterHormannClipping{M, A}, ::Type{T}, a_list, b
689690
idx = curr.neighbor
690691
curr = curr_list[idx]
691692
end
692-
push!(return_polys, GI.Polygon([pt_list]))
693+
push!(return_polys, GI.Polygon([GI.LinearRing(pt_list; crs)]; crs))
693694
end
694695
return return_polys
695696
end
696697

697698
# Get type of polygons that will be made
698699
# TODO: Increase type options
699-
_get_poly_type(::Type{T}) where T =
700-
GI.Polygon{false, false, Vector{GI.LinearRing{false, false, Vector{Tuple{T, T}}, Nothing, Nothing}}, Nothing, Nothing}
700+
function _get_poly_type(::Type{T}, crs::CRST = nothing) where {T, CRST}
701+
GI.Polygon{false, false, Vector{GI.LinearRing{false, false, Vector{Tuple{T, T}}, Nothing, CRST}}, Nothing, CRST}
702+
end
701703

702704
#=
703705
_find_non_cross_orientation(a_list, b_list, a_poly, b_poly; exact)

src/methods/clipping/intersection.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,17 @@ function _intersection(
8080
# First we get the exteriors of 'poly_a' and 'poly_b'
8181
ext_a = GI.getexterior(poly_a)
8282
ext_b = GI.getexterior(poly_b)
83+
84+
crs = mutual_crs(poly_a, poly_b)
8385
# Then we find the intersection of the exteriors
8486
a_list, b_list, a_idx_list = _build_ab_list(alg, T, ext_a, ext_b, _inter_delay_cross_f, _inter_delay_bounce_f; exact)
8587
polys = _trace_polynodes(alg, T, a_list, b_list, a_idx_list, _inter_step, poly_a, poly_b)
8688
if isempty(polys) # no crossing points, determine if either poly is inside the other
8789
a_in_b, b_in_a = _find_non_cross_orientation(alg, a_list, b_list, ext_a, ext_b; exact)
8890
if a_in_b
89-
push!(polys, GI.Polygon([tuples(ext_a)]))
91+
push!(polys, GI.Polygon([tuples(ext_a; crs)]; crs))
9092
elseif b_in_a
91-
push!(polys, GI.Polygon([tuples(ext_b)]))
93+
push!(polys, GI.Polygon([tuples(ext_b; crs)]; crs))
9294
end
9395
end
9496
remove_idx = falses(length(polys))
@@ -130,7 +132,8 @@ function _intersection(
130132
if !isnothing(fix_multipoly) # Fix multipoly_b to prevent duplicated intersection regions
131133
multipoly_b = fix_multipoly(multipoly_b)
132134
end
133-
polys = Vector{_get_poly_type(T)}()
135+
crs = mutual_crs(poly_a, multipoly_b)
136+
polys = Vector{_get_poly_type(T, crs)}()
134137
for poly_b in GI.getpolygon(multipoly_b)
135138
append!(polys, intersection(alg, poly_a, poly_b; target))
136139
end
@@ -163,7 +166,8 @@ function _intersection(
163166
multipoly_b = fix_multipoly(multipoly_b)
164167
fix_multipoly = nothing
165168
end
166-
polys = Vector{_get_poly_type(T)}()
169+
crs = mutual_crs(multipoly_a, multipoly_b)
170+
polys = Vector{_get_poly_type(T, crs)}()
167171
for poly_a in GI.getpolygon(multipoly_a)
168172
append!(polys, intersection(alg, poly_a, multipoly_b; target, fix_multipoly))
169173
end

src/utils/utils.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,21 @@ end
289289

290290

291291

292+
"""
293+
mutual_crs(a, b)
292294
295+
Return the CRS of `a` if it is the same as `b`, or `nothing` if they are not the same.
293296
297+
This is tolerant of `nothing` values in one of a or b, it assumes that they are the same crs.
298+
"""
299+
function mutual_crs(a, b)
300+
if GI.crs(a) == GI.crs(b)
301+
return GI.crs(a)
302+
elseif GI.crs(a) == nothing && GI.crs(b) != nothing
303+
return GI.crs(b)
304+
elseif GI.crs(a) != nothing && GI.crs(b) == nothing
305+
return GI.crs(a)
306+
else
307+
return nothing
308+
end
309+
end

0 commit comments

Comments
 (0)