Skip to content

Commit

Permalink
Add cmprefimpl (PostGIS) tests for Dump
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Jul 18, 2021
1 parent dc52163 commit fa72bef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
33 changes: 33 additions & 0 deletions internal/cmprefimpl/cmppg/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,39 @@ func CheckForceOrientation(t *testing.T, want UnaryResult, g geom.Geometry) {
})
}

func CheckDump(t *testing.T, want UnaryResult, g geom.Geometry) {
if g.IsEmpty() {
// For empty geometries, PostGIS just returns no dumped geometries.
// Simplefeatures chooses not to do this behaviour to provide better
// consistency when it comes to Multi or GeometryCollections that
// contain only empty elements. If we were to follow the PostGIS
// behaviour, then 'MULTIPOLYGON(EMPTY)' would return 0 dumped
// geometries, whereas 'MULTIPOLYGON(((0 0,0 1,1 0,0 0)),EMPTY)' would
// return 2 dumped geometries (triangle, and empty polygon).
return
}
t.Run("CheckDump", func(t *testing.T) {
got := g.Dump()
if len(got) != len(want.Dump) {
for i, g := range got {
t.Logf("got %d: %s", i, g.AsText())
}
for i, g := range want.Dump {
t.Logf("want %d: %s", i, g.AsText())
}
t.Errorf("length mismatch, got=%d want=%d", len(got), len(want.Dump))
} else {
for i, g := range got {
if !geom.ExactEquals(g, want.Dump[i], geom.ToleranceXY(0.00001)) {
t.Logf("got: %s", g.AsText())
t.Logf("want: %s", want.Dump[i].AsText())
t.Errorf("mismatch at position %d", i)
}
}
}
})
}

func containsOnlyPolygonsOrMultiPolygons(g geom.Geometry) bool {
switch g.Type() {
case geom.TypePolygon, geom.TypeMultiPolygon:
Expand Down
1 change: 1 addition & 0 deletions internal/cmprefimpl/cmppg/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestFuzz(t *testing.T) {
CheckReverse(t, want, g)
CheckType(t, want, g)
CheckForceOrientation(t, want, g)
CheckDump(t, want, g)
})
}
}
Expand Down
16 changes: 15 additions & 1 deletion internal/cmprefimpl/cmppg/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"strings"

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

Expand Down Expand Up @@ -33,6 +34,7 @@ type UnaryResult struct {
Force4D geom.Geometry
ForceCW geom.Geometry
ForceCCW geom.Geometry
Dump []geom.Geometry
}

const (
Expand All @@ -47,6 +49,7 @@ func (p BatchPostGIS) Unary(g geom.Geometry) (UnaryResult, error) {
var boundaryWKT sql.NullString
var convexHullWKT string
var reverseWKT string
var dumpWKTs []string

var result UnaryResult
err := p.db.QueryRow(`
Expand Down Expand Up @@ -110,7 +113,9 @@ func (p BatchPostGIS) Unary(g geom.Geometry) (UnaryResult, error) {
ST_AsBinary(ST_Force4D(ST_GeomFromWKB($1))),
ST_AsBinary(ST_ForcePolygonCW(ST_GeomFromWKB($1))),
ST_AsBinary(ST_ForcePolygonCCW(ST_GeomFromWKB($1)))
ST_AsBinary(ST_ForcePolygonCCW(ST_GeomFromWKB($1))),
array_agg(ST_AsText(geom)) FROM ST_Dump(ST_GeomFromWKB($1))
`, g, isNestedGeometryCollection(g), g.AsText(),
).Scan(
Expand All @@ -135,6 +140,7 @@ func (p BatchPostGIS) Unary(g geom.Geometry) (UnaryResult, error) {
&result.Force4D,
&result.ForceCW,
&result.ForceCCW,
pq.Array(&dumpWKTs),
)
if err != nil {
return result, err
Expand All @@ -160,6 +166,14 @@ func (p BatchPostGIS) Unary(g geom.Geometry) (UnaryResult, error) {

// remove ST_ prefix that ST_GeometryType returned, since we don't want ST_ prefix in our type
result.Type = strings.TrimPrefix(result.Type, postgisTypePrefix)

for _, wkt := range dumpWKTs {
dumpGeom, err := geom.UnmarshalWKT(wkt)
if err != nil {
return result, err
}
result.Dump = append(result.Dump, dumpGeom)
}
return result, nil
}

Expand Down

0 comments on commit fa72bef

Please sign in to comment.