Skip to content

Commit b1ae5f5

Browse files
committed
add poly promotion for MultiPolygon
1 parent 358d4d1 commit b1ae5f5

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/basic_types.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,17 @@ function coordinates(polygon::Polygon{N,T}) where {N,T}
321321
end
322322
end
323323

324+
function Base.promote_rule(::Type{Polygon{N, T1}}, ::Type{Polygon{N, T2}}) where {N, T1, T2}
325+
return Polygon{N, promote_rule(T1, T2)}
326+
end
327+
328+
function Base.convert(::Type{Polygon{N, T}}, poly::Polygon{N}) where {N, T}
329+
return Polygon(
330+
convert(Vector{Point{N, T}}, poly.exterior),
331+
convert(Vector{Vector{Point{N, T}}}, poly.interiors),
332+
)
333+
end
334+
324335
"""
325336
MultiPolygon(polygons::AbstractPolygon)
326337
@@ -333,6 +344,10 @@ end
333344
function MultiPolygon(polygons::AbstractVector{<:AbstractPolygon{Dim,T}}) where {Dim,T}
334345
return MultiPolygon(convert(Vector{eltype(polygons)}, polygons))
335346
end
347+
function MultiPolygon(polygons::AbstractVector{<:AbstractPolygon{Dim}}) where {Dim}
348+
T = reduce(promote_type, typeof.(polygons), init = eltype(polygons))
349+
return MultiPolygon(convert(Vector{T}, polygons))
350+
end
336351

337352
Base.getindex(mp::MultiPolygon, i) = mp.polygons[i]
338353
Base.size(mp::MultiPolygon) = size(mp.polygons)

test/polygons.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
p2 = Polygon(OffsetArray(exterior, 0), interiors)
1616
@test p2 == p1
1717

18-
# TODO: promote polygon type automatically when creating MultiPolygon
19-
polygon = Polygon(Point2f.(points))
18+
polygon = Polygon(points)
2019
mp = MultiPolygon([polygon, p1, p2])
2120
@test mp.polygons == [polygon, p1, p2]
2221
@test mp[1] == polygon
2322
@test mp[2] == p1
2423
@test size(mp) == (3,) # TODO: What does size even mean here?
2524
@test length(mp) == 3
2625
@test MultiPolygon(OffsetArray([polygon, p1, p2], 0)) == mp
26+
mp = MultiPolygon([Polygon(Point2f.()), p1, p2])
27+
2728
end
2829

2930
rect = Rect2f(0, 0, 1, 1)

0 commit comments

Comments
 (0)