From c819eb12889d5d31c39a29cae1ee6e309a415462 Mon Sep 17 00:00:00 2001 From: Peter Stace Date: Sat, 31 Jul 2021 06:06:46 +1000 Subject: [PATCH] Implement DumpCoordinates for Point --- geom/dump_coordinates_test.go | 54 +++++++++++++++++++++++++++++++++++ geom/type_point.go | 16 +++++++++++ geom/util_test.go | 11 +++++-- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/geom/dump_coordinates_test.go b/geom/dump_coordinates_test.go index f05538f7..204af2b5 100644 --- a/geom/dump_coordinates_test.go +++ b/geom/dump_coordinates_test.go @@ -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 diff --git a/geom/type_point.go b/geom/type_point.go index c64afa08..bb7d4f62 100644 --- a/geom/type_point.go +++ b/geom/type_point.go @@ -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 +} diff --git a/geom/util_test.go b/geom/util_test.go index 4674af94..ae35003f 100644 --- a/geom/util_test.go +++ b/geom/util_test.go @@ -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 }