itacart.integer_index

The integer index: one 64-bit integer per cell, and back.

A compositional index is a string, and a string is the wrong key for a database column, a sort, a join or a hash table of a hundred million cells. This module gives every cell an integer, in the manner of the NUNIQ scheme of the IVOA’s Multi-Order Coverage maps for HEALPix: one number that carries the resolution, sorts in the grid’s canonical order within a resolution, and reaches its parent by arithmetic.

The integer of a cell at resolution k is OFFSET[k] + position. The position counts the box of every address the grammar can write at that resolution, in canonical order: the quadrant first, in the order NE, NW, SE, SW; then the resolution-1 pair, column before row as the siblings of itacart.compose() are ordered, over 2 005 columns and 1 001 rows; then one refinement code per level, by its place in the level’s alphabet, four at an even level and twenty-five at an odd one:

position(quadrant)  = q
position(level 1)   = (q * 2005 + column) * 1001 + row
position(level k)   = position(level k-1) * r_k + code_k

and OFFSET[k] is the size of every box above resolution k, so the fourteen resolutions occupy fourteen consecutive ranges, the four quadrants of resolution 0 first. The largest integer is 8.4335e18, below 2**63 = 9.2234e18, so every integer fits a signed 64-bit column: the type databases and columnar formats offer everywhere, where the unsigned one is not.

The box is wider than the grid. It holds the western zero column, which no quadrant west of the meridian has; the columns a row lacks past the border; column 2004, which is only ever a prefix; and the codes the polar row collapses away. Those positions have integers and name no cell, so the existence contract runs both ways here: a spelling that names no cell gets no integer, and an integer that names no cell gets no spelling. Both refusals are NonExistentCellError.

Ancestry is lexical, as it is for strings. int_parent() divides the position, which is itacart.get_parent() in arithmetic: the parent of NE(2004/0000(3)) is the integer of NE(2004/0000), a prefix that names no cell, although the cell’s physical parent is NE(2003/0000). The physical relation is itacart.get_children() and lives with the strings.

The names str_to_int() and int_to_str() are the ones H3 gives the same pair of conversions, kept as aliases for the same reason the other H3 names are.

Module constants

BOX_COLUMNS

0 to 2004, the last one a prefix only.

BOX_ROWS

0 to 1000, the last one the polar caps.

RATIOS

Positions under one position of the level above, from resolution 0 down.

BOX_SIZES

Positions in the box of each resolution, 0 to 13.

OFFSETS

First integer of each resolution, and one past the last of resolution 13.

INTEGER_LIMIT

One past the largest integer.

Functions

cell_to_int

The integer of a cell.

int_parent

The lexical parent of an integer, by division of its position.

int_resolution

The resolution an integer carries, 0 to 13.

int_to_cell

The cell an integer names.

int_to_str

The cell an integer names.

str_to_int

The integer of a cell.