Skip to content

Commit 416de76

Browse files
committed
fix crs on intersection
1 parent fdda925 commit 416de76

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

src/methods/clipping/clipping_processor.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -937,11 +937,12 @@ A list of GeoInterface polygons is returned from this function.
937937
Note: `poly_a` and `poly_b` are temporary inputs used for debugging and can be removed
938938
eventually.
939939
=#
940-
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}
940+
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}
941941
n_a_pts, n_b_pts = length(a_list), length(b_list)
942942
total_pts = n_a_pts + n_b_pts
943943
n_cross_pts = length(a_idx_list)
944-
return_polys = Vector{_get_poly_type(T)}(undef, 0)
944+
945+
return_polys = Vector{_get_poly_type(T, crs)}(undef, 0)
945946
# Keep track of number of processed intersection points
946947
visited_pts = 0
947948
processed_pts = 0
@@ -996,15 +997,16 @@ function _trace_polynodes(alg::FosterHormannClipping{M, A}, ::Type{T}, a_list, b
996997
idx = curr.neighbor
997998
curr = curr_list[idx]
998999
end
999-
push!(return_polys, GI.Polygon([pt_list]))
1000+
push!(return_polys, GI.Polygon([GI.LinearRing(pt_list; crs)]; crs))
10001001
end
10011002
return return_polys
10021003
end
10031004

10041005
# Get type of polygons that will be made
10051006
# TODO: Increase type options
1006-
_get_poly_type(::Type{T}) where T =
1007-
GI.Polygon{false, false, Vector{GI.LinearRing{false, false, Vector{Tuple{T, T}}, Nothing, Nothing}}, Nothing, Nothing}
1007+
function _get_poly_type(::Type{T}, crs::CRST = nothing) where {T, CRST}
1008+
GI.Polygon{false, false, Vector{GI.LinearRing{false, false, Vector{Tuple{T, T}}, Nothing, CRST}}, Nothing, CRST}
1009+
end
10081010

10091011
#=
10101012
_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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,21 @@ end
296296
_geometry_or_error(g::Extents.Extent; kw...) = g
297297

298298

299+
"""
300+
mutual_crs(a, b)
301+
302+
Return the CRS of `a` if it is the same as `b`, or `nothing` if they are not the same.
303+
304+
This is tolerant of `nothing` values in one of a or b, it assumes that they are the same crs.
305+
"""
306+
function mutual_crs(a, b)
307+
if GI.crs(a) == GI.crs(b)
308+
return GI.crs(a)
309+
elseif GI.crs(a) == nothing && GI.crs(b) != nothing
310+
return GI.crs(b)
311+
elseif GI.crs(a) != nothing && GI.crs(b) == nothing
312+
return GI.crs(a)
313+
else
314+
return nothing
315+
end
316+
end

0 commit comments

Comments
 (0)