Skip to content

Commit

Permalink
Implement DumpCoordinates for Point
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Jul 30, 2021
1 parent dcd51a7 commit c819eb1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
54 changes: 54 additions & 0 deletions geom/dump_coordinates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,60 @@ import (
. "github.com/peterstace/simplefeatures/geom"
)

func TestDumpCoordinatesPoint(t *testing.T) {
for _, tc := range []struct {
description string
inputWKT string
want Sequence
}{
{
description: "empty",
inputWKT: "POINT EMPTY",
want: NewSequence(nil, DimXY),
},
{
description: "empty z",
inputWKT: "POINT Z EMPTY",
want: NewSequence(nil, DimXYZ),
},
{
description: "empty m",
inputWKT: "POINT M EMPTY",
want: NewSequence(nil, DimXYM),
},
{
description: "empty zm",
inputWKT: "POINT ZM EMPTY",
want: NewSequence(nil, DimXYZM),
},
{
description: "non-empty",
inputWKT: "POINT(1 2)",
want: NewSequence([]float64{1, 2}, DimXY),
},
{
description: "non-empty z",
inputWKT: "POINT Z(1 2 3)",
want: NewSequence([]float64{1, 2, 3}, DimXYZ),
},
{
description: "non-empty m",
inputWKT: "POINT M(1 2 3)",
want: NewSequence([]float64{1, 2, 3}, DimXYM),
},
{
description: "non-empty zm",
inputWKT: "POINT ZM(1 2 3 4)",
want: NewSequence([]float64{1, 2, 3, 4}, DimXYZM),
},
} {
t.Run(tc.description, func(t *testing.T) {
got := geomFromWKT(t, tc.inputWKT).AsPoint().DumpCoordinates()
expectSequenceEq(t, got, tc.want)
})
}
}

func TestDumpCoordinatesMultiPoint(t *testing.T) {
for _, tc := range []struct {
description string
Expand Down
16 changes: 16 additions & 0 deletions geom/type_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,19 @@ func (p Point) asXYs() []XY {
}
return nil
}

// DumpCoordinates returns a Sequence representing the point. For an empty
// Point, the Sequence will be empty. For a non-empty Point, the Sequence will
// contain the single set of coordinates representing the point.
func (p Point) DumpCoordinates() Sequence {
ctype := p.CoordinatesType()
var floats []float64
coords, ok := p.Coordinates()
if ok {
n := ctype.Dimension()
floats = coords.appendFloat64s(make([]float64, 0, n))
}
seq := NewSequence(floats, ctype)
seq.assertNoUnusedCapacity()
return seq
}
11 changes: 8 additions & 3 deletions geom/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,22 @@ func expectBytesEq(t *testing.T, got, want []byte) {
func expectSequenceEq(t *testing.T, got, want Sequence) {
t.Helper()
show := func() {
t.Logf("len(got): %d", got.Length())
t.Logf("len(got): %d, ct(got): %s", got.Length(), got.CoordinatesType())
for i := 0; i < got.Length(); i++ {
t.Logf("got[%d]: %v", i, got.Get(i))
}
t.Logf("len(want): %d", want.Length())
t.Logf("len(want): %d, ct(want): %s", want.Length(), want.CoordinatesType())
for i := 0; i < want.Length(); i++ {
t.Logf("want[%d]: %v", i, want.Get(i))
}
}
if got.CoordinatesType() != want.CoordinatesType() {
t.Errorf("mismatched coordinate type")
show()
return
}
if got.Length() != want.Length() {
t.Errorf("length mismatch: got=%d want=%d", got.Length(), want.Length())
t.Errorf("length mismatch")
show()
return
}
Expand Down

0 comments on commit c819eb1

Please sign in to comment.