Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ext/GeometryBasicsGeoInterfaceExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ geointerface_geomtype(::GeoInterface.PointTrait) = Point
geointerface_geomtype(::GeoInterface.MultiPointTrait) = MultiPoint
geointerface_geomtype(::GeoInterface.LineTrait) = Line
geointerface_geomtype(::GeoInterface.LineStringTrait) = LineString
geointerface_geomtype(::GeoInterface.LinearRingTrait) = LineString

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol I just ran into this again, we should get this in!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I have also fixed this multiple times locally 😭

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get the codecov bot onto it? I'm not a member here

geointerface_geomtype(::GeoInterface.MultiLineStringTrait) = MultiLineString
geointerface_geomtype(::GeoInterface.PolygonTrait) = Polygon
geointerface_geomtype(::GeoInterface.MultiPolygonTrait) = MultiPolygon
Expand Down Expand Up @@ -111,7 +112,19 @@ function GeoInterface.convert(::Type{Point}, type::PointTrait, geom)
return Point{2,T}(x, y)
end
end
function GeoInterface.convert(::Type{LineString}, type::LineStringTrait, geom)
function GeoInterface.convert(::Type{Line}, type::LineTrait, geom)
g1, g2 = GeoInterface.getgeom(geom)
x, y = GeoInterface.x(g1), GeoInterface.y(g1)
if GeoInterface.is3d(geom)
z = GeoInterface.z(g1)
T = promote_type(typeof(x), typeof(y), typeof(z))
return Line{3,T}(Point{3,T}(x, y, z), Point{3,T}(GeoInterface.x(g2), GeoInterface.y(g2), GeoInterface.z(g2)))
else
T = promote_type(typeof(x), typeof(y))
return Line{2,T}(Point{2,T}(x, y), Point{2,T}(GeoInterface.x(g2), GeoInterface.y(g2)))
end
end
function GeoInterface.convert(::Type{LineString}, type::Union{LineStringTrait, LinearRingTrait}, geom)
g1 = getgeom(geom, 1)
x, y = GeoInterface.x(g1), GeoInterface.y(g1)
if GeoInterface.is3d(geom)
Expand Down
28 changes: 28 additions & 0 deletions test/geointerface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,31 @@ end
@test mls isa MultiLineString{2, Float64}
@test length(mls) == 2
end

@testset "Convert Line and LinearRing" begin
# Line (2D)
gi_line_2d = GeoInterface.Line([(1.0, 2.0), (3.0, 4.0)])
line_2d = GeoInterface.convert(GeometryBasics, gi_line_2d)
@test line_2d isa Line{2, Float64}
@test line_2d[1] == Point(1.0, 2.0)
@test line_2d[2] == Point(3.0, 4.0)

# Line (3D)
gi_line_3d = GeoInterface.Line([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)])
line_3d = GeoInterface.convert(GeometryBasics, gi_line_3d)
@test line_3d isa Line{3, Float64}
@test line_3d[1] == Point(1.0, 2.0, 3.0)
@test line_3d[2] == Point(4.0, 5.0, 6.0)

# LinearRing → LineString (2D)
gi_ring = GeoInterface.LinearRing([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)])
ls_from_ring = GeoInterface.convert(GeometryBasics, gi_ring)
@test ls_from_ring isa LineString{2, Float64}
@test length(ls_from_ring) == 4

# LinearRing → LineString (3D)
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)])
ls_from_ring_3d = GeoInterface.convert(GeometryBasics, gi_ring_3d)
@test ls_from_ring_3d isa LineString{3, Float64}
@test length(ls_from_ring_3d) == 4
end
Loading