Skip to content

Commit 846d99a

Browse files
committed
add MultiPoint and change MultiLineString field name
1 parent 7e00842 commit 846d99a

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/basic_types.jl

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,10 @@ struct MultiPolygon{
265265
Element <: AbstractPolygon{Dim, T},
266266
A <: AbstractVector{Element}
267267
} <: AbstractVector{Element}
268+
268269
polygons::A
269270
end
270271

271-
272-
273272
function MultiPolygon(polygons::AbstractVector{P}; kw...) where P <: AbstractPolygon{Dim, T} where {Dim, T}
274273
MultiPolygon(meta(polygons; kw...))
275274
end
@@ -283,9 +282,32 @@ struct MultiLineString{
283282
A <: AbstractVector{Element}
284283
} <: AbstractVector{Element}
285284

286-
polygons::A
285+
linestrings::A
287286
end
288287

288+
function MultiLineString(linestrings::AbstractVector{L}; kw...) where L <: AbstractVector{LineP{Dim, T, P}} where {Dim, T, P}
289+
MultiLineString(meta(linestrings; kw...))
290+
end
291+
292+
Base.getindex(ms::MultiLineString, i) = ms.linestrings[i]
293+
Base.size(ms::MultiLineString) = size(ms.linestrings)
294+
295+
struct MultiPoint{
296+
Dim, T <: Real,
297+
Element <: Point{Dim, T},
298+
A <: AbstractVector{Element}
299+
} <: AbstractVector{Element}
300+
301+
points::A
302+
end
303+
304+
function MultiPoint(points::AbstractVector{P}; kw...) where P <: AbstractPoint{Dim, T} where {Dim, T}
305+
MultiPoint(meta(points; kw...))
306+
end
307+
308+
Base.getindex(mpt::MultiPoint, i) = mpt.points[i]
309+
Base.size(mpt::MultiPoint) = size(mpt.points)
310+
289311
struct Mesh{
290312
Dim, T <: Real,
291313
Element <: Polytope{Dim, T},

test/runtests.jl

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using GeometryBasics
2-
using GeometryBasics: Polygon, MultiPolygon, Point, LineFace, Polytope, Line
2+
using GeometryBasics: LineFace, Polytope, Line
3+
using GeometryBasics: Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon
34
using GeometryBasics: Simplex, connect, Triangle, NSimplex, Tetrahedron
45
using GeometryBasics: QuadFace, hascolumn, getcolumn, metafree, coordinates, TetrahedronFace
5-
using GeometryBasics: TupleView, TriangleFace, SimplexFace, LineString, Mesh, meta, column_names
6+
using GeometryBasics: TupleView, TriangleFace, SimplexFace, Mesh, meta, column_names
67
using Test, Random, Query, StructArrays, Tables
78
using StaticArrays
89

@@ -235,5 +236,35 @@ end
235236

236237
end
237238

239+
@testset "Multi geometries" begin
240+
# coordinates from https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Geometric_objects
241+
points = Point{2, Int}[(10, 40), (40, 30), (20, 20), (30, 10)]
242+
multipoint = MultiPoint(points)
243+
@test size(multipoint) === size(points)
244+
@test multipoint[3] === points[3]
245+
246+
linestring1 = LineString(Point{2, Int}[(10, 10), (20, 20), (10, 40)])
247+
linestring2 = LineString(Point{2, Int}[(40, 40), (30, 30), (40, 20), (30, 10)])
248+
multilinestring = MultiLineString([linestring1, linestring2])
249+
@test size(multilinestring) === (2,)
250+
@test multilinestring[1] === linestring1
251+
@test multilinestring[2] === linestring2
252+
253+
polygon11 = Polygon(Point{2, Int}[(30, 20), (45, 40), (10, 40), (30, 20)])
254+
polygon12 = Polygon(Point{2, Int}[(15, 5), (40, 10), (10, 20), (5, 10), (15, 5)])
255+
multipolygon1 = MultiPolygon([polygon11, polygon12])
256+
@test size(multipolygon1) === (2,)
257+
@test multipolygon1[1] === polygon11
258+
@test multipolygon1[2] === polygon12
259+
260+
polygon21 = Polygon(Point{2, Int}[(40, 40), (20, 45), (45, 30), (40, 40)])
261+
polygon22 = Polygon(LineString(Point{2, Int}[(20, 35), (10, 30), (10, 10), (30, 5), (45, 20), (20, 35)]),
262+
[LineString(Point{2, Int}[(30, 20), (20, 15), (20, 25), (30, 20)])])
263+
multipolygon2 = MultiPolygon([polygon21, polygon22])
264+
@test size(multipolygon2) === (2,)
265+
@test multipolygon2[1] === polygon21
266+
@test multipolygon2[2] === polygon22
267+
end
268+
238269
end
239270
end

0 commit comments

Comments
 (0)