Skip to content

Commit

Permalink
Implement DumpCoordinates for GeometryCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Jul 31, 2021
1 parent 5d42f9c commit 5d581a5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
49 changes: 44 additions & 5 deletions geom/dump_coordinates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,45 @@ func TestDumpCoordinatesMultiPolygon(t *testing.T) {
}
}

func TestDumpCoordinatesGeometryCollection(t *testing.T) {
for _, tc := range []struct {
description string
inputWKT string
want Sequence
}{
{
description: "empty",
inputWKT: "GEOMETRYCOLLECTION EMPTY",
want: NewSequence(nil, DimXY),
},
{
description: "empty z",
inputWKT: "GEOMETRYCOLLECTION Z EMPTY",
want: NewSequence(nil, DimXYZ),
},
{
description: "single point",
inputWKT: "GEOMETRYCOLLECTION(POINT(1 2))",
want: NewSequence([]float64{1, 2}, DimXY),
},
{
description: "single point z",
inputWKT: "GEOMETRYCOLLECTION Z(POINT Z(1 2 0))",
want: NewSequence([]float64{1, 2, 0}, DimXYZ),
},
{
description: "nested",
inputWKT: "GEOMETRYCOLLECTION Z(GEOMETRYCOLLECTION Z(POINT Z(1 2 0)))",
want: NewSequence([]float64{1, 2, 0}, DimXYZ),
},
} {
t.Run(tc.description, func(t *testing.T) {
got := geomFromWKT(t, tc.inputWKT).AsGeometryCollection().DumpCoordinates()
expectSequenceEq(t, got, tc.want)
})
}
}

func TestDumpCoordinatesGeometry(t *testing.T) {
for _, tc := range []struct {
description string
Expand Down Expand Up @@ -317,11 +356,11 @@ func TestDumpCoordinatesGeometry(t *testing.T) {
inputWKT: "MULTIPOLYGON Z(((0 0 1,0 1 1,1 0 1,0 0 1)))",
want: NewSequence([]float64{0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1}, DimXYZ),
},
//{
// description: "GeometryCollection",
// inputWKT: "GEOMETRYCOLLECTION Z(POINT Z(0 1 2))",
// want: NewSequence([]float64{0, 1, 2}, DimXYZ),
//},
{
description: "GeometryCollection",
inputWKT: "GEOMETRYCOLLECTION Z(POINT Z(0 1 2))",
want: NewSequence([]float64{0, 1, 2}, DimXYZ),
},
} {
t.Run(tc.description, func(t *testing.T) {
got := geomFromWKT(t, tc.inputWKT).DumpCoordinates()
Expand Down
4 changes: 2 additions & 2 deletions geom/type_geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ func (g Geometry) appendDump(gs []Geometry) []Geometry {
func (g Geometry) DumpCoordinates() Sequence {
switch g.gtype {
case TypeGeometryCollection:
panic("GeometryCollection not yet handled")
return g.AsGeometryCollection().DumpCoordinates()
case TypePoint:
return g.AsPoint().DumpCoordinates()
case TypeLineString:
Expand All @@ -787,6 +787,6 @@ func (g Geometry) DumpCoordinates() Sequence {
case TypeMultiPolygon:
return g.AsMultiPolygon().DumpCoordinates()
default:
panic("invalid")
panic("unknown type: " + g.Type().String())
}
}
10 changes: 10 additions & 0 deletions geom/type_geometry_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,13 @@ func (c GeometryCollection) Dump() []Geometry {
}
return gs
}

// DumpCoordinates returns a Sequence holding all control points in the
// GeometryCollection.
func (c GeometryCollection) DumpCoordinates() Sequence {
var coords []float64
for _, g := range c.geoms {
coords = g.DumpCoordinates().appendAllPoints(coords)
}
return NewSequence(coords, c.ctype)
}

0 comments on commit 5d581a5

Please sign in to comment.