GeoZL

Near-lossless

A quantizer snaps each sample to a grid, so the error never passes the bound you set. Every stage after it is lossless. quant_linear bounds the absolute error, quant_log the relative error, and quant_sqrt an error that grows with the signal, like sensor noise.

The bound covers every finite sample. NaN and infinities are outside it and belong to the nodata codec. On floats, quant_log and quant_sqrt guarantee their bound only from the smallest normal value up, because subnormals sit a fixed gap apart that no bound shrinking with the signal can cover.

The examples on this page compress one 256 by 256 uint16 tile, made the same way in each language, and check the bound on the way back.

import numpy as np
import geozl

y, x = np.mgrid[0:256, 0:256]
tile = (1000 + 3 * x + 2 * y + (x * y) % 17).astype(np.uint16)
library(geozl)

tile <- outer(0:255, 0:255, function(x, y) 1000 + 3 * x + 2 * y + (x * y) %% 17)
using GeoZL

tile = [UInt16(1000 + 3x + 2y + (x * y) % 17) for x in 0:255, y in 0:255]

15quant_linear

quant_linear snaps samples to one uniform grid, so every sample comes back within MAX_ERROR of where it started, in the data’s own units.

quant_linear snaps a continuous value to the nearest step of a uniform grid, staying within plus or minus MAX_ERROR, and stores the small integer index
each sample snaps to the nearest step of one uniform grid

Use it when precision does not depend on size, such as elevation in metres. In the GeoZL API a plain number selects it, so geozl.graph(tile, recipe, error=2) keeps every sample within 2 units of the original. Use quant_log for values spanning orders of magnitude.

ctid0x72D781
headerdtype + flags + step · 10 bytes
recipeLINEAR:MAX_ERROR=V
typesintegers 8 to 64-bit · floats 16 to 64-bit
decodex = q · step, or a copy when the stream holds the reconstruction
// integer input
step = max(1, floor(2 * MAX_ERROR));

q = sign(s) * ((abs(s) + step / 2) / step);   // encode
o = clamp(q * step);                           // decode
# error = 5 gives the same recipe.
g = geozl.graph(tile, "planar>zigzag>entropy", error="LINEAR:MAX_ERROR=5")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
diff = np.abs(back.astype(np.int64) - tile)
assert diff.max() <= 5
# error = 5 gives the same recipe.
g <- geozl_graph(tile, "planar>zigzag>entropy", error = "LINEAR:MAX_ERROR=5", dtype = "uint16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "uint16", dim = dim(tile))
stopifnot(max(abs(back - tile)) <= 5)
# error = 5 gives the same recipe.
g = GeoZL.graph(tile, "planar>zigzag>entropy"; error = "LINEAR:MAX_ERROR=5")
frame = GeoZL.compress(tile, g)
back = GeoZL.decompress(UInt16, frame, size(tile))
diff = abs.(Int.(back) .- Int.(tile))
@assert maximum(diff) <= 5

Source in core/src/quant_linear

16quant_log

quant_log bounds the error relative to the value. Its grid is uniform in log2. Values near 8 and 8,000 have the same percentage error bound.

quant_log holds a relative bound. The grid of levels around 8 and the grid around 8000 have the same shape, so a sample at either end comes back within the same one per cent, and in log2 the two grids share one uniform step
around 8 and around 8,000 the grid has the same shape, one uniform step in log2

Use it on proportional data such as precipitation, radiance or concentrations. A percentage such as error="1%" means LOG. Zero stays exact, and retiling a raster does not change a single value.

ctid0x72D782
headerdtype + flags + step · 10 bytes
recipeLOG:MAX_ERROR=P%
typesintegers 8 to 64-bit · floats 16 to 64-bit
decodeone table lookup, or a copy when the stream holds the reconstruction
// float input, A is -24 for f16, -149 for f32, -1074 for f64
q = index of the nearest level in log2 space;   // encode

o = q == 0 ? 0                                  // decode
           : sign(q) * exp2(A + (abs(q) - 1) * step);
# error = "1%" gives the same recipe.
g = geozl.graph(tile, "planar>zigzag>entropy", error="LOG:MAX_ERROR=1%")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
diff = np.abs(back.astype(np.int64) - tile)
assert np.all(diff <= 0.01 * tile)
# error = "1%" gives the same recipe.
g <- geozl_graph(tile, "planar>zigzag>entropy", error = "LOG:MAX_ERROR=1%", dtype = "uint16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "uint16", dim = dim(tile))
stopifnot(all(abs(back - tile) <= 0.01 * tile))
# error = "1%" gives the same recipe.
g = GeoZL.graph(tile, "planar>zigzag>entropy"; error = "LOG:MAX_ERROR=1%")
frame = GeoZL.compress(tile, g)
back = GeoZL.decompress(UInt16, frame, size(tile))
diff = abs.(Int.(back) .- Int.(tile))
@assert all(diff .<= 0.01 .* tile)

Source in core/src/quant_log

17quant_sqrt

quant_sqrt bounds the error at K standard deviations of the sensor noise, whose variance grows as a + b·x. Its levels spread out as the signal rises.

quant_sqrt holds a bound that grows with the square root of the value. The levels sit further apart the higher the value goes, so the slack at 81 is three times the slack at 9, and the index behind them still steps by one throughout
the levels spread out as the value grows. The slack at 81 is three times the slack at 9

Use it on data from photon-counting sensors. Fit a and b once per product with geozl.lossy.fit_noise() and reuse its recipe(). Per-tile fitting gives tiles different grids.

ctid0x72D783
headerdtype + flags + step + offset · 18 bytes
recipeSQRT:MAX_ERROR=KN, optionally ,A=a,B=b
typesintegers 8 to 64-bit · floats 16 to 64-bit
decodex = (q · step)² − offset, or a copy when the stream holds the reconstruction
// encode, the nearest level in the original units
t = (s + offset) / (step * step);
q = t <= 0.25 ? 0 : floor(0.5 + sqrt(t - 0.25));

// decode
o = (q * step) * (q * step) - offset;
# Fix A and B here; this synthetic tile has no shot-noise variance to fit.
g = geozl.graph(tile, "planar>zigzag>entropy", error="SQRT:MAX_ERROR=0.5N,A=4,B=1")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
diff = np.abs(back.astype(np.int64) - tile)
assert np.all(diff <= 0.5 * np.sqrt(4 + 1 * tile))
# Fix A and B here; this synthetic tile has no shot-noise variance to fit.
g <- geozl_graph(tile, "planar>zigzag>entropy", error = "SQRT:MAX_ERROR=0.5N,A=4,B=1", dtype = "uint16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "uint16", dim = dim(tile))
stopifnot(all(abs(back - tile) <= 0.5 * sqrt(4 + 1 * tile)))
# Fix A and B here; this synthetic tile has no shot-noise variance to fit.
g = GeoZL.graph(tile, "planar>zigzag>entropy"; error = "SQRT:MAX_ERROR=0.5N,A=4,B=1")
frame = GeoZL.compress(tile, g)
back = GeoZL.decompress(UInt16, frame, size(tile))
diff = abs.(Int.(back) .- Int.(tile))
@assert all(diff .<= 0.5 .* sqrt.(4 .+ 1 .* tile))

Source in core/src/quant_sqrt