Skip to content

Commit

Permalink
Add DumpPoints method for MultiPoint type
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Jul 26, 2021
1 parent befe1f6 commit f5ea654
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
75 changes: 75 additions & 0 deletions geom/dump_coordinates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package geom_test

import (
"testing"

. "github.com/peterstace/simplefeatures/geom"
)

func TestDumpCoordinatesMultiPoint(t *testing.T) {
for _, tc := range []struct {
description string
inputWKT string
want []Coordinates
}{
{
description: "empty",
inputWKT: "MULTIPOINT EMPTY",
want: nil,
},
{
description: "contains empty point",
inputWKT: "MULTIPOINT(EMPTY)",
want: nil,
},
{
description: "single non-empty point",
inputWKT: "MULTIPOINT(1 2)",
want: []Coordinates{
NewXYCoordinates(1, 2),
},
},
{
description: "multiple non-empty points",
inputWKT: "MULTIPOINT(1 2,3 4)",
want: []Coordinates{
NewXYCoordinates(1, 2),
NewXYCoordinates(3, 4),
},
},
{
description: "mix of empty and non-empty points",
inputWKT: "MULTIPOINT(EMPTY,3 4)",
want: []Coordinates{
NewXYCoordinates(3, 4),
},
},
{
description: "Z coordinates",
inputWKT: "MULTIPOINT Z(3 4 5)",
want: []Coordinates{
NewXYZCoordinates(3, 4, 5),
},
},
{
description: "M coordinates",
inputWKT: "MULTIPOINT M(3 4 6)",
want: []Coordinates{
NewXYMCoordinates(3, 4, 6),
},
},
{
description: "ZM coordinates",
inputWKT: "MULTIPOINT ZM(3 4 5 6)",
want: []Coordinates{
NewXYZMCoordinates(3, 4, 5, 6),
},
},
} {
t.Run(tc.description, func(t *testing.T) {
mp := geomFromWKT(t, tc.inputWKT).AsMultiPoint()
got := mp.DumpCoordinates()
expectCoordinateSliceEq(t, got, tc.want)
})
}
}
40 changes: 39 additions & 1 deletion geom/type_coordinates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package geom

// Coordinates represents a point location.
// Coordinates represents a point location. Coordinates values may be
// constructed manually using the type definition directly. Alternatively, one
// of the New(XYZM)Coordinates constructor functions can be used.
type Coordinates struct {
// XY represents the XY position of the point location.
XY
Expand All @@ -18,3 +20,39 @@ type Coordinates struct {
// or not Z and M are populated.
Type CoordinatesType
}

// NewXYCoordinates constructs a new set of coordinates of type XY.
func NewXYCoordinates(x, y float64) Coordinates {
return Coordinates{
Type: DimXY,
XY: XY{x, y},
}
}

// NewXYZCoordinates constructs a new set of coordinates of type XYZ.
func NewXYZCoordinates(x, y, z float64) Coordinates {
return Coordinates{
Type: DimXYZ,
XY: XY{x, y},
Z: z,
}
}

// NewXYMCoordinates constructs a new set of coordinates of type XYM.
func NewXYMCoordinates(x, y, m float64) Coordinates {
return Coordinates{
Type: DimXYM,
XY: XY{x, y},
M: m,
}
}

// NewXYZMCoordinates constructs a new set of coordinates of type XYZM.
func NewXYZMCoordinates(x, y, z, m float64) Coordinates {
return Coordinates{
Type: DimXYZM,
XY: XY{x, y},
Z: z,
M: m,
}
}
14 changes: 14 additions & 0 deletions geom/type_multi_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,17 @@ func (m MultiPoint) Dump() []Point {
}
return pts
}

// DumpCoordinates returns the MultiPoint represented as a slice of
// coordinates.
func (m MultiPoint) DumpCoordinates() []Coordinates {
n := m.seq.Length()
coords := make([]Coordinates, 0, n)
for i := 0; i < n; i++ {
if m.empty.Get(i) {
continue
}
coords = append(coords, m.seq.Get(i))
}
return coords
}
26 changes: 26 additions & 0 deletions geom/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,29 @@ func expectBytesEq(t *testing.T, got, want []byte) {
t.Errorf("\ngot: %v\nwant: %v\n", got, want)
}
}

func expectCoordinateSliceEq(t *testing.T, got, want []Coordinates) {
t.Helper()
show := func() {
t.Logf("len(got): %d", len(got))
for i, c := range got {
t.Logf("got[%d]: %v", i, c)
}
t.Logf("len(want): %d", len(want))
for i, c := range want {
t.Logf("want[%d]: %v", i, c)
}
}

if len(want) != len(got) {
t.Errorf("length mismatch: got=%d want=%d", len(got), len(want))
show()
return
}
for i, g := range got {
w := want[i]
if g != w {
t.Errorf("mismatch at %d: got:%v want:%v", i, g, w)
}
}
}

0 comments on commit f5ea654

Please sign in to comment.