Skip to content

Commit b28b9a4

Browse files
committed
Fixed LineString coordinates. Added more tests.
1 parent 53d85c3 commit b28b9a4

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

src/GeometryBasics.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module GeometryBasics
22

3-
using StaticArrays, Tables, StructArrays, IterTools, LinearAlgebra, GeoInterface
3+
using StaticArrays, Tables, StructArrays, IterTools, LinearAlgebra
4+
using GeoInterface: GeoInterface
45
using EarCut_jll
56

67
using Base: @propagate_inbounds

src/basic_types.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ Fixed Size Polygon, e.g.
5757
- ...
5858
"""
5959
struct Ngon{Dim,T<:Real,N,Point<:AbstractPoint{Dim,T}} <: AbstractPolygon{Dim,T}
60-
6160
points::SVector{N,Point}
6261
end
6362

@@ -137,7 +136,6 @@ It applies to infinite dimensions. The structure of this type is designed
137136
to allow embedding in higher-order spaces by parameterizing on `T`.
138137
"""
139138
struct Simplex{Dim,T<:Real,N,Point<:AbstractPoint{Dim,T}} <: Polytope{Dim,T}
140-
141139
points::SVector{N,Point}
142140
end
143141

@@ -328,7 +326,6 @@ Base.size(mp::MultiPolygon) = size(mp.polygons)
328326

329327
struct MultiLineString{Dim,T<:Real,Element<:LineString{Dim,T},A<:AbstractVector{Element}} <:
330328
AbstractVector{Element}
331-
332329
linestrings::A
333330
end
334331

@@ -347,7 +344,6 @@ A collection of points
347344
"""
348345
struct MultiPoint{Dim,T<:Real,P<:AbstractPoint{Dim,T},A<:AbstractVector{P}} <:
349346
AbstractVector{P}
350-
351347
points::A
352348
end
353349

src/geointerface.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ GeoInterface.getcoord(::GeoInterface.PointTrait, g::Point, i::Int) = g[i]
3030
GeoInterface.ngeom(::GeoInterface.LineTrait, g::Line) = length(g)
3131
GeoInterface.getgeom(::GeoInterface.LineTrait, g::Line, i::Int) = g[i]
3232

33-
GeoInterface.ngeom(::GeoInterface.LineStringTrait, g::LineString) = length(g)
33+
GeoInterface.ngeom(::GeoInterface.LineStringTrait, g::LineString) = length(g) + 1 # n line segments + 1
3434
function GeoInterface.getgeom(::GeoInterface.LineStringTrait, g::LineString, i::Int)
35-
return coordinates(g)[i]
35+
return GeometryBasics.coordinates(g)[i]
3636
end
3737

3838
GeoInterface.ngeom(::GeoInterface.PolygonTrait, g::Polygon) = length(g.interiors) + 1 # +1 for exterior
39-
function GeoInterface.getgeom(::GeoInterface.PolygonTrait, g::Polygon, i::Int)
39+
function GeoInterface.getgeom(::GeoInterface.PolygonTrait,
40+
g::Polygon,
41+
i::Int)::typeof(g.exterior)
4042
return i > 1 ? g.interiors[i - 1] : g.exterior
4143
end
4244

@@ -66,9 +68,12 @@ function GeoInterface.ngeom(::GeoInterface.AbstractGeometryTrait,
6668
::Simplex{Dim,T,N,P}) where {Dim,T,N,P}
6769
return N
6870
end
69-
GeoInterface.ngeom(::PolygonTrait, ::Ngon) = 1 # can't have any holes
70-
GeoInterface.getgeom(::PolygonTrait, g::Ngon, _) = LineString(g.points)
71+
GeoInterface.ngeom(::GeoInterface.PolygonTrait, ::Ngon) = 1 # can't have any holes
72+
GeoInterface.getgeom(::GeoInterface.PolygonTrait, g::Ngon, _) = LineString(g.points)
7173

72-
GeoInterface.ncoord(::PolyhedralSurfaceTrait, ::Mesh{Dim,T,E,V} where {Dim,T,E,V}) = Dim
73-
GeoInterface.ngeom(::PolyhedralSurfaceTrait, g::AbstractMesh) = length(g)
74-
GeoInterface.getgeom(::PolyhedralSurfaceTrait, g::AbstractMesh, i) = g[i]
74+
function GeoInterface.ncoord(::GeoInterface.PolyhedralSurfaceTrait,
75+
::Mesh{Dim,T,E,V} where {Dim,T,E,V})
76+
return Dim
77+
end
78+
GeoInterface.ngeom(::GeoInterface.PolyhedralSurfaceTrait, g::AbstractMesh) = length(g)
79+
GeoInterface.getgeom(::GeoInterface.PolyhedralSurfaceTrait, g::AbstractMesh, i) = g[i]

test/geointerface.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
@testset "Basic types" begin
22
point = Point(2, 3)
33
GeoInterface.testgeometry(point)
4+
@test GeoInterface.ncoord(point) == 2
5+
@test GeoInterface.getcoord(point, 2) == 3
6+
@test GeoInterface.coordinates(point) == [2, 3]
47

58
mp = MultiPoint([point, point])
69
GeoInterface.testgeometry(mp)
10+
@test GeoInterface.ngeom(mp) == 2
11+
@test GeoInterface.getgeom(mp, 2) == point
12+
@test GeoInterface.coordinates(mp) == [[2, 3], [2, 3]]
713

814
linestring = LineString(Point{2,Int}[(10, 10), (20, 20), (10, 40)])
915
GeoInterface.testgeometry(linestring)
16+
@test GeoInterface.ngeom(linestring) == 3
17+
@test GeoInterface.getgeom(linestring, 1) == Point(10, 10)
18+
@test GeoInterface.getgeom(linestring, 2) == Point(20, 20)
19+
@test GeoInterface.getgeom(linestring, 3) == Point(10, 40)
20+
@test GeoInterface.coordinates(linestring) == [[10, 10], [20, 20], [10, 40]]
1021

1122
multilinestring = MultiLineString([linestring, linestring])
1223
GeoInterface.testgeometry(multilinestring)
24+
@test GeoInterface.coordinates(multilinestring) ==
25+
[[[10, 10], [20, 20], [10, 40]], [[10, 10], [20, 20], [10, 40]]]
1326

14-
poly = Polygon(rand(Point{2,Float32}, 5))
27+
poly = Polygon(rand(Point{2,Float32}, 5), [rand(Point{2,Float32}, 5)])
1528
GeoInterface.testgeometry(poly)
29+
@test length(GeoInterface.coordinates(poly)) == 2
30+
@test length(GeoInterface.coordinates(poly)[1]) == 5
1631

1732
triangle = Triangle(point, point, point)
1833
GeoInterface.testgeometry(triangle)
34+
@test length(GeoInterface.coordinates(triangle)) == 1
35+
@test length(GeoInterface.coordinates(triangle)[1]) == 3
1936

2037
polys = MultiPolygon([poly, poly])
2138
GeoInterface.testgeometry(polys)
39+
@test length(GeoInterface.coordinates(polys)) == 2
40+
@test length(GeoInterface.coordinates(polys)[1]) == 2
41+
@test length(GeoInterface.coordinates(polys)[1][1]) == 5
2242
end
2343

2444
@testset "Mesh" begin

0 commit comments

Comments
 (0)