Compaction & Normalization

Two different tidyings, often confused. Compaction changes which cells a string names — coarser ones, covering the same ground. Normalization changes only how the same cells are written.

import itacart
from itacart import index as ix

parent = "NE(0500/0100(1))"
siblings = ix.compose(list(itacart.get_children(parent, flatten=True)))
ix.count_cells(siblings)
25

Twenty-five siblings that exhaust their parent. Compaction replaces them with it:

itacart.compact_cells(siblings)
'NE(0500/0100(1))'

One cell where there were twenty-five, covering identical ground. The replacement happens only when the sibling set is exhaustive — a partial set stays as it is, because folding it would claim ground that was not in the input.

Compaction recurses: a parent produced this way can itself join a complete set one level up. Expanding is the inverse, to a uniform resolution:

sum(1 for _ in itacart.uncompact_cells(parent, target_res=3))
25

Why compaction is worth doing

A parcel covered at a fine resolution can run to a great many cells, and most of its interior is solid. Compaction leaves the fine cells along the boundary and folds the interior into as few coarse cells as it can, which is usually an order-of-magnitude reduction in what has to be stored, hashed or transmitted. Parcel Representation covers the trade-off in cadastral terms.

The saving is safe because compaction folds by the children a parent actually has, not by the string. Away from the border those are the codes of the next alphabet spelled under it, and the fold is the familiar one. A cell whose outer side has been carried onto the domain border also has children spelled under the next column’s prefix, and compaction counts them:

border = "NE(2003/0000)"
children = list(itacart.get_children(border, flatten=True))
own = [cell for cell in children if cell.startswith("NE(2003/0000(")]
len(children), itacart.compact_cells(ix.compose(children)), itacart.compact_cells(ix.compose(own))
(5, 'NE(2003/0000)', 'NE(2003/0000(1,2,3,4))')

Five children, one of them spelled under NE(2004/0000), a prefix that names no cell. The five fold into their parent. The four under the parent’s own spelling cover 80.05 per cent of it, so they do not fold: that would claim the rest. Normalization, which rewrites spelling only, leaves both as written:

ix.normalize(ix.compose(own)) == ix.compose(own)
True

What compaction can do at the domain border is inherit the approximation of the absorbing side by a chord — Lexical Ancestry vs Physical Refinement is where that is stated.

Normalization writes the same cells one way

ix.normalize("NE(0001/0002(1(A2,A1,A1)))")
'NE(0001/0002(1(A1,A2)))'

Duplicates collapse, siblings are ordered, and the result is canonical: two strings naming the same set normalize to the same text.

The rewrites normalization performs

docs/_static/f2/f2_07_normalisation_rewrites.png

That property is what makes an index comparable and hashable. Two records of the same parcel, written by different hands in different orders, agree only if there is a canonical form to agree in — which is the whole basis of using an index as a key. Canonical Geometry carries the idea through to the geometry itself.

Which one you want

Use normalization before comparing, hashing or storing an index. Use compaction when you want fewer cells for the same ground. They compose: compact first, then normalize the result, and you have the smallest canonical spelling of a region.