Skip to main content

Module sort_key

Module sort_key 

Source
Expand description

Sort key encoding for external sort — (tile_id, layer, sort_key).

The sort key packs tile identity, layer ordinal, and feature ordering into a single u64 for efficient comparison during external merge sort.

§Layout (MSB → LSB)

[4 bits zoom][30 bits tile_id][5 bits layer][25 bits feature_sort]
  • zoom (4 bits): 0–15 (max_zoom=15 is the project default)
  • tile_id (30 bits): Row-major y * (1 << z) + x. At z15, max value is 32768 * 32768 - 1 ≈ 1.07B, which fits in 30 bits (max 1.07B). The previous 29-bit layout silently overflowed at z15.
  • layer (5 bits): 0–31 (supports up to 31 layers for configurable schemas)
  • feature_sort (25 bits): Biased signed value (±16M range)

This creates a total ordering: tiles grouped together → layers within a tile → features within a layer.

Functions§

decode_layer
Decode the layer ordinal from a sort key.
decode_tile_id
Decode the zoom level and tile ID from a sort key.
encode
Encode a tile coordinate + layer + feature sort key into a single u64.
tile_id_to_xy
Convert a linear tile ID back to (x, y) at a given zoom level.