3. Prediction (S2)¶
The idea¶
A vertex is rarely surprising. It lies close to the vertex drawn before it, and map geometry has shape:
buildings have right angles, roads keep their direction. Instead of storing a vertex, the encoder stores
how far it is from a guess, the residual p[i] - guess(i). The guess may only use vertices the decoder
has already decoded, so the decoder can make the same guess and add the residual back. The better the
guess, the smaller and more repetitive the residuals, and the fewer bytes the compressor needs.
Different vertices deserve different guesses. Every predictor therefore gives each vertex a class, computed from the topology alone: the first vertex of a ring, an ordinary vertex, the fourth corner of a rectangle. The class picks the guess, and later decides which stream the residual goes to (segmentation). Because both sides compute the classes, no label is ever written.
A vertex without a geometric guess is predicted by the previous vertex of the buffer, even across feature boundaries. The chain of differences never restarts, so there are no absolute seed values in the payload.
The guesses¶
Delta¶
The guess is the previous vertex: residual = p[i] - p[i-1]. One class. This is what stock MLT does, and it
remains the best choice where there is no shape to exploit, as in point layers (poi, housenumber).
Quad corner¶
Most buildings are rectangles, and a rectangle is fully determined by three of its corners. In a run of
exactly four vertices (a closed ring without its repeated closing vertex), the fourth corner is guessed to
complete the parallelogram of the first three: guess = p0 + p2 - p1. For an exact rectangle the
residual is (0, 0).
Classes: START (first vertex of a run), INTERIOR (delta) and CORNER (the guessed fourth vertex).
vertices p0=(100,200) p1=(130,200) p2=(130,240) p3=(100,240)
class START INTERIOR INTERIOR CORNER
guess previous p0 p1 p0 + p2 - p1 = (100,240)
residual p0 - previous (30,0) (0,40) (0,0)
Line continuation¶
A road tends to continue in the direction it was going, with a similar step length. From the third vertex
of a line part on, the guess extends the last edge: guess = p[i-1] + (p[i-1] - p[i-2]). The residual is
then the change of the edge: how much the road bent and how much the step grew or shrank.
the first four vertices of a line part of six or more
vertices p0=(0,0) p1=(10,2) p2=(21,4) p3=(31,7)
guess previous p0 2·p1 - p0=(20,4) 2·p2 - p1=(32,6)
residual p0 (10,2) (1,0) (-1,1)
Only line parts of at least six vertices are continued. On shorter parts and on polygon rings the
continuation guess measured worse than plain delta. Classes: START, INTERIOR (delta: the second vertex
of a part, short parts, rings and points) and LINE_INTERIOR (continued).
From guesses to predictors¶
A predictor is one of the guesses above, optionally combined with two further ideas, each with a page of its own:
- frames rotate the residual of a ring edge into the direction of the wall, so one of its two components is near zero;
- shared references replace a vertex that an earlier ring or line already holds by a pointer to it.
Seven combinations are live. The code is the value in the payload header.
| code | predictor | guess | frame | shared references |
|---|---|---|---|---|
| 0 | DELTA |
previous vertex | ||
| 1 | QUAD_CORNER |
quad corner | ||
| 23 | QUAD_CORNER_PREVFRAME_FIRST_ONLY |
quad corner | ring frame | |
| 26 | QUAD_CORNER_SHARED_RAW |
quad corner | ring references | |
| 27 | QUAD_CORNER_SHARED_RAW_PREVFRAME_FIRST_ONLY |
quad corner | ring frame | ring references |
| 2 | LINE_CONTINUATION |
line continuation | ||
| 28 | LINE_CONTINUATION_SHARED_RAW |
line continuation | line references |
The missing codes belonged to retired predictors (for example a turn-angle predictor, a corner guess on lines, earlier ring frames and the line frames, and codes 7, 25 and 12, the ring and line references with the earlier index, see shared references). Their codes are not reused, so a payload never changes meaning.
Decoding¶
The decoder derives the classes from the topology, then walks the vertices in order: it takes the next residual from the vertex's stream, undoes the frame rotation if the vertex is framed, and adds the guess, computed from the vertices it has already decoded.
What it is worth¶
Measured when the stage was added (Hilbert order, split varint, geometry buffers, gzip, against plain
delta + varint): DELTA -3,97%, QUAD_CORNER -4,79%, LINE_CONTINUATION -3,83%. Over the whole corpus
the corner guess wins only narrowly, because the best guess depends on the layer: corners for buildings,
continuation for roads, delta for points. That is the reason the predictor is chosen per layer
(compositions).
On their own layers the gains are larger, and grow with the ideas added on top:
| layer | predictor | gzip against plain delta + varint |
|---|---|---|
building |
QUAD_CORNER |
-9,20% |
building |
QUAD_CORNER_PREVFRAME_FIRST_ONLY |
-12,38% |
transportation |
LINE_CONTINUATION |
-5,23% |
In the code (research-rust/mlt-pipeline/src/predict.rs) every guess classifies the vertices from the
topology, says which classes have a guess, and computes the guess from the decoded prefix.
Further reading: quad corner, line continuation; guesses that did not pay, such as delta of delta, right-angle turns and turn scalar.