Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump Rings #449

Merged
merged 3 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions geom/attr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1586,3 +1586,51 @@ func TestSummary(t *testing.T) {
})
}
}

func TestPolygonDumpRings(t *testing.T) {
for i, tc := range []struct {
inputWKT string
wantRingWKTs []string
}{
{
"POLYGON EMPTY",
nil,
},
{
"POLYGON ((0 0,0 1,1 0,0 0))",
[]string{"LINESTRING(0 0,0 1,1 0,0 0)"},
},
{
"POLYGON ((0 0,0 4,4 0,0 0),(1 1,1 2,2 1,1 1))",
[]string{
"LINESTRING(0 0,0 4,4 0,0 0)",
"LINESTRING(1 1,1 2,2 1,1 1)",
},
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
input := geomFromWKT(t, tc.inputWKT).MustAsPolygon()
wantRings := make([]LineString, len(tc.wantRingWKTs))
for j, wantRingWKT := range tc.wantRingWKTs {
wantRings[j] = geomFromWKT(t, wantRingWKT).MustAsLineString()
}

gotRings := input.DumpRings()

expectIntEq(t, len(gotRings), len(wantRings))
for j := range wantRings {
expectGeomEq(t,
gotRings[j].AsGeometry(),
wantRings[j].AsGeometry(),
)
}

// Ensure that we actually got copy of the slice's backing array
// rather than just a copy of its header:
otherRings := input.DumpRings()
if len(otherRings) > 0 {
expectTrue(t, &gotRings[0] != &otherRings[0])
}
})
}
}
9 changes: 9 additions & 0 deletions geom/type_polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,15 @@ func (p Polygon) DumpCoordinates() Sequence {
return seq
}

// DumpRings returns a copy of the Polygon's rings as a slice of LineStrings.
// If the Polygon is empty, then the slice will have length zero. Otherwise,
// the slice will consist of the exterior ring, followed by any interior rings.
func (p Polygon) DumpRings() []LineString {
tmp := make([]LineString, len(p.rings))
copy(tmp, p.rings)
return tmp
}

// Summary returns a text summary of the Polygon following a similar format to https://postgis.net/docs/ST_Summary.html.
func (p Polygon) Summary() string {
numPoints := p.DumpCoordinates().Length()
Expand Down