Skip to content

Keep the Z and M of empty geometries in UnmarshalTWKB - #723

Merged
peterstace merged 3 commits into
peterstace:masterfrom
youdie006:twkb-empty-dims
Nov 24, 2026
Merged

peterstace merged 3 commits into
peterstace:masterfrom
youdie006:twkb-empty-dims

Conversation

@youdie006

@youdie006 youdie006 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

UnmarshalTWKB reads 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:

                     before                                         after
03180d               POLYGON EMPTY                                  POLYGON Z EMPTY
07180d               GEOMETRYCOLLECTION EMPTY                       GEOMETRYCOLLECTION Z EMPTY
07080d0203180d...    GEOMETRYCOLLECTION(POLYGON EMPTY,POINT(1 2))   GEOMETRYCOLLECTION Z (POLYGON Z EMPTY,POINT Z (1 2 3))
07080f0204180f...    GEOMETRYCOLLECTION(MULTIPOINT EMPTY,           GEOMETRYCOLLECTION ZM (MULTIPOINT ZM EMPTY,
                       LINESTRING(1 2,5 6))                           LINESTRING ZM (1 2 3 4,5 6 7 8))

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 TWKBTestCases rows fail without the change and pass with it (all but the zero-count MultiPolygon row use skipEncode, since MarshalTWKB writes no extended byte for empty geometries, see #724); removing any one of the checks fails its rows. make unit (golang:1.19) and make 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 writeIsEmptyHeader says it does, so a Z collection with an empty member does not round trip through MarshalTWKB; 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.

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 peterstace left a comment •

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.

Thanks!

EDIT: Actually, there are a few issues I can see after further examination, I'll post below.

peterstace

This comment was marked as duplicate.

peterstace

This comment was marked as duplicate.

Comment thread geom/twkb_parser.go Resolved
Comment thread geom/twkb_test.go Resolved
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 peterstace left a comment

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.

Thanks for the contribution!

@peterstace
peterstace merged commit 1a32737 into peterstace:master Nov 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants