Internals · Binary Specification

TXG — TEXSPACE GeoArchive Specification (v2)

Status: Stable v2 (TXGR\x02 / TXGS\x02) + Sidecar Archives (TXGZ\x01 / TXGL\x01 / TXGO\x01) Goal: Cloud-native, immutable, HTTP-Range-queryable binary geocoding, timezone, spatial-join, and private-overlay archives that serve planet-scale forward geocoding, address verification, autocomplete, reverse geocoding, point-in-polygon spatial joins, and relational nearby search directly from static object storage (S3, Cloudflare R2, GCS, or local disk) with zero backend servers.


1. Design Principles#

  1. Minimize HTTP round-trips (2–3 RTTs cold, 1–2 RTTs warm): Every lookup index uses a single-level in-memory fence-key directory embedded in the file's Root Block (bytes 0..root_end, typically 32–128 KB). A client fetches the Root Block once per archive, caches it in memory/IndexedDB, and resolves every subsequent key directly to a single compressed leaf block (16–32 KB) with zero intermediate B-tree hops.
  2. Hilbert-clustered entity & address layout:
    • All entities inside a shard are assigned dense sequential IDs (hilbert_id: u32) ordered by S2CellId(lat, lon) at level 30. Nearby streets, POIs, and addresses share the same 32 KB block, enabling:
    • Single-range reverse geocoding (s2_cell -> 1–2 ENTS blocks + ADDR block).
    • Single-range local fuzzy/typo rescue (fetch the S2 neighborhood's ENTS block and run Levenshtein/Metaphone in RAM instead of fetching global trigram lists over HTTP).
    • High-ratio delta-coordinate compression (~4–6 bytes/rooftop compressed).
  3. Deterministic, immutable byte ranges for CDN caching: All leaf blocks have fixed byte offsets computed at build time. Every client querying "san francisco" or "london" requests the exact same Range: bytes=start-end, enabling high edge-cache hit ratios on Cloudflare / CloudFront / Fastly.
  4. Sans-I/O execution model: Query planning and decoding are pure Rust (no C/SQLite dependencies, wasm32-unknown-unknown compatible). Callers supply any async or sync byte-range transport (fetch() in browsers/Workers, reqwest/HTTP in CLI, pread/mmap on disk).

2. Primitives & Framing#

  • Endianness: Little-endian for all fixed-width integers (u16, u32, u64, i32).
  • Varints:
    • uvarint: unsigned LEB128 (u64).
    • svarint: ZigZag-encoded signed LEB128 (i64, (v << 1) ^ (v >> 63)).
  • Strings: uvarint(byte_len) followed by UTF-8 bytes.
  • Coordinates: Quantized WGS-84 degrees lat_e7 = round(lat * 1e7) as i32, lon_e7 = round(lon * 1e7) as i32 (~1.1 cm precision).
  • Compression (compression: u8):
    • 0: Uncompressed raw bytes.
    • 1: Raw DEFLATE (RFC 1951).
    • 2: Zstandard (RFC 8878).
  • Front-coded sorted strings: Within a sorted leaf block, each key is stored as: uvarint(shared_prefix_bytes) | uvarint(suffix_bytes) | suffix_utf8 relative to the immediately preceding key (shared_prefix_bytes = 0 for the first entry).
  • Block reference: (offset: u64, comp_len: u32, raw_len: u32) — absolute byte range offset .. offset + comp_len inside the archive.

3. Archive Type 1: Global Router (router.txg)#

Maps normalized attribute strings across up to 128 shards and embeds global admin places for zero-shard city/country autocomplete.

3.1 Fixed 64-Byte Header (bytes 0..64)#

OffsetSizeFieldDescription
08magicASCII TXGR + version \x02\x00\x00\x00
81compressionBlock compression codec (0, 1, or 2)
91flagsBitmask (reserved, 0)
102shard_countNumber of shards in catalog (<= 128)
124reserved0
168root_offsetByte offset of Root Block (64)
248root_comp_lenCompressed byte length of Root Block
328root_raw_lenUncompressed byte length of Root Block
408total_attrsTotal normalized routing attributes across all blocks
488total_adminsTotal global admin entries across all blocks
568created_unixBuild timestamp (Unix seconds UTC)

3.2 Router Root Block (compressed)#

  1. Shard Catalog (uvarint(shard_count) entries):
    • shard_id: String (e.g. "us_ca")
    • rel_path: String (e.g. "shards/us_ca.txg")
    • min_lat_e7: i32, min_lon_e7: i32, max_lat_e7: i32, max_lon_e7: i32
    • entity_count: uvarint, address_count: uvarint
    • shard_root_end: uvarint (exact byte length of shard header + root so clients fetch it in 1 Range read)
  2. Attribute-Shard Fence Directory (uvarint(n_blocks) entries):
    • Front-coded first_key: String (lexicographically smallest attr_norm in block)
    • offset_delta: uvarint (delta from previous block's offset + comp_len, base = root_offset + root_comp_len)
    • comp_len: uvarint, raw_len: uvarint
  3. Admin-Place Fence Directory (uvarint(n_admin_blocks) entries):
    • Same front-coded fence format for global admin/landmark blocks.

3.3 Attribute-Shard Leaf Blocks (~24 KB compressed)#

  • uvarint(entry_count)
  • Sorted front-coded entries:
    • shared_prefix: uvarint, suffix: String
    • mask_tag: u8:
    • 1..=8: sparse list of mask_tag shard indices (u8 each)
    • 0xFF: dense 16-byte little-endian u128 shard bitmask

3.4 Admin-Place Leaf Blocks (~20 KB compressed)#

  • uvarint(entry_count)
  • Sorted front-coded entries by name_norm:
    • shared_prefix: uvarint, suffix: String (normalized key)
    • display_name: String (empty means reuse name_norm)
    • layer: u8, pop_q8: u8, shard_idx: u8
    • lat_e7: i32, lon_e7: i32

4. Archive Type 2: Spatial Shard (<shard>.txg)#

Self-contained archive for one geographic shard (usable standalone or routed via router.txg).

4.1 Fixed 64-Byte Header (bytes 0..64)#

OffsetSizeFieldDescription
08magicASCII TXGS + version \x02\x00\x00\x00
81compressionBlock compression codec (0, 1, or 2)
91flagsBitmask (reserved, 0)
102reserved_u160
124entity_countTotal Hilbert-ordered entities in shard
168root_offsetByte offset of Root Block (64)
248root_comp_lenCompressed byte length of Root Block
328root_raw_lenUncompressed byte length of Root Block
408address_countTotal rooftop/parcel addresses in shard
488attr_countTotal normalized attributes in shard
568created_unixBuild timestamp (Unix seconds UTC)

4.2 Shard Root Block (compressed)#

  1. Shard Metadata: shard_id: String, min_lat_e7..max_lon_e7: 4×i32, macro_admin_ref: (offset, comp_len, raw_len).
  2. ATTR Fence Directory: front-coded first_attr_norm + block (offset, comp_len, raw_len).
  3. POST Spill Block Directory: (first_s2_cell: u64, last_s2_cell: u64, offset, comp_len, raw_len) indexed by u32 block ID.
  4. ENTS Hilbert Fence Directory: (first_hilbert_id: u32, first_s2_cell: u64, last_s2_cell: u64, offset, comp_len, raw_len).
  5. ADDR Fence Directory: (first_street_hilbert_id: u32, first_hn_key: u64, offset, comp_len, raw_len).
  6. ADMIN Local Fence Directory: (first_s2_cell: u64, last_s2_cell: u64, offset, comp_len, raw_len).

4.3 Section 1 — ATTR Leaf Blocks (~16 KB compressed)#

Sorted by attr_norm. Each entry:

  • Front-coded attr_norm
  • idf_q8: u8 (round(idf * 10.0).clamp(0, 255))
  • entity_count: uvarint
  • footprint_cell_count: uvarint, followed by sorted S2 CellUnion cell IDs encoded as svarint deltas (first raw u64, subsequent deltas)
  • posting_mode: u8:
    • 0 (inline, entity_count <= 24): uvarint(pair_count) + sorted (s2_cell_delta, hilbert_id) pairs
    • 1 (spilled into POST): uvarint(first_post_block_idx) + uvarint(post_block_count)

4.4 Section 2 — POST Spill Blocks (~16 KB compressed)#

For high-frequency attributes (entity_count > 24), postings are sorted by (s2_cell_id, hilbert_id) and split into 16 KB blocks. The Root Block's POST directory records (first_s2_cell, last_s2_cell) per chunk so clients skip chunks outside the query's S2 focus.

4.5 Section 3 — ENTS Blocks (~32 KB compressed)#

Entities sorted by (s2_cell_l30(lat, lon), layer, original_entity_id). Block b starts at first_hilbert_id. Each entry:

  • s2_cell_delta: uvarint (delta from previous entity's s2_cell_l30)
  • layer: u8, source: u8, pop_q8: u8, flags: u8
    • flags & 0x01 (ENT_FLAG_HAS_ADDRS): has rooftop/parcel addresses in ADDR
    • flags & 0x02 (ENT_FLAG_HAS_POLYLINE): has compact street polyline (for interpolation & reverse snapping)
    • flags & 0x04 (ENT_FLAG_SAME_PARENTS): inherits the immediately preceding entity's [country, region, county, locality] ADMIN_MACRO indices in 0 bytes (~98% hit rate along the Hilbert curve)
  • lat_e7_delta: svarint, lon_e7_delta: svarint
  • name: String
  • If flags & 0x02: uvarint(vertex_count) + delta-encoded (dlat_e7, dlon_e7) polyline vertices (max 32 simplified vertices).
  • If flags & 0x04 == 0: 4 × uvarint (idx + 1, with 0 encoding NO_ADMIN) giving precomputed full-resolution [country, region, county, locality] indices into ADMIN_MACRO.

4.6 Section 4 — ADDR Blocks (~16 KB compressed)#

Sorted by (street_hilbert_id, hn_sort_key) where hn_sort_key = (numeric_prefix as u64) << 16 | suffix_hash16. Each block begins with a mini postcode dictionary (uvarint(n_pcs) + strings) followed by uvarint(addr_count) entries:

  • street_hilbert_id_delta: uvarint
  • housenumber: String
  • dlat_e7: svarint, dlon_e7: svarint
  • postcode_idx: uvarint (0 = empty, 1..=n_pcs = dictionary index)
  • addr_flags: u8:
    • addr_flags & 0x01 (ADDR_FLAG_IS_PARCEL): cadastral parcel, followed by apn: String
    • addr_flags & 0x02 (ADDR_FLAG_CUSTOM_LOCALITY): cross-border arterial override, followed by uvarint(locality_macro_idx + 1) when a rooftop belongs to a different full-resolution ADMIN_MACRO locality than its parent street centroid (e.g. Mission St crossing San Francisco / Daly City)

4.7 Section 5 — ADMIN & ADMIN_MACRO Blocks (~24–32 KB compressed)#

  • ADMIN_MACRO (1 block per shard, cached once): all Country, Region, County, and Locality entities in the shard with simplified exterior polygon rings (delta-e7 vertices). Referenced in O(1) at runtime via precomputed full-resolution parent_admins / locality_override indices computed once at pack time before polygon simplification.
  • ADMIN Local Blocks (sorted by s2_cell_l30): Macrohood, Neighbourhood, and PostalCode entities with simplified exterior polygon rings (or point-only centroids) for fine-grained reverse geocoding and display label enrichment.

5. Archive Type 3: Timezone Archive (tz.txg, magic TXGZ)#

Packs all 444 IANA timezone polygon boundaries plus RFC 8536 TZif v2/v3 DST rule blobs into a single static HTTP-Range file (~4.6 MB).

5.1 Fixed 32-Byte Header (bytes 0..32)#

OffsetSizeFieldDescription
08magicASCII TXGZ + version \x01\x00\x00\x00
81compressionBlock compression codec (1 = Deflate)
93reserved0
128root_offsetByte offset of Root Block (32)
204root_comp_lenCompressed byte length of Root Block (~22 KB)
244root_raw_lenUncompressed byte length of Root Block
284reserved0

5.2 Root & Data Blocks#

  • Initial Probe (64 KiB): Fetches the header and complete Root Block in 1 RTT (zones: Vec<(tzid, tzif_block)> + polys: Vec<(zone_idx, bbox_e7, poly_block)>).
  • Polygon Blocks: Delta-E7 exterior ring + hole rings compressed with Deflate; only 1–2 bbox-overlapping polygons and their tiny TZif rule blobs are fetched in Round 2.
  • Ocean fallback: Coordinates matching zero polygons resolve in 0 extra RTTs to nautical Etc/GMT±N.

6. Archive Type 4: Polygon Spatial-Join Layer (*.txg, magic TXGL)#

"PMTiles for Point-in-Polygon" — packs any GeoJSON FeatureCollection or .geojsonl of Polygon / MultiPolygon features with arbitrary JSON properties (texspace pack-layer) into a single immutable HTTP-Range file.

6.1 Fixed 32-Byte Header (bytes 0..32)#

OffsetSizeFieldDescription
08magicASCII TXGL + version \x01\x00\x00\x00
81compressionBlock compression codec (1 = Deflate)
93reserved0
124feature_countTotal polygon/multipolygon features
164root_comp_lenCompressed byte length of Root Block (at offset 32)
204root_raw_lenUncompressed byte length of Root Block
248created_unixBuild timestamp (Unix seconds UTC)

6.2 Root Directory & Bundled Feature Blocks#

  • Root Block (bytes 32 .. 32 + root_comp_len, written via two-pass exact reservation):
    • layer_name: String, description: String, uvarint(feature_count)
    • Entries sorted smallest bbox area first (nested enclaves match ahead of enclosing zones):
    • id: String | 4×i32 (min_lat_e7..max_lon_e7) | BlockRef(offset, comp_len, raw_len)
  • Feature Block (1 Range read fetches BOTH properties and geometry):
    • properties_json: String (arbitrary JSON object preserved verbatim)
    • uvarint(polygon_count) → for each polygon: delta-E7 exterior_ring + uvarint(hole_count) + delta-E7 hole_rings
  • Single-RTT small archives: Initial probe is 64 KiB (LAYER_INITIAL_PROBE_BYTES); any TXGL archive <= 64 KiB serves header, root, and all feature blocks from a single HTTP 206 read.

7. Archive Type 5: Private Overlay Archive (*.txg, magic TXGO)#

Packs private CSV, TSV, or GeoJSON site catalogs (stores, warehouses, EV chargers, hubs; texspace pack-overlay) into a single S2 Hilbert-clustered + term-indexed static HTTP-Range archive that fuses seamlessly into search, autocomplete, reverse, and nearby.

7.1 Fixed 32-Byte Header (bytes 0..32)#

OffsetSizeFieldDescription
08magicASCII TXGO + version \x01\x00\x00\x00
81compressionBlock compression codec (1 = Deflate)
93reserved0
124site_countTotal Hilbert-ordered private sites
164root_comp_lenCompressed byte length of Root Block (at offset 32)
204root_raw_lenUncompressed byte length of Root Block
248created_unixBuild timestamp (Unix seconds UTC)

7.2 Root Directory, Site Blocks & Term Blocks#

  • Root Block: name: String, description: String, bbox_e7: 4×i32, site_count: uvarint,
    • site_fences: Vec<(first_site_idx, last_site_idx, first_s2, last_s2, BlockRef)> (128 S2-sorted sites/block — enables both O(1) ID lookup and S2 cap nearby scans)
    • term_fences: Vec<StrFence> (256 alphabetically sorted terms/block — supports exact, prefix, and bounded Damerau-Levenshtein fuzzy lookup in RAM)
  • Site Blocks: Delta site_idx, s2_cell_l30: u64, lat_e7: i32, lon_e7: i32, id, name, category, address, properties_json.
  • Term Blocks: Sorted (term: String, delta_encoded site_indices: Vec<u32>).

8. Query Execution & Wire Budget#

OperationTypical RTTs (Cold / Warm)Typical Wire Transfer
Boot (router.txg header + 262 KB root via 384 KiB probe)1 / 0 (cached)~384 KB once (cached across sessions)
Global Forward / Verify (440-query Third-Party Global Holdout)6–7 / 4 p50255 KB p50 warm session (12 ranges)
Global Reverse (37.72047, -122.43765)3 / 1–2~64–110 KB cold / ~32–64 KB warm
Focused Autocomplete ("4865 Miss")2–4 / 1~32–80 KB
Timezone (tz.txg) or Spatial Join (TXGL)1–2 / 0–1~64 KB probe (+ 1–2 feature blocks if > 64 KB)
Parallel enrich() (Reverse + TZ + Spatial + 2 TXGL layers)4 / 1–2Coalesced across all archives in parallel Promise.all rounds
Private Overlay (TXGO <= 64 KB) / In-Memory mountGeoJSON1 / 0<= 64 KB once (or 0 B in RAM)