Repository navigation
Keep the Z and M of empty geometries in UnmarshalTWKB - #723
Merged
Merged
Conversation
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.
peterstace
approved these changes
Nov 20, 2026
This was referenced Nov 23, 2026
peterstace
reviewed
Nov 23, 2026
peterstace
requested changes
Nov 23, 2026
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.
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.
peterstace
approved these changes
Nov 24, 2026
peterstace
left a comment
Owner
There was a problem hiding this comment.
Thanks for the contribution!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
UnmarshalTWKBreads the extended dimensions byte for empty geometries, and the Point and LineString parsers keep it, but an empty Polygon, MultiPoint, MultiLineString, MultiPolygon or GeometryCollection came back as XY. A collection holding one of them was then narrowed to XY, which dropped the Z and M of its other members.PostGIS writes that byte for empty Z/M geometries. Decoding
ST_AsTWKB(..., 0, 3, 0)output from PostGIS 3.5.2:The empty branches now use
ForceCoordinatesType(p.ctype), as the WKB parser does, and so does a count of zero (the only empty form of a polygon inside a MultiPolygon). A non-empty collection keeps its children's types. XY empties are unchanged.The new
TWKBTestCasesrows fail without the change and pass with it (all but the zero-count MultiPolygon row useskipEncode, sinceMarshalTWKBwrites no extended byte for empty geometries, see #724); removing any one of the checks fails its rows.make unit(golang:1.19) andmake lint(golangci-lint v1.61.0) pass. I didn't run the geos, pgscan or cmp jobs.This only changes the parser. The writer still drops the dimensions of empty geometries, as the comment in
writeIsEmptyHeadersays it does, so a Z collection with an empty member does not round trip throughMarshalTWKB; that is separate from #721.Author Motivation
I found this by reading the TWKB parser with an AI assistant, not as a user of simplefeatures: the Point and LineString branches kept the dimensions of an empty geometry and the others didn't. I then checked the bytes against PostGIS's
ST_AsTWKB.Written with AI assistance (Claude); I have reviewed the change.