TreeBlob
A cell set, encoded as a prefix tree. It answers where, compactly, and it answers some questions without being decoded at all.
import itacart
from itacart import serialization, geometry
from shapely.geometry import Polygon
parcel = Polygon([(-46.6329, -23.5509), (-46.6327, -23.5509),
(-46.6327, -23.5507), (-46.6329, -23.5507)])
cover = geometry.polyfill(parcel, resolution=7)
blob = serialization.encode_tree(cover)
itacart.count_cells(cover), len(blob)
(4, 17)
Why a tree and not a list
A compositional index is already a tree — shared prefixes with branches — and the encoding keeps that structure instead of flattening it. Cells under one parent share their parent’s bytes once, so a dense region costs far less than the same count of scattered cells.
This is the same saving compaction gives at the text level, made structural.
Compacting before encoding, which serialize_to_blob does by default,
compounds the two.
Reading without decoding
serialization.count_vertices(blob)
4
iter_leaves streams encoded leaf nodes in the same way, so a large set can be
walked without ever building its strings. For a single node, the structural
questions are answered on the bytes:
node = serialization.encode_node(itacart.decompose(cover)[0])
(serialization.resolution_of_binary(node),
serialization.is_ancestor_binary(
serialization.prefix_at_resolution_binary(node, 4), node))
(7, True)
Child counts are not refinement ratios
The format does not assume four or twenty-five children. It stores the count it found, because at the domain border a cell holds fewer — between two and six — and an encoder that multiplied instead of counting would produce a blob that decodes to cells which do not exist.
That is Absorbing Boundary Cells reaching into the wire format, and the specification gives it a section of its own.
One group per quadrant
The quadrant is the root of the index, and a TreeBlob holds one group for each quadrant the index names, up to all four. A cell set spread over three quadrants goes into one blob and comes back whole:
spread = "NE(0001/0001),NW(0003/0004),SE(0002/0002)"
serialization.deserialize_from_blob(serialization.serialize_to_blob(spread)) == spread
True
That is the form compose writes for any cell set across the equator or the
prime meridian, and the form polyfill writes for any region over the
equator. The groups are ordered by quadrant, so a cell set has one spelling in
bytes however it was written; the header field that counts them is in
TreeBlob binary encoding.
Validation
serialization.validate_tree(blob) is None
True
Raises on the first structural problem rather than returning a verdict. A blob that survives validation decodes to a set of cells that exist.
The wire format, the index space, the density figures and the relationship to GeometryBlob are specified in TreeBlob binary encoding.