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

Open
#729 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
72/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
data

Research direction

Start by reading geom/twkb_parser.go, in particular nextGeometry and the parse*/next* pairs for each geometry type. Confirm the changes from #723 are present at the base commit before editing, then add parseCount and route the count reads in the next* functions through it, deleting the now-redundant parse* wrappers except parsePoint and the numGeoms == 0 guard in nextGeometryCollection. Run go test ./geom with the existing tests unchanged; done means the suite passes and callers see no behavioural change.

Written by the indexing model from the issue text.

Description

cleanup

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.

Dominant language
Go
Stars
176
Forks
27
Avg merge
1d 20h
Merged PRs (30d)
6

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from peterstace/simplefeatures

All issues in peterstace/simplefeatures

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.