MarshalTWKB writes incorrect output for empty geometries

Open
#721 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
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go

Research direction

The bug is in the TWKB encoder: start with writeAdditionalHeaders and writeMultiPoint in the TWKB marshalling code, and check how w.isEmpty is set. Reproduce with MarshalTWKB on POINT EMPTY and MULTIPOINT(EMPTY,5 5) and round-trip through UnmarshalTWKB. Done means empty geometries emit no size or bounding box bytes, and the MultiPoint empty-point case is resolved (skip or error) with tests covering the ID-list interaction.

Written by the indexing model from the issue text.

Description

bug

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.

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

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.