MarshalTWKB fails for Z or M geometries when the XY precision is negative

Open Beginner friendly
#726 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go, wasm
Domain
backend

Research direction

The defaults for the Z and M precisions are assigned in MarshalTWKB, which lives in the TWKB encoding code of the simplefeatures package. Start there, and check the validation message 'TWKB got precZ = -1, expected it to be between 0 and +7'. Reproduce with POINT Z (10 20 3) at precXY -1, then confirm the default falls to zero when precXY is negative. Done when that call succeeds and an explicit TWKBPrecisionZ(-1) is still rejected, with the doc comment updated to state the defaults.

Written by the indexing model from the issue text.

Description

bug

MarshalTWKB returns an error for a geometry with Z or M when precXY is negative, unless the caller also passes TWKBPrecisionZ or TWKBPrecisionM. This reproduces on master at a5aec8d.

g, _ := geom.UnmarshalWKT("POINT Z (10 20 3)")
_, err := geom.MarshalTWKB(g, -1)
// TWKB got precZ = -1, expected it to be between 0 and +7
b, _ := geom.MarshalTWKB(g, -1, geom.TWKBPrecisionZ(0))
// b is 110801020406

A geometry with M fails the same way on precM. Empty geometries with Z or M fail too, although no coordinates are written for them.

Cause

The Z and M precisions default to precXY. The XY precision can be between -8 and +7, but the Z and M precisions have to be between 0 and +7, so a negative precXY gives a default that fails validation.

Likely fix

Default the Z and M precisions to zero when precXY is negative. Zero is the closest valid value, and it is the default for prec_z and prec_m in PostGIS's ST_AsTWKB. The defaults are assigned before the options are applied, so an explicit TWKBPrecisionZ(-1) would still be rejected.

The doc comment on MarshalTWKB doesn't say what the Z and M precisions default to, and the fix could add that.

Interaction with #723

Before #723, UnmarshalTWKB returned XY for some empty Z/M geometries, so the result could be passed back to MarshalTWKB at a negative precision. With #723 they keep Z and M and reach this error. For example, 131801 decodes as POLYGON Z EMPTY with #723 and as POLYGON EMPTY without it, and MarshalTWKB(g, -1) fails on the first.

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.