Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

- Fix quadratic time and memory (in the nesting depth) when building nested
`GeometryCollection`s, including when parsing them from WKB, WKT, GeoJSON
and TWKB.

- **Breaking change:** The minimum required Go version is now 1.19 (previously
1.18).

Expand Down
19 changes: 19 additions & 0 deletions geom/perf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ func BenchmarkUnmarshalWKB(b *testing.B) {
})
}
})
b.Run("nested_collection", func(b *testing.B) {
xys := make([]float64, 2*1000)
for i := range xys {
xys[i] = float64(i)
}
leaf := geom.NewMultiPointXY(xys...).AsGeometry()
for _, depth := range []int{1, 10, 100} {
b.Run(fmt.Sprintf("depth=%d", depth), func(b *testing.B) {
wkb := nestInCollections(leaf, depth).AsBinary()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := geom.UnmarshalWKB(wkb, geom.NoValidate{})
if err != nil {
b.Fatal(err)
}
}
})
}
})
}

func BenchmarkIntersectsLineStringWithLineString(b *testing.B) {
Expand Down
7 changes: 6 additions & 1 deletion geom/type_geometry_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func NewGeometryCollection(geoms []Geometry) GeometryCollection {
}
geoms = append([]Geometry(nil), geoms...)
for i := range geoms {
geoms[i] = geoms[i].ForceCoordinatesType(ctype)
if geoms[i].CoordinatesType() != ctype {
geoms[i] = geoms[i].ForceCoordinatesType(ctype)
}
}
return GeometryCollection{geoms, ctype}
}
Expand Down Expand Up @@ -422,6 +424,9 @@ func (c GeometryCollection) CoordinatesType() CoordinatesType {
// ForceCoordinatesType returns a new [GeometryCollection] with a different [CoordinatesType]. If
// a dimension is added, then new values are populated with 0.
func (c GeometryCollection) ForceCoordinatesType(newCType CoordinatesType) GeometryCollection {
if c.ctype == newCType {
return c
}
gs := make([]Geometry, len(c.geoms))
for i := range c.geoms {
gs[i] = c.geoms[i].ForceCoordinatesType(newCType)
Expand Down
42 changes: 42 additions & 0 deletions geom/type_geometry_collection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package geom_test

import (
"testing"

"github.com/peterstace/simplefeatures/geom"
"github.com/peterstace/simplefeatures/internal/test"
)

func nestInCollections(g geom.Geometry, depth int) geom.Geometry {
for i := 0; i < depth; i++ {
g = geom.NewGeometryCollection([]geom.Geometry{g}).AsGeometry()
}
return g
}

var collectionSink geom.GeometryCollection

func TestNewGeometryCollectionDoesNotCopyNestedMembers(t *testing.T) {
leaf := test.FromWKT(t, "MULTIPOINT(1 2,3 4,5 6)")
wrap := func(g geom.Geometry) func() {
return func() { collectionSink = geom.NewGeometryCollection([]geom.Geometry{g}) }
}
shallow := testing.AllocsPerRun(10, wrap(nestInCollections(leaf, 1)))
deep := testing.AllocsPerRun(10, wrap(nestInCollections(leaf, 100)))
test.Eq(t, deep, shallow)
}

func TestGeometryCollectionForceCoordinatesTypeUnchanged(t *testing.T) {
gc := nestInCollections(test.FromWKT(t, "MULTIPOINT(1 2,3 4,5 6)"), 100).MustAsGeometryCollection()
allocs := testing.AllocsPerRun(10, func() { collectionSink = gc.ForceCoordinatesType(geom.DimXY) })
test.Eq(t, allocs, 0.0)
test.DeepEqual(t, gc.ForceCoordinatesType(geom.DimXY), gc)
}

func TestNewGeometryCollectionForcesNestedMembers(t *testing.T) {
nestedZ := nestInCollections(test.FromWKT(t, "POINT Z(1 2 3)"), 3)
gc := geom.NewGeometryCollection([]geom.Geometry{nestedZ, test.FromWKT(t, "POINT(4 5)")})
test.ExactEqualsWKT(t, gc.AsGeometry(),
"GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(POINT(1 2)))),POINT(4 5))")
test.Eq(t, gc.GeometryN(0).CoordinatesType(), geom.DimXY)
}
Loading