From d392183a0b7ebacae10db41cdaf21c3a41e6c11a Mon Sep 17 00:00:00 2001 From: youdie006 Date: Fri, 20 Nov 2026 09:35:33 +0900 Subject: [PATCH 1/3] Keep the Z and M of empty geometries in UnmarshalTWKB The extended dimensions byte is read for empty geometries too, and the Point and LineString parsers keep it, but the other types returned an XY empty geometry. A collection holding one was then narrowed to XY, so its other members lost their Z and M. --- CHANGELOG.md | 4 ++++ geom/twkb_parser.go | 10 +++++----- geom/twkb_test.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80a78a45..e40eafc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- Fix `UnmarshalTWKB` dropping the Z and M of an empty `Polygon`, + `MultiPoint`, `MultiLineString`, `MultiPolygon` or `GeometryCollection`. A + collection containing one of them lost the Z and M of its other members too. + - 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 diff --git a/geom/twkb_parser.go b/geom/twkb_parser.go index 34da1568..8e433620 100644 --- a/geom/twkb_parser.go +++ b/geom/twkb_parser.go @@ -437,7 +437,7 @@ func (p *twkbParser) nextLineString() (LineString, error) { func (p *twkbParser) parsePolygon() (Polygon, error) { if p.isEmpty { - return NewPolygon(nil), nil + return Polygon{}.ForceCoordinatesType(p.ctype), nil } return p.nextPolygon() } @@ -487,7 +487,7 @@ func (p *twkbParser) nextPolygon() (Polygon, error) { func (p *twkbParser) parseMultiPoint() (MultiPoint, error) { if p.isEmpty { - return NewMultiPoint(nil), nil + return MultiPoint{}.ForceCoordinatesType(p.ctype), nil } return p.nextMultiPoint() } @@ -515,7 +515,7 @@ func (p *twkbParser) nextMultiPoint() (MultiPoint, error) { func (p *twkbParser) parseMultiLineString() (MultiLineString, error) { if p.isEmpty { - return NewMultiLineString(nil), nil + return MultiLineString{}.ForceCoordinatesType(p.ctype), nil } return p.nextMultiLineString() } @@ -543,7 +543,7 @@ func (p *twkbParser) nextMultiLineString() (MultiLineString, error) { func (p *twkbParser) parseMultiPolygon() (MultiPolygon, error) { if p.isEmpty { - return NewMultiPolygon(nil), nil + return MultiPolygon{}.ForceCoordinatesType(p.ctype), nil } return p.nextMultiPolygon() } @@ -571,7 +571,7 @@ func (p *twkbParser) nextMultiPolygon() (MultiPolygon, error) { func (p *twkbParser) parseGeometryCollection() (GeometryCollection, error) { if p.isEmpty { - return NewGeometryCollection(nil), nil + return GeometryCollection{}.ForceCoordinatesType(p.ctype), nil } return p.nextGeometryCollection() } diff --git a/geom/twkb_test.go b/geom/twkb_test.go index 5c8e37f5..f7b05911 100644 --- a/geom/twkb_test.go +++ b/geom/twkb_test.go @@ -153,6 +153,12 @@ var TWKBTestCases = []struct { twkbHex: "0310", wkt: "POLYGON EMPTY", }, + { + description: "polygon z lacking data", + twkbHex: "031801", + wkt: "POLYGON Z EMPTY", + skipEncode: true, + }, { description: "polygon no rings", twkbHex: "030000", @@ -192,6 +198,12 @@ var TWKBTestCases = []struct { twkbHex: "0410", wkt: "MULTIPOINT EMPTY", }, + { + description: "multipoint m lacking data", + twkbHex: "041802", + wkt: "MULTIPOINT M EMPTY", + skipEncode: true, + }, { description: "multipoint no contents", twkbHex: "040000", @@ -263,6 +275,12 @@ var TWKBTestCases = []struct { twkbHex: "0510", wkt: "MULTILINESTRING EMPTY", }, + { + description: "multilinestring zm lacking data", + twkbHex: "051803", + wkt: "MULTILINESTRING ZM EMPTY", + skipEncode: true, + }, { description: "multilinestring no contents", twkbHex: "050000", @@ -279,6 +297,12 @@ var TWKBTestCases = []struct { twkbHex: "0610", wkt: "MULTIPOLYGON EMPTY", }, + { + description: "multipolygon z lacking data", + twkbHex: "061801", + wkt: "MULTIPOLYGON Z EMPTY", + skipEncode: true, + }, { description: "multipolygon no contents", twkbHex: "060000", @@ -318,6 +342,18 @@ var TWKBTestCases = []struct { twkbHex: "0710", wkt: "GEOMETRYCOLLECTION EMPTY", }, + { + description: "geometry collection z lacking data", + twkbHex: "071801", + wkt: "GEOMETRYCOLLECTION Z EMPTY", + skipEncode: true, + }, + { + description: "geometry collection z with polygon z lacking data", + twkbHex: "07080102031801010801020406", + wkt: "GEOMETRYCOLLECTION Z (POLYGON Z EMPTY,POINT Z (1 2 3))", + skipEncode: true, + }, { description: "geometry collection no contents", twkbHex: "070000", From aa35959fd6038bc997bc2eb67837450618f2c12e Mon Sep 17 00:00:00 2001 From: youdie006 Date: Mon, 23 Nov 2026 15:03:22 +0900 Subject: [PATCH 2/3] Keep the Z and M of zero-count geometries in UnmarshalTWKB A count of zero also encodes an empty Polygon, MultiPoint, MultiLineString, MultiPolygon or GeometryCollection, and a Polygon inside a MultiPolygon has no header, so a ring count of zero is its only empty form. The collection only takes the parent's coordinates type when it is empty, since each child has its own header. --- CHANGELOG.md | 5 +++-- geom/twkb_parser.go | 15 +++++++++++++++ geom/twkb_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e40eafc0..f4dd7992 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,9 @@ ## Unreleased - Fix `UnmarshalTWKB` dropping the Z and M of an empty `Polygon`, - `MultiPoint`, `MultiLineString`, `MultiPolygon` or `GeometryCollection`. A - collection containing one of them lost the Z and M of its other members too. + `MultiPoint`, `MultiLineString`, `MultiPolygon` or `GeometryCollection`, + whether written with the empty flag or with a count of zero. A collection + containing one of them lost the Z and M of its other members too. - 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 diff --git a/geom/twkb_parser.go b/geom/twkb_parser.go index 8e433620..1c4fe344 100644 --- a/geom/twkb_parser.go +++ b/geom/twkb_parser.go @@ -447,6 +447,9 @@ func (p *twkbParser) nextPolygon() (Polygon, error) { if err != nil { return Polygon{}, fmt.Errorf("num rings varint malformed: %w", err) } + if numRings == 0 { + return Polygon{}.ForceCoordinatesType(p.ctype), nil + } var rings []LineString for r := uint64(0); r < numRings; r++ { @@ -502,6 +505,9 @@ func (p *twkbParser) nextMultiPoint() (MultiPoint, error) { return MultiPoint{}, err } } + if numPoints == 0 { + return MultiPoint{}.ForceCoordinatesType(p.ctype), nil + } var pts []Point for i := 0; i < int(numPoints); i++ { pt, err := p.nextPoint() @@ -530,6 +536,9 @@ func (p *twkbParser) nextMultiLineString() (MultiLineString, error) { return MultiLineString{}, err } } + if numLineStrings == 0 { + return MultiLineString{}.ForceCoordinatesType(p.ctype), nil + } var lines []LineString for i := 0; i < int(numLineStrings); i++ { ls, err := p.nextLineString() @@ -558,6 +567,9 @@ func (p *twkbParser) nextMultiPolygon() (MultiPolygon, error) { return MultiPolygon{}, err } } + if numPolygons == 0 { + return MultiPolygon{}.ForceCoordinatesType(p.ctype), nil + } var polys []Polygon for i := 0; i < int(numPolygons); i++ { poly, err := p.nextPolygon() @@ -586,6 +598,9 @@ func (p *twkbParser) nextGeometryCollection() (GeometryCollection, error) { return GeometryCollection{}, err } } + if numGeoms == 0 { + return GeometryCollection{}.ForceCoordinatesType(p.ctype), nil + } var geoms []Geometry for i := 0; i < int(numGeoms); i++ { subParser := newTWKBParser(p.twkb[p.pos:]) diff --git a/geom/twkb_test.go b/geom/twkb_test.go index f7b05911..6e39caa8 100644 --- a/geom/twkb_test.go +++ b/geom/twkb_test.go @@ -165,6 +165,12 @@ var TWKBTestCases = []struct { wkt: "POLYGON EMPTY", skipEncode: true, }, + { + description: "polygon z no rings", + twkbHex: "03080100", + wkt: "POLYGON Z EMPTY", + skipEncode: true, + }, { description: "polygon unclosed rings", twkbHex: "030002040000060000060500040203000202000001", @@ -210,6 +216,12 @@ var TWKBTestCases = []struct { wkt: "MULTIPOINT EMPTY", skipEncode: true, }, + { + description: "multipoint z no contents", + twkbHex: "04080100", + wkt: "MULTIPOINT Z EMPTY", + skipEncode: true, + }, { description: "multipoint with bbox", twkbHex: "04010408060803040604040404", @@ -287,6 +299,12 @@ var TWKBTestCases = []struct { wkt: "MULTILINESTRING EMPTY", skipEncode: true, }, + { + description: "multilinestring z no contents", + twkbHex: "05080100", + wkt: "MULTILINESTRING Z EMPTY", + skipEncode: true, + }, { description: "multilinestring", twkbHex: "050002020000020203020202020202", @@ -309,6 +327,12 @@ var TWKBTestCases = []struct { wkt: "MULTIPOLYGON EMPTY", skipEncode: true, }, + { + description: "multipolygon z no contents", + twkbHex: "06080100", + wkt: "MULTIPOLYGON Z EMPTY", + skipEncode: true, + }, { description: "multipolygon with polygon lacking data", twkbHex: "06000100", @@ -326,6 +350,11 @@ var TWKBTestCases = []struct { twkbHex: "0600020001040000060000060500", wkt: "MULTIPOLYGON(EMPTY,((0 0,3 0,3 3,0 3,0 0)))", }, + { + description: "multipolygon z unclosed rings with various contents", + twkbHex: "06080102000104000002060000000600050000", + wkt: "MULTIPOLYGON Z (EMPTY,((0 0 1,3 0 1,3 3 1,0 3 1,0 0 1)))", + }, { description: "multipolygon unclosed rings", twkbHex: "0600020104000006000006050001040802000202000001", @@ -360,6 +389,24 @@ var TWKBTestCases = []struct { wkt: "GEOMETRYCOLLECTION EMPTY", skipEncode: true, }, + { + description: "geometry collection z no contents", + twkbHex: "07080100", + wkt: "GEOMETRYCOLLECTION Z EMPTY", + skipEncode: true, + }, + { + description: "geometry collection z with multipoint z no contents", + twkbHex: "0708010204080100010801020406", + wkt: "GEOMETRYCOLLECTION Z (MULTIPOINT Z EMPTY,POINT Z (1 2 3))", + skipEncode: true, + }, + { + description: "geometry collection without extended dimensions holding a point z", + twkbHex: "070001010801020406", + wkt: "GEOMETRYCOLLECTION Z (POINT Z (1 2 3))", + skipEncode: true, + }, { description: "geometry collection with point and empty", twkbHex: "070002010000020310", From 056e055626e322abcd24eaff28a50239395ffef0 Mon Sep 17 00:00:00 2001 From: youdie006 Date: Tue, 24 Nov 2026 10:22:15 +0900 Subject: [PATCH 3/3] Build parsed TWKB geometries with the parser's coordinates type The members of a Polygon, MultiPoint, MultiLineString or MultiPolygon are all parsed with p.ctype, so build the result with it instead of inferring the type from the members, which falls back to XY when there are none. This replaces the zero-count checks for those four types. --- geom/twkb_parser.go | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/geom/twkb_parser.go b/geom/twkb_parser.go index 1c4fe344..f981ef26 100644 --- a/geom/twkb_parser.go +++ b/geom/twkb_parser.go @@ -447,9 +447,6 @@ func (p *twkbParser) nextPolygon() (Polygon, error) { if err != nil { return Polygon{}, fmt.Errorf("num rings varint malformed: %w", err) } - if numRings == 0 { - return Polygon{}.ForceCoordinatesType(p.ctype), nil - } var rings []LineString for r := uint64(0); r < numRings; r++ { @@ -485,7 +482,7 @@ func (p *twkbParser) nextPolygon() (Polygon, error) { ls := NewLineString(NewSequence(coords, p.ctype)) rings = append(rings, ls) } - return NewPolygon(rings), nil + return Polygon{rings, p.ctype}, nil } func (p *twkbParser) parseMultiPoint() (MultiPoint, error) { @@ -505,9 +502,6 @@ func (p *twkbParser) nextMultiPoint() (MultiPoint, error) { return MultiPoint{}, err } } - if numPoints == 0 { - return MultiPoint{}.ForceCoordinatesType(p.ctype), nil - } var pts []Point for i := 0; i < int(numPoints); i++ { pt, err := p.nextPoint() @@ -516,7 +510,7 @@ func (p *twkbParser) nextMultiPoint() (MultiPoint, error) { } pts = append(pts, pt) } - return NewMultiPoint(pts), nil + return MultiPoint{pts, p.ctype}, nil } func (p *twkbParser) parseMultiLineString() (MultiLineString, error) { @@ -536,9 +530,6 @@ func (p *twkbParser) nextMultiLineString() (MultiLineString, error) { return MultiLineString{}, err } } - if numLineStrings == 0 { - return MultiLineString{}.ForceCoordinatesType(p.ctype), nil - } var lines []LineString for i := 0; i < int(numLineStrings); i++ { ls, err := p.nextLineString() @@ -547,7 +538,7 @@ func (p *twkbParser) nextMultiLineString() (MultiLineString, error) { } lines = append(lines, ls) } - return NewMultiLineString(lines), nil + return MultiLineString{lines, p.ctype}, nil } func (p *twkbParser) parseMultiPolygon() (MultiPolygon, error) { @@ -567,9 +558,6 @@ func (p *twkbParser) nextMultiPolygon() (MultiPolygon, error) { return MultiPolygon{}, err } } - if numPolygons == 0 { - return MultiPolygon{}.ForceCoordinatesType(p.ctype), nil - } var polys []Polygon for i := 0; i < int(numPolygons); i++ { poly, err := p.nextPolygon() @@ -578,7 +566,7 @@ func (p *twkbParser) nextMultiPolygon() (MultiPolygon, error) { } polys = append(polys, poly) } - return NewMultiPolygon(polys), nil + return MultiPolygon{polys, p.ctype}, nil } func (p *twkbParser) parseGeometryCollection() (GeometryCollection, error) {