Skip to content

Commit 83a0844

Browse files
asinghvi17claude
andcommitted
Add LineString constructor for AbstractVector
Fixes GI.convert failing when geometry is backed by StaticArrays. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9918b18 commit 83a0844

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/basic_types.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ A LineString is a collection of points connected by line segments.
303303
struct LineString{Dim, T<:Real} <: AbstractGeometry{Dim, T}
304304
points::Vector{Point{Dim, T}}
305305
end
306+
307+
function LineString(points::AbstractVector{Point{Dim, T}}) where {Dim, T}
308+
return LineString{Dim,T}(convert(Vector{Point{Dim, T}}, points))
309+
end
310+
306311
Base.length(ls::LineString) = length(coordinates(ls))
307312
Base.:(==)(a::LineString, b::LineString) = a.points == b.points
308313
coordinates(ls::LineString) = ls.points

test/geointerface.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,26 @@ end
129129
@test ext.Y == (0.0f0, 1.0f0)
130130
@test ext.Z == (0.0f0, 1.0f0)
131131
end
132+
133+
@testset "Convert from StaticArrays" begin
134+
using GeometryBasics.StaticArrays
135+
136+
# LineString from StaticVector
137+
ls = GeoInterface.convert(GeometryBasics, GeoInterface.LineString(SA[(1.0, 2.0), (3.0, 4.0)]))
138+
@test ls isa LineString{2, Float64}
139+
@test length(ls) == 2
140+
@test ls.points[1] == Point(1.0, 2.0)
141+
@test ls.points[2] == Point(3.0, 4.0)
142+
143+
# Polygon from StaticVector rings
144+
ring = GeoInterface.LinearRing(SA[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)])
145+
poly = GeoInterface.convert(GeometryBasics, GeoInterface.Polygon([ring]))
146+
@test poly isa Polygon{2, Float64}
147+
148+
# MultiLineString from StaticVector
149+
line1 = GeoInterface.LineString(SA[(1.0, 2.0), (3.0, 4.0)])
150+
line2 = GeoInterface.LineString(SA[(5.0, 6.0), (7.0, 8.0)])
151+
mls = GeoInterface.convert(GeometryBasics, GeoInterface.MultiLineString([line1, line2]))
152+
@test mls isa MultiLineString{2, Float64}
153+
@test length(mls) == 2
154+
end

0 commit comments

Comments
 (0)