Skip to content

Commit 64cd25a

Browse files
committed
Add include_rat option and streaming SpatRaster disk writing; add stream_grid_raster & parquet helpers, update docs/tests and include HDF in format validation
1 parent 5aa136d commit 64cd25a

8 files changed

Lines changed: 656 additions & 25 deletions

File tree

R/inspire_grid.R

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ inspire_grid <- function(
7979
dsn = NULL,
8080
layer = NULL,
8181
max_memory_gb = NULL,
82+
include_rat = FALSE,
8283
...
8384
) {
8485
# Validate output_type early
@@ -116,6 +117,7 @@ inspire_grid.sf <- function(
116117
dsn = NULL,
117118
layer = NULL,
118119
max_memory_gb = NULL,
120+
include_rat = FALSE,
119121
...
120122
) {
121123
inspire_grid_from_extent(
@@ -135,6 +137,7 @@ inspire_grid.sf <- function(
135137
dsn = dsn,
136138
layer = layer,
137139
max_memory_gb = max_memory_gb,
140+
include_rat = include_rat,
138141
...
139142
)
140143
}
@@ -158,6 +161,7 @@ inspire_grid.sfc <- function(
158161
dsn = NULL,
159162
layer = NULL,
160163
max_memory_gb = NULL,
164+
include_rat = FALSE,
161165
...
162166
) {
163167
inspire_grid_from_extent(
@@ -177,6 +181,7 @@ inspire_grid.sfc <- function(
177181
dsn = dsn,
178182
layer = layer,
179183
max_memory_gb = max_memory_gb,
184+
include_rat = include_rat,
180185
...
181186
)
182187
}
@@ -200,6 +205,7 @@ inspire_grid.bbox <- function(
200205
dsn = NULL,
201206
layer = NULL,
202207
max_memory_gb = NULL,
208+
include_rat = FALSE,
203209
...
204210
) {
205211
inspire_grid_from_extent(
@@ -219,6 +225,7 @@ inspire_grid.bbox <- function(
219225
dsn = dsn,
220226
layer = layer,
221227
max_memory_gb = max_memory_gb,
228+
include_rat = include_rat,
222229
...
223230
)
224231
}
@@ -242,6 +249,7 @@ inspire_grid.numeric <- function(
242249
dsn = NULL,
243250
layer = NULL,
244251
max_memory_gb = NULL,
252+
include_rat = FALSE,
245253
...
246254
) {
247255
inspire_grid_from_extent(
@@ -261,6 +269,7 @@ inspire_grid.numeric <- function(
261269
dsn = dsn,
262270
layer = layer,
263271
max_memory_gb = max_memory_gb,
272+
include_rat = include_rat,
264273
...
265274
)
266275
}
@@ -284,6 +293,7 @@ inspire_grid.matrix <- function(
284293
dsn = NULL,
285294
layer = NULL,
286295
max_memory_gb = NULL,
296+
include_rat = FALSE,
287297
...
288298
) {
289299
inspire_grid_from_extent(
@@ -303,6 +313,7 @@ inspire_grid.matrix <- function(
303313
dsn = dsn,
304314
layer = layer,
305315
max_memory_gb = max_memory_gb,
316+
include_rat = include_rat,
306317
...
307318
)
308319
}
@@ -327,6 +338,7 @@ inspire_grid.character <- function(
327338
dsn = NULL, # Used
328339
layer = NULL, # Used
329340
max_memory_gb = NULL, # Ignored (Sink)
341+
include_rat = FALSE, # Ignored (Sink)
330342
...
331343
) {
332344
# 1. Guardrails: Warn if specific ignored arguments are provided
@@ -417,6 +429,7 @@ inspire_grid_from_extent <- function(
417429
dsn = NULL,
418430
layer = NULL,
419431
max_memory_gb = NULL,
432+
include_rat = FALSE,
420433
...
421434
) {
422435
# --- 1. Validate Arguments ---
@@ -484,6 +497,7 @@ inspire_grid_from_extent <- function(
484497
axis_order = axis_order,
485498
include_llc = include_llc,
486499
point_type = point_type,
500+
include_rat = include_rat,
487501
...
488502
)
489503
parallel_backend_args <- c(backend_args, list(quiet = quiet))
@@ -524,6 +538,7 @@ inspire_grid_from_extent <- function(
524538
}
525539

526540
# --- 3. RASTER PATH (SEQUENTIAL ONLY) ---
541+
# --- 3. RASTER PATH ---
527542
if (output_type == "spatraster") {
528543
if (!requireNamespace("terra", quietly = TRUE)) {
529544
stop(
@@ -535,11 +550,29 @@ inspire_grid_from_extent <- function(
535550
if (isTRUE(parallel) || (is.character(parallel) && parallel == "auto")) {
536551
if (!quiet) {
537552
message(
538-
"Note: 'spatraster' output does not support parallel processing. Running sequentially."
553+
"Note: 'spatraster' output is processed sequentially by terra (streaming)."
539554
)
540555
}
541556
}
542557

558+
# Case A: Streaming to Disk (File-backed)
559+
if (!is.null(dsn)) {
560+
# Validate extension
561+
validate_disk_compatibility(output_type, dsn)
562+
563+
return(stream_grid_raster_terra(
564+
grid_extent = grid_extent,
565+
cellsize_m = cellsize_m,
566+
crs = crs,
567+
dsn = dsn,
568+
layer = layer,
569+
dot_args = backend_args,
570+
quiet = quiet,
571+
max_memory_gb = max_memory_gb
572+
))
573+
}
574+
575+
# Case B: In-Memory Generation (Legacy/Small grids)
543576
# Collect arguments
544577
all_args <- c(
545578
list(grid_extent = grid_extent, cellsize_m = cellsize_m, crs = crs),
@@ -548,17 +581,6 @@ inspire_grid_from_extent <- function(
548581

549582
# Generate in-memory
550583
r <- do.call(inspire_grid_from_extent_internal, all_args)
551-
552-
# Write to disk if requested
553-
if (!is.null(dsn)) {
554-
if (!quiet) {
555-
message("Writing raster to ", dsn)
556-
}
557-
# Pass ellipsis (...) to writeRaster for options like compression
558-
terra::writeRaster(r, filename = dsn, overwrite = TRUE, ...)
559-
return(invisible(dsn))
560-
}
561-
562584
return(r)
563585
}
564586

R/inspire_grid_params.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@
5555
#' For \code{output_type = "spatraster"} writing, these are passed to \code{\link[terra]{writeRaster}}.
5656
#' For streaming backends (`mirai` or sequential), this can include \code{max_cells_per_chunk} to control memory usage.
5757
#' @param max_memory_gb A numeric value. Maximum memory in gigabytes to use for grid creation. Default is `NULL`, in which case there is an automatic limit based on **available free system memory** (not total system RAM).
58+
#' @param include_rat Logical. If `TRUE`, generate a Raster Attribute Table (RAT)
59+
#' mapping numeric cell IDs to INSPIRE grid ID strings. Default is `FALSE`.
60+
#'
61+
#' **What is a RAT?** A Raster Attribute Table stores metadata (like INSPIRE IDs)
62+
#' for each unique raster value. Without RAT, raster cells contain only numeric
63+
#' IDs (1, 2, 3...). With RAT, software like QGIS/R can display the IDs as
64+
#' human-readable labels.
65+
#'
66+
#' **Format-specific behavior:**
67+
#' \itemize{
68+
#' \item **GeoTIFF (.tif):** RAT stored in `.tif.aux.xml` sidecar file (XML).
69+
#' **Warning:** This sidecar can be **larger than the TIFF itself** for large
70+
#' grids. For chunked/streaming writes, requires a second pass (slower).
71+
#' Consider NetCDF or KEA formats for large grids with labels.
72+
#' \item **NetCDF (.nc), KEA (.kea):** RAT embedded natively. **Recommended**
73+
#' for large grids requiring labels.
74+
#' \item **HDF5 (.hdf):** RAT **not supported**. An error is raised if
75+
#' `include_rat = TRUE`.
76+
#' }
5877
#'
5978
#' @name inspire_grid_params
6079
#' @keywords internal

0 commit comments

Comments
 (0)