diff --git a/CHANGELOG.md b/CHANGELOG.md index 80a78a45..f4dd7992 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +- Fix `UnmarshalTWKB` dropping the Z and M of an empty `Polygon`, + `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 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..f981ef26 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() } @@ -482,12 +482,12 @@ 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) { if p.isEmpty { - return NewMultiPoint(nil), nil + return MultiPoint{}.ForceCoordinatesType(p.ctype), nil } return p.nextMultiPoint() } @@ -510,12 +510,12 @@ 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) { if p.isEmpty { - return NewMultiLineString(nil), nil + return MultiLineString{}.ForceCoordinatesType(p.ctype), nil } return p.nextMultiLineString() } @@ -538,12 +538,12 @@ 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) { if p.isEmpty { - return NewMultiPolygon(nil), nil + return MultiPolygon{}.ForceCoordinatesType(p.ctype), nil } return p.nextMultiPolygon() } @@ -566,12 +566,12 @@ 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) { if p.isEmpty { - return NewGeometryCollection(nil), nil + return GeometryCollection{}.ForceCoordinatesType(p.ctype), nil } return p.nextGeometryCollection() } @@ -586,6 +586,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 5c8e37f5..6e39caa8 100644 --- a/geom/twkb_test.go +++ b/geom/twkb_test.go @@ -153,12 +153,24 @@ 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", wkt: "POLYGON EMPTY", skipEncode: true, }, + { + description: "polygon z no rings", + twkbHex: "03080100", + wkt: "POLYGON Z EMPTY", + skipEncode: true, + }, { description: "polygon unclosed rings", twkbHex: "030002040000060000060500040203000202000001", @@ -192,12 +204,24 @@ 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", wkt: "MULTIPOINT EMPTY", skipEncode: true, }, + { + description: "multipoint z no contents", + twkbHex: "04080100", + wkt: "MULTIPOINT Z EMPTY", + skipEncode: true, + }, { description: "multipoint with bbox", twkbHex: "04010408060803040604040404", @@ -263,12 +287,24 @@ 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", wkt: "MULTILINESTRING EMPTY", skipEncode: true, }, + { + description: "multilinestring z no contents", + twkbHex: "05080100", + wkt: "MULTILINESTRING Z EMPTY", + skipEncode: true, + }, { description: "multilinestring", twkbHex: "050002020000020203020202020202", @@ -279,12 +315,24 @@ 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", 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", @@ -302,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", @@ -318,12 +371,42 @@ 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", 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",