Skip to content

Treat the TWKB empty flag as a zero count in the parser #729

Backlog

Description

@peterstace

The TWKB parser has a parse* and a next* function for each geometry type. Each parse* function is called once, from nextGeometry, after the headers have been read. It returns an empty geometry when the "is empty" flag is set in the header, and otherwise calls next*, which parses the body.

The split is needed for Point, LineString and Polygon. nextPoint, nextLineString and nextPolygon also parse the members of the multi types, and those members don't have headers. For MultiPoint, MultiLineString, MultiPolygon and GeometryCollection, next* is only called from its parse*.

Suggested change

A geometry with the flag set doesn't have a body, which amounts to a count of zero. One helper can express that where the count is read:

// parseCount reads the count that starts the body of a geometry. A geometry
// flagged as empty doesn't have a body, so its count is zero.
func (p *twkbParser) parseCount() (uint64, error) {
	if p.isEmpty {
		return 0, nil
	}
	return p.parseUnsignedVarint()
}

With #723 merged, the next* functions return an empty geometry of the right coordinates type for a count of zero. If they read their counts through parseCount:

  • parseLineString, parsePolygon, parseMultiPoint, parseMultiLineString, parseMultiPolygon and parseGeometryCollection can be removed, and nextGeometry can call the next* functions directly.
  • parsePoint stays, because a Point doesn't have a count.
  • The numGeoms == 0 check in nextGeometryCollection stays, because the coordinates type of a collection without children has to come from its header.

The flag is never set while a member is being parsed, because a flagged geometry has a count of zero and its members aren't read. So nextPoint, nextLineString and nextPolygon parse members the same way as they do today.

I tried this on top of #723 at 056e055. It is 21 insertions and 54 deletions in geom/twkb_parser.go, and go test ./geom passes with the tests unchanged. Callers can't observe the change.

Relation to #725

#725 needs a check on the element count in four of the next* functions. parseCount would be one place to put that check.

Activity

  1. added
    cleanup
    on Nov 24, 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

    cleanup

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions