Module location

Module location 

Source
Expand description

Variable-precision geographic location encoding.

NodeLocation represents a geographic position as a 1–7 byte grid code. Each byte refines the location to a 16×16 sub-grid of the parent cell, with the high nibble indexing latitude and the low nibble indexing longitude.

The encoding has a useful truncation property: dropping trailing bytes gives the correct lower-precision encoding of the same position — no recomputation needed.

Every coordinate pair in this module is (latitude, longitude), in that order, without exception — parameters, return tuples, and rendered text alike.

§Encoding

For a given precision N (1–7 bytes), two 4N-bit indices are computed:

lat_index = floor((lat +  90) × 16^N / 180)
lon_index = floor((lon + 180) × 16^N / 360)

Nibbles are extracted most-significant-first into bytes:

byte[k] = ((lat_index >> (4×(N-1-k))) & 0xF) << 4
        | ((lon_index >> (4×(N-1-k))) & 0xF)

§Precision

BytesEquator cell, lat × lon (approx.)
11,250 × 2,500 km
278 × 156 km
34.9 × 9.8 km
4305 × 610 m
519 × 38 m
61.2 × 2.4 m
77.5 × 15 cm

§Feature: f64

By default all floating-point arithmetic uses f32, which is adequate for precisions 1–5 (cells ≥ 19 m). Enable the f64 crate feature for accurate encoding and decoding at 6–7 byte precision.

Structs§

NodeLocation
A variable-precision geographic location encoded as a 1–7 byte grid code.

Constants§

MAX_PRECISION
Maximum supported precision in bytes.