Skip to content

Commit 7a9d261

Browse files
asinghvi17claude
andauthored
GeoInterface improvements (#264)
* Collect all LinearRingTrant to LineStringTrait * Add Line convert method * Add tests for Line and LinearRing GeoInterface conversions Tests the new GeoInterface.convert for LineTrait → Line (2D and 3D) and LinearRingTrait → LineString (2D and 3D) to improve codecov. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4bd7a23 commit 7a9d261

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

ext/GeometryBasicsGeoInterfaceExt.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ geointerface_geomtype(::GeoInterface.PointTrait) = Point
3434
geointerface_geomtype(::GeoInterface.MultiPointTrait) = MultiPoint
3535
geointerface_geomtype(::GeoInterface.LineTrait) = Line
3636
geointerface_geomtype(::GeoInterface.LineStringTrait) = LineString
37+
geointerface_geomtype(::GeoInterface.LinearRingTrait) = LineString
3738
geointerface_geomtype(::GeoInterface.MultiLineStringTrait) = MultiLineString
3839
geointerface_geomtype(::GeoInterface.PolygonTrait) = Polygon
3940
geointerface_geomtype(::GeoInterface.MultiPolygonTrait) = MultiPolygon
@@ -111,7 +112,19 @@ function GeoInterface.convert(::Type{Point}, type::PointTrait, geom)
111112
return Point{2,T}(x, y)
112113
end
113114
end
114-
function GeoInterface.convert(::Type{LineString}, type::LineStringTrait, geom)
115+
function GeoInterface.convert(::Type{Line}, type::LineTrait, geom)
116+
g1, g2 = GeoInterface.getgeom(geom)
117+
x, y = GeoInterface.x(g1), GeoInterface.y(g1)
118+
if GeoInterface.is3d(geom)
119+
z = GeoInterface.z(g1)
120+
T = promote_type(typeof(x), typeof(y), typeof(z))
121+
return Line{3,T}(Point{3,T}(x, y, z), Point{3,T}(GeoInterface.x(g2), GeoInterface.y(g2), GeoInterface.z(g2)))
122+
else
123+
T = promote_type(typeof(x), typeof(y))
124+
return Line{2,T}(Point{2,T}(x, y), Point{2,T}(GeoInterface.x(g2), GeoInterface.y(g2)))
125+
end
126+
end
127+
function GeoInterface.convert(::Type{LineString}, type::Union{LineStringTrait, LinearRingTrait}, geom)
115128
g1 = getgeom(geom, 1)
116129
x, y = GeoInterface.x(g1), GeoInterface.y(g1)
117130
if GeoInterface.is3d(geom)

test/geointerface.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,31 @@ end
210210
@test mls isa MultiLineString{2, Float64}
211211
@test length(mls) == 2
212212
end
213+
214+
@testset "Convert Line and LinearRing" begin
215+
# Line (2D)
216+
gi_line_2d = GeoInterface.Line([(1.0, 2.0), (3.0, 4.0)])
217+
line_2d = GeoInterface.convert(GeometryBasics, gi_line_2d)
218+
@test line_2d isa Line{2, Float64}
219+
@test line_2d[1] == Point(1.0, 2.0)
220+
@test line_2d[2] == Point(3.0, 4.0)
221+
222+
# Line (3D)
223+
gi_line_3d = GeoInterface.Line([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)])
224+
line_3d = GeoInterface.convert(GeometryBasics, gi_line_3d)
225+
@test line_3d isa Line{3, Float64}
226+
@test line_3d[1] == Point(1.0, 2.0, 3.0)
227+
@test line_3d[2] == Point(4.0, 5.0, 6.0)
228+
229+
# LinearRing → LineString (2D)
230+
gi_ring = GeoInterface.LinearRing([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)])
231+
ls_from_ring = GeoInterface.convert(GeometryBasics, gi_ring)
232+
@test ls_from_ring isa LineString{2, Float64}
233+
@test length(ls_from_ring) == 4
234+
235+
# LinearRing → LineString (3D)
236+
gi_ring_3d = GeoInterface.LinearRing([(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 1.0, 0.0), (0.0, 0.0, 0.0)])
237+
ls_from_ring_3d = GeoInterface.convert(GeometryBasics, gi_ring_3d)
238+
@test ls_from_ring_3d isa LineString{3, Float64}
239+
@test length(ls_from_ring_3d) == 4
240+
end

0 commit comments

Comments
 (0)