R
- Package
geozlfor 64-bit R 4.1 or newer, sources inbindings/r.
The R package has the graph, compress, decompress and profile calls of the Python package and writes the same frames. It works on the double, integer and raw vectors that terra and stars hold.
Install
install.packages("geozl", repos = "https://asterisk-labs.r-universe.dev")
# or from GitHub
remotes::install_github("asterisk-labs/geozl", subdir = "bindings/r")
A source install compiles GeoZL and OpenZL with their own CMake files, so it needs CMake 3.21 or newer and C11 and C++17 compilers. webR is still not supported.
library(geozl)
tile <- outer(1:256, 1:256, function(x, y) 1000 + 3 * (x - 1) + 2 * (y - 1))
rows <- geozl_profile(tile, dtype = "uint16")
g <- geozl_graph(tile, rows$graph[[1]], dtype = "uint16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "uint16", dim = dim(tile))
identical(back, tile) # TRUE
Functions
Every exported function is below. A bad argument stops with a plain R error, and so does a request the library refuses as invalid, with ZL error code 21 or 30, such as an unknown recipe or a lossy tile outside its graph's domain; that error also has class geozl_invalid and the code in its code field. Any other refusal, such as a corrupt frame, signals a condition of class geozl_error whose code field holds the ZL error code, or NA when there is none. Every error has a NULL call.
e <- tryCatch(geozl_decompress(as.raw(0:63)), geozl_error = identity)
e$code # 10
geozl_profile
geozl_profile(x, prior = "planar", width = NULL, planes = NULL,
error = NULL, reps = 5L, nodata = NULL, dtype = NULL,
verify = FALSE)
Compresses and decompresses x with every recipe the prior allows and ranks them by compression ratio. Recipes that do not apply to the input are left out.
| prior | One predictor plus the id path, "planar", "med", "delta_w", "delta_n", "average", "wp_static" or "delta_1d". NULL tries every predictor and "none" only id. |
|---|---|
| reps | Round trips per recipe, from 1 to 2147483647. The fastest is kept. |
| verify | Check checksums while timing decode. The frames carry them either way. |
| others | As in geozl_graph. |
Returns a geozl_profile data frame, best ratio first, with the columns graph, bytes, ratio, encode_mbps, decode_mbps and shannon_pct. The attributes dims, dtype, raw_bytes, width, planes, prior, reps and error keep the input and settings, and print shows them above the table as Python does. When every recipe fails, it stops with geozl_profile failed and the first reason.
rows <- geozl_profile(tile, prior = NULL, dtype = "uint16", reps = 1)
rows$graph[[1]] # "med>zigzag>zstd"
geozl_graph
geozl_graph(x, method, width = NULL, planes = NULL, error = NULL,
nodata = NULL, dtype = NULL)
Builds a reusable graph for one recipe. The graph fixes the recipe, the storage type and the geometry of every tile compressed with it.
| x | A double, integer or raw vector or array. For a bounded-error graph it fixes the domain later tiles are checked against, so use data spanning the product. |
|---|---|
| method | A recipe as geozl_profile spells it, such as "planar>zigzag>pfor". Python's graph lists them all. |
| width | Row length in samples, at least 1. Defaults to dim(x)[1], which is 0 for an empty raster as in Python, and is required for a vector without dim. |
| planes | Stacked images predicted independently. Defaults to the last dimension of an array with three or more, otherwise 1. |
| error | NULL or 0 for lossless, a number for an absolute bound, "1%" for a relative bound, or a full LINEAR, LOG or SQRT recipe, as in Python. |
| nodata | A sentinel for missing samples. NA and NaN in x are stored as it. NaN selects NaN handling, which a float raster with missing values gets without asking, and an integer dtype refuses it. |
| dtype | The storage type, as numpy ("uint16"), terra ("INT2U") or GDAL ("UInt16") names it, for 8 to 64 bit integers and 16 to 64 bit floats. Defaults to "int32" for integer, "float64" for double and "uint8" for raw input. |
Returns a geozl_graph with the fields method, width, planes, dtype, itemsize, error and nodata. It prints them in that order and wraps native state, so it is not thread safe and does not survive saving and reloading.
Samples are converted to dtype first. Integer types refuse values that are not whole or do not fit, and float types round to nearest. A raw vector is taken as native samples of dtype already.
g <- geozl_graph(tile, "planar>zigzag>pfor", error = 2, dtype = "uint16")
g$error # "LINEAR:MAX_ERROR=2"
g$itemsize # 2
geozl_compress
geozl_compress(x, graph, coeffs = NULL)
Compresses one tile with a graph and returns the frame as a raw vector. x is converted to the graph's dtype, and its shape is not checked against the graph, but a row predictor refuses a tile longer than one row that is not whole rows of the graph's width. A tile the graph refuses, such as a lossy tile outside its domain, stops with geozl_compress failed (method = "...") and the reason.
| graph | A graph from geozl_graph. Its dtype field must still name the type it was built with. |
|---|---|
| coeffs | A list of integer vectors stored in the frame header, read back with geozl_coeffs. GeoZL gives them no meaning. |
frame <- geozl_compress(tile, g, coeffs = list(c(1L, 2L, 3L), 4L))
geozl_decompress
geozl_decompress(frame, dtype = NULL, dim = NULL, as = NULL,
nodata = NULL, verify = TRUE, max_output_size = NULL)
Decodes a frame. The frame stores no dtype or shape, so pass the ones it was written with.
| dtype | The storage type of the frame. Without it the result is the raw native bytes. |
|---|---|
| dim | Dimensions for the result, in samples, whole numbers from 0 to 2147483647 with a product of at most 252. |
| as | "double", the default with a dtype, "integer", or "raw" for the native bytes, which torch reads without a copy through torch_tensor_from_buffer(). |
| nodata | Samples equal to this value become NA, and NaN or NA turns every NaN of a float dtype into NA. It is checked as in geozl_graph, and needs a dtype with as other than "raw". |
| verify | Check the frame checksums. |
| max_output_size | Refuse a frame that declares more output bytes than this, a plain error. The limit is a finite number of at least 0. Set it for untrusted frames. |
A frame the library cannot read, one that declares more bytes than an R vector holds, or one that decodes to fewer bytes than it declares, signals a geozl_error. int64 and uint64 samples read as double warn when a value does not convert exactly, such as 2^53 + 1.
back <- geozl_decompress(frame, "uint16", dim = dim(tile), as = "integer")
geozl_coeffs
geozl_coeffs(frame)
Returns the coefficient vectors stored by geozl_compress as a list of integer vectors, or NULL when the frame has none. Only the frame header is read.
geozl_coeffs(frame) # list(c(1L, 2L, 3L), 4L)
Rasters
The fastest-varying dimension is the row. An array shaped (x, y) or (x, y, band), as stars holds a raster, gives the same frame as Python does for (y, x) or (band, y, x).
The matrix from terra::values(r) is shaped (ncell, nlyr), so pass width = terra::ncol(r), planes = terra::nlyr(r). terra::as.array() is (y, x, band), so pass it through aperm(a, c(2, 1, 3)) first.
R stores NA and NaN as the nodata sentinel, where Python leaves NaN in the samples. A lossless float64 frame keeps NA and NaN apart. float32 and float16 store both as one NaN, so NA reads back as NaN.
What it covers
The R package covers profile, graph, compress, decompress and coeffs. It has no OpenZL nodes like geozl.lossless and geozl.lossy, no fit_noise and no GPU decode. A SQRT recipe without A= and B= is fitted on the raster given to geozl_graph. To share one curve across products, fit it in Python with fit_noise and pass the recipe it writes.