Skip to content

MarshalTWKB writes incorrect output for empty geometries #721

Open

Description

@peterstace

MarshalTWKB has two bugs involving empty geometries. Both reproduce on master at ce2170a.

Stray size and bounding box bytes after an empty geometry

An empty geometry is written with only the "is empty" bit set in its metadata header, so the header doesn't declare a size or a bounding box. writeAdditionalHeaders still runs afterwards and appends them when TWKBSizeHeader() or TWKBBoundingBoxHeader() is used.

Geometry Option Output Expected
POINT EMPTY size 011000 0110
POINT EMPTY bbox 011000000000 0110
GEOMETRYCOLLECTION EMPTY bbox 071000000000 0710

At the top level the extra bytes trail the geometry and UnmarshalTWKB ignores them. Inside a collection they sit in front of the next child and break parsing:

g, _ := geom.UnmarshalWKT("GEOMETRYCOLLECTION(POINT EMPTY,POINT(1 1))")
b, _ := geom.MarshalTWKB(g, 0, geom.TWKBSizeHeader())
// b is 070209020110000102020202. The empty point is written as 01 10 00.
_, err := geom.UnmarshalTWKB(b)
// TWKB parsing error at byte 12: unsupported geometry type 0

A likely fix is for writeAdditionalHeaders to return early when w.isEmpty is set.

Empty points in a MultiPoint are written as (0 0)

writeMultiPoint writes the coordinates of every point without checking whether the point is empty, so an empty point comes out as zeros:

g, _ := geom.UnmarshalWKT("MULTIPOINT(EMPTY,5 5)")
b, _ := geom.MarshalTWKB(g, 0)
// b is 04000200000a0a
back, _ := geom.UnmarshalTWKB(b)
// back is MULTIPOINT((0 0),(5 5))

With TWKBBoundingBoxHeader() the bounding box is (0 0)-(5 5) instead of (5 5)-(5 5). Once #720 is merged, that box also widens the bounding box of an enclosing GeometryCollection.

TWKB stores a MultiPoint as a count followed by coordinates, so it doesn't have a way to mark one of the points as empty. The options are to skip empty points or to return an error. Skipping changes the point count, which matters when TWKBIDList is used, because the ID list has to match the number of points.

Activity

  1. added
    bug
    on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions