Skip to content

zorder

Package zorder provides z-order curve helpers for mapping multi-dimensional integer coordinates to a single ordered scalar (Morton encoding). Useful as the secondary key for a spatial-ish index built on a 1-D B-tree.

  • zorder(d1, d2, …, dN) → INTEGER: interleaves the low bits of each dimension into a single int64. 2 to 24 dimensions; each dimension must fit in floor(63/N) bits, otherwise the function returns an error.
  • unzorder(z, N, i) → INTEGER: extracts dimension i (0-based) from a z value that was encoded with N dimensions.

Ported from ncruces/ext/zorder and the SQLite-bundled zorder.c. Pure Go, no external dependencies.

import (
sqlite "gosqlite.org"
"gosqlite.org/ext/zorder"
)
if err := zorder.Register(conn); err != nil { ... }
row := db.QueryRow(`SELECT zorder(10, 20, 30), unzorder(zorder(10, 20, 30), 3, 1)`)

For pool-wide install via gosqlite.org.Driver.ConnectHook, blank-import the auto sub-package:

import _ "gosqlite.org/ext/zorder/auto"

Full API: pkg.go.dev/gosqlite.org/ext/zorder