From cbd3f3871e2fc45f7cf6db3e0ecaa10bbe1903ab Mon Sep 17 00:00:00 2001 From: Shounak Joshi Date: Thu, 12 Nov 2026 12:10:22 +0530 Subject: [PATCH 1/3] Fix the bounding box of a GeometryCollection in the TWKB output The sub-geometries of a collection are written by their own writers, so their coordinates never reached the bounding box of the collection, and a collection was written with an all-zero box. --- CHANGELOG.md | 6 ++++++ geom/twkb_test.go | 17 +++++++++++++++++ geom/twkb_write.go | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60dcbff4..5172f8fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## Unreleased +- Fix the bounding box of a `GeometryCollection` in the TWKB output. The + sub-geometries of a collection are written by their own writers, so their + coordinates never reached the bounding box of the collection, and a + collection was written with an all-zero box. A consumer that filters a + TWKB stream by its bounding boxes never found a collection. + - Fix quadratic time and memory (in the nesting depth) when building nested `GeometryCollection`s, including when parsing them from WKB, WKT, GeoJSON and TWKB. diff --git a/geom/twkb_test.go b/geom/twkb_test.go index 8ecc3cf7..11a26c06 100644 --- a/geom/twkb_test.go +++ b/geom/twkb_test.go @@ -469,6 +469,23 @@ func TestTWKBUnmarshalMarshalValid(t *testing.T) { // in particular some will always close polygon rings by duplicating // the first point as the final point. So some test cases listed // in this file may not agree with all PostGIS implementations. + +func TestTWKBBBoxOfGeometryCollection(t *testing.T) { + // The bounding box of a collection has to cover its sub-geometries. + g := test.FromWKT(t, "GEOMETRYCOLLECTION(POINT(-1 -2))") + twkb, err := geom.MarshalTWKB(g, 0, geom.TWKBBoundingBoxHeader()) + test.NoErr(t, err) + + env, ok, err := geom.UnmarshalTWKBEnvelope(twkb) + test.NoErr(t, err) + test.True(t, ok) + + min, max, ok := env.XYEnvelope.MinMaxXYs() + test.True(t, ok) + test.Eq(t, min, geom.XY{X: -1, Y: -2}) + test.Eq(t, max, geom.XY{X: -1, Y: -2}) +} + func TestWriteTWKBSQLFile(t *testing.T) { t.Skip("this test was only used during initial development") sql := "" diff --git a/geom/twkb_write.go b/geom/twkb_write.go index e27a4aea..042671ed 100644 --- a/geom/twkb_write.go +++ b/geom/twkb_write.go @@ -448,12 +448,35 @@ func (w *twkbWriter) writeGeometryCollection(gc GeometryCollection) error { if err := subWriter.writeGeometry(g); err != nil { return err } + w.widenBBox(subWriter) subTWKB := subWriter.formTWKB() w.twkbContents = append(w.twkbContents, subTWKB...) } return nil } +// widenBBox widens the bounding box of the writer with the bounding box of +// the given writer. The sub-geometries of a collection are written by their +// own writers, so their coordinates do not reach the box of the collection +// otherwise. +func (w *twkbWriter) widenBBox(other *twkbWriter) { + if !other.bboxValid { + return + } + for d := 0; d < w.dimensions; d++ { + switch { + case !w.bboxValid: + w.bboxMin[d] = other.bboxMin[d] + w.bboxMax[d] = other.bboxMax[d] + case other.bboxMin[d] < w.bboxMin[d]: + w.bboxMin[d] = other.bboxMin[d] + case other.bboxMax[d] > w.bboxMax[d]: + w.bboxMax[d] = other.bboxMax[d] + } + } + w.bboxValid = true +} + func (w *twkbWriter) writeTypeAndPrecision(kind twkbGeometryType) { w.kind = kind w.writeHeaderByte(byte(encodeZigZagInt64(int64(w.precXY))<<4) | byte(w.kind)) From 724a91f3514c2a56d1155f15f1b4131d0b6fd91e Mon Sep 17 00:00:00 2001 From: Shounak Joshi <25joshiss_1@rbunagpur.in> Date: Fri, 13 Nov 2026 10:57:56 +0530 Subject: [PATCH 2/3] geom: avoid shadowing the predeclared min and max in the TWKB test The predeclared linter rejects the min and max variables of the TWKB bounding box test, as they shadow the builtins of the same name. They are renamed to gotMin and gotMax, which is what the other tests of the package use. --- geom/twkb_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/geom/twkb_test.go b/geom/twkb_test.go index 11a26c06..dda88850 100644 --- a/geom/twkb_test.go +++ b/geom/twkb_test.go @@ -480,10 +480,10 @@ func TestTWKBBBoxOfGeometryCollection(t *testing.T) { test.NoErr(t, err) test.True(t, ok) - min, max, ok := env.XYEnvelope.MinMaxXYs() + gotMin, gotMax, ok := env.XYEnvelope.MinMaxXYs() test.True(t, ok) - test.Eq(t, min, geom.XY{X: -1, Y: -2}) - test.Eq(t, max, geom.XY{X: -1, Y: -2}) + test.Eq(t, gotMin, geom.XY{X: -1, Y: -2}) + test.Eq(t, gotMax, geom.XY{X: -1, Y: -2}) } func TestWriteTWKBSQLFile(t *testing.T) { From c133be51f04b83f20cfc3a50ec3f0feccfc83295 Mon Sep 17 00:00:00 2001 From: Shounak Joshi <25joshiss_1@rbunagpur.in> Date: Fri, 13 Nov 2026 11:04:28 +0530 Subject: [PATCH 3/3] geom: fix the bbox of a geometry collection with more than one child The switch in widenBBox ran at most one case per dimension, so a child that extended past the collection on both sides only updated the min. The min and the max are now checked independently. The test moves into the TWKBTestCases table, which checks the exact bytes on encode and the envelope on decode. It covers a collection that is widened on both sides by a later child and a nested collection. The two of them fail without the change. --- geom/twkb_test.go | 38 +++++++++++++++++++++----------------- geom/twkb_write.go | 9 +++------ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/geom/twkb_test.go b/geom/twkb_test.go index dda88850..5c8e37f5 100644 --- a/geom/twkb_test.go +++ b/geom/twkb_test.go @@ -342,6 +342,27 @@ var TWKBTestCases = []struct { hasIDList: true, listedIDs: []int64{0, 1}, }, + { + description: "geometry collection with bbox", + twkbHex: "0701010003000101000103", + wkt: "GEOMETRYCOLLECTION(POINT(-1 -2))", + hasBBox: true, + listedBBox: [2]string{"POINT(-1 -2)", "POINT(-1 -2)"}, + }, + { + description: "geometry collection with bbox widened on both sides by a later child", + twkbHex: "0701001400140201000a0a02000200001414", + wkt: "GEOMETRYCOLLECTION(POINT(5 5),LINESTRING(0 0,10 10))", + hasBBox: true, + listedBBox: [2]string{"POINT(0 0)", "POINT(10 10)"}, + }, + { + description: "nested geometry collection with bbox", + twkbHex: "07010004000402010002020700020100000001000404", + wkt: "GEOMETRYCOLLECTION(POINT(1 1),GEOMETRYCOLLECTION(POINT(0 0),POINT(2 2)))", + hasBBox: true, + listedBBox: [2]string{"POINT(0 0)", "POINT(2 2)"}, + }, } func TestTWKBUnmarshalMarshalValid(t *testing.T) { @@ -469,23 +490,6 @@ func TestTWKBUnmarshalMarshalValid(t *testing.T) { // in particular some will always close polygon rings by duplicating // the first point as the final point. So some test cases listed // in this file may not agree with all PostGIS implementations. - -func TestTWKBBBoxOfGeometryCollection(t *testing.T) { - // The bounding box of a collection has to cover its sub-geometries. - g := test.FromWKT(t, "GEOMETRYCOLLECTION(POINT(-1 -2))") - twkb, err := geom.MarshalTWKB(g, 0, geom.TWKBBoundingBoxHeader()) - test.NoErr(t, err) - - env, ok, err := geom.UnmarshalTWKBEnvelope(twkb) - test.NoErr(t, err) - test.True(t, ok) - - gotMin, gotMax, ok := env.XYEnvelope.MinMaxXYs() - test.True(t, ok) - test.Eq(t, gotMin, geom.XY{X: -1, Y: -2}) - test.Eq(t, gotMax, geom.XY{X: -1, Y: -2}) -} - func TestWriteTWKBSQLFile(t *testing.T) { t.Skip("this test was only used during initial development") sql := "" diff --git a/geom/twkb_write.go b/geom/twkb_write.go index 042671ed..cdf4e4f8 100644 --- a/geom/twkb_write.go +++ b/geom/twkb_write.go @@ -464,13 +464,10 @@ func (w *twkbWriter) widenBBox(other *twkbWriter) { return } for d := 0; d < w.dimensions; d++ { - switch { - case !w.bboxValid: + if !w.bboxValid || other.bboxMin[d] < w.bboxMin[d] { w.bboxMin[d] = other.bboxMin[d] - w.bboxMax[d] = other.bboxMax[d] - case other.bboxMin[d] < w.bboxMin[d]: - w.bboxMin[d] = other.bboxMin[d] - case other.bboxMax[d] > w.bboxMax[d]: + } + if !w.bboxValid || other.bboxMax[d] > w.bboxMax[d] { w.bboxMax[d] = other.bboxMax[d] } }