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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions geom/twkb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions geom/twkb_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,32 @@ 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++ {
if !w.bboxValid || other.bboxMin[d] < w.bboxMin[d] {
w.bboxMin[d] = other.bboxMin[d]
}
if !w.bboxValid || other.bboxMax[d] > w.bboxMax[d] {
w.bboxMax[d] = other.bboxMax[d]
}
}
Comment on lines +466 to +473

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch runs at most one case per dimension, so when other extends past w on both sides only the min is updated. That works in writePointArray because a single value can't be below the min and above the max at once, but a child's box can.

GEOMETRYCOLLECTION(POINT(5 5),LINESTRING(0 0,10 10))

is written with a box of (0 0)-(5 5) instead of (0 0)-(10 10). Nested collections have the same problem: GEOMETRYCOLLECTION(POINT(1 1),GEOMETRYCOLLECTION(POINT(0 0),POINT(2 2))) gets (0 0)-(1 1).

Checking the min and the max independently fixes it:

Suggested change
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]
}
}
for d := 0; d < w.dimensions; d++ {
if !w.bboxValid || other.bboxMin[d] < w.bboxMin[d] {
w.bboxMin[d] = other.bboxMin[d]
}
if !w.bboxValid || other.bboxMax[d] > w.bboxMax[d] {
w.bboxMax[d] = other.bboxMax[d]
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both bounds are now checked independently, with your suggestion. The test is now three entries at the end of TWKBTestCases with your values, and the standalone test function that caused the lint failure and the misplaced comment is removed. The two new cases fail with the old widenBBox and pass with the new one.

w.bboxValid = true
}

func (w *twkbWriter) writeTypeAndPrecision(kind twkbGeometryType) {
w.kind = kind
w.writeHeaderByte(byte(encodeZigZagInt64(int64(w.precXY))<<4) | byte(w.kind))
Expand Down
Loading