Repository navigation
Description
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,parseMultiPolygonandparseGeometryCollectioncan be removed, andnextGeometrycan call thenext*functions directly.parsePointstays, because a Point doesn't have a count.- The
numGeoms == 0check innextGeometryCollectionstays, 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