Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions R/inspire_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ inspire_grid <- function(
include_llc = TRUE,
point_type = "centroid",
parallel = "auto",
quiet = FALSE,
quiet = getOption("gridmaker.quiet", FALSE),
dsn = NULL,
layer = NULL,
max_memory_gb = NULL,
Expand Down Expand Up @@ -116,7 +116,7 @@ inspire_grid.sf <- function(
include_llc = TRUE,
point_type = "centroid",
parallel = "auto",
quiet = FALSE,
quiet = getOption("gridmaker.quiet", FALSE),
dsn = NULL,
layer = NULL,
max_memory_gb = NULL,
Expand Down Expand Up @@ -160,7 +160,7 @@ inspire_grid.sfc <- function(
include_llc = TRUE,
point_type = "centroid",
parallel = "auto",
quiet = FALSE,
quiet = getOption("gridmaker.quiet", FALSE),
dsn = NULL,
layer = NULL,
max_memory_gb = NULL,
Expand Down Expand Up @@ -204,7 +204,7 @@ inspire_grid.bbox <- function(
include_llc = TRUE,
point_type = "centroid",
parallel = "auto",
quiet = FALSE,
quiet = getOption("gridmaker.quiet", FALSE),
dsn = NULL,
layer = NULL,
max_memory_gb = NULL,
Expand Down Expand Up @@ -248,7 +248,7 @@ inspire_grid.numeric <- function(
include_llc = TRUE,
point_type = "centroid",
parallel = "auto",
quiet = FALSE,
quiet = getOption("gridmaker.quiet", FALSE),
dsn = NULL,
layer = NULL,
max_memory_gb = NULL,
Expand Down Expand Up @@ -292,7 +292,7 @@ inspire_grid.matrix <- function(
include_llc = TRUE,
point_type = "centroid",
parallel = "auto",
quiet = FALSE,
quiet = getOption("gridmaker.quiet", FALSE),
dsn = NULL,
layer = NULL,
max_memory_gb = NULL,
Expand Down Expand Up @@ -337,7 +337,7 @@ inspire_grid.character <- function(
include_llc = TRUE, # Used
point_type = "llc", # Used
parallel = "auto", # Ignored (Sink)
quiet = FALSE, # Used
quiet = getOption("gridmaker.quiet", FALSE), # Used
dsn = NULL, # Used
layer = NULL, # Used
max_memory_gb = NULL, # Ignored (Sink)
Expand Down Expand Up @@ -429,7 +429,7 @@ inspire_grid_from_extent <- function(
include_llc = TRUE,
point_type = "centroid",
parallel = "auto",
quiet = FALSE,
quiet = getOption("gridmaker.quiet", FALSE),
dsn = NULL,
layer = NULL,
max_memory_gb = NULL,
Expand Down
2 changes: 1 addition & 1 deletion R/inspire_grid_from_ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ inspire_grid_from_ids <- function(
include_llc = TRUE,
id_format = c("both", "long", "short"),
axis_order = c("NE", "EN"),
quiet = FALSE,
quiet = getOption("gridmaker.quiet", FALSE),
dsn = NULL,
layer = NULL,
...
Expand Down
23 changes: 14 additions & 9 deletions R/stream_grid_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@ stream_grid_raster_terra <- function(
max_memory_gb = NULL
) {
# --- 1. SETUP ---
# Configure terra options for this session
old_opts <- terra::terraOptions()
on.exit(suppressWarnings(do.call(terra::terraOptions, old_opts)), add = TRUE)
# Save and restore terra options on exit
old_opts <- terra::terraOptions(print = FALSE)
on.exit({
# Only restore options that are safe to restore
restore_opts <- old_opts[names(old_opts) %in%
c("memfrac", "memmax", "memmin", "todisk", "tempdir")]
do.call(terra::terraOptions, restore_opts)
}, add = TRUE)

# Configure for streaming (always write to disk)
terra::terraOptions(todisk = TRUE)

if (!is.null(max_memory_gb)) {
terra::terraOptions(memmax = max_memory_gb, todisk = TRUE)
} else {
terra::terraOptions(todisk = TRUE)
if (is.null(old_opts$memfrac) || old_opts$memfrac > 0.6) {
terra::terraOptions(memfrac = 0.5)
}
terra::terraOptions(memmax = max_memory_gb)
} else if (old_opts$memfrac > 0.6) {
Comment thread
e-kotov marked this conversation as resolved.
Outdated
terra::terraOptions(memfrac = 0.5)
}

# --- RAT SUPPORT DISABLED ---
Expand Down
22 changes: 14 additions & 8 deletions R/stream_grid_raster_parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,22 @@ stream_raster_parallel_mirai <- function(
}

# --- 1. SETUP ---
old_opts <- terra::terraOptions()
on.exit(suppressWarnings(do.call(terra::terraOptions, old_opts)), add = TRUE)
# Save and restore terra options on exit
old_opts <- terra::terraOptions(print = FALSE)
on.exit({
# Only restore options that are safe to restore
restore_opts <- old_opts[names(old_opts) %in%
c("memfrac", "memmax", "memmin", "todisk", "tempdir")]
do.call(terra::terraOptions, restore_opts)
}, add = TRUE)

# Configure for streaming (always write to disk)
terra::terraOptions(todisk = TRUE)

if (!is.null(max_memory_gb)) {
terra::terraOptions(memmax = max_memory_gb, todisk = TRUE)
} else {
terra::terraOptions(todisk = TRUE)
if (is.null(old_opts$memfrac) || old_opts$memfrac > 0.6) {
terra::terraOptions(memfrac = 0.5)
}
terra::terraOptions(memmax = max_memory_gb)
} else if (old_opts$memfrac > 0.6) {
Comment thread
e-kotov marked this conversation as resolved.
Outdated
terra::terraOptions(memfrac = 0.5)
}

# RAT deprecation warning
Expand Down
99 changes: 57 additions & 42 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,40 @@ regex_match <- function(text, pattern, i = NULL, ...) {
c(xmin = 0, ymin = 0, xmax = n1 * cellsize_m, ymax = cellsize_m),
crs = grid_crs
)
sample_grid_1 <- inspire_grid_from_extent_internal(

# Construct args to avoid passing ignored point_type (which warns)
args1 <- list(
grid_extent = sample_extent_1,
cellsize_m = cellsize_m,
output_type = output_type,
id_format = id_format,
axis_order = "NE",
include_llc = include_llc,
point_type = point_type
include_llc = include_llc
)
if (output_type == "sf_points") {
args1$point_type <- point_type
}
sample_grid_1 <- do.call(inspire_grid_from_extent_internal, args1)
size1 <- as.numeric(utils::object.size(sample_grid_1))

# Create the second, larger sample grid.
sample_extent_2 <- sf::st_bbox(
c(xmin = 0, ymin = 0, xmax = n2 * cellsize_m, ymax = cellsize_m),
crs = grid_crs
)
sample_grid_2 <- inspire_grid_from_extent_internal(

args2 <- list(
grid_extent = sample_extent_2,
cellsize_m = cellsize_m,
output_type = output_type,
id_format = id_format,
axis_order = "NE",
include_llc = include_llc,
point_type = point_type
include_llc = include_llc
)
if (output_type == "sf_points") {
args2$point_type <- point_type
}
sample_grid_2 <- do.call(inspire_grid_from_extent_internal, args2)
size2 <- as.numeric(utils::object.size(sample_grid_2))

# Calculate the memory cost per additional cell (the slope).
Expand Down Expand Up @@ -261,36 +270,42 @@ regex_match <- function(text, pattern, i = NULL, ...) {
n1 <- 10
n2 <- 20

# Create sample 1
# Create sample 1
Comment thread
e-kotov marked this conversation as resolved.
sample_extent_1 <- sf::st_bbox(
c(xmin = 0, ymin = 0, xmax = n1 * cellsize_m, ymax = cellsize_m),
crs = grid_crs
)
sample_grid_1 <- inspire_grid_from_extent_internal(

# Prepare safe arguments
out_type <- dot_args$output_type %||% "sf_polygons"
pt_type <- dot_args$point_type %||% "centroid"

args1 <- list(
grid_extent = sample_extent_1,
cellsize_m = cellsize_m,
output_type = dot_args$output_type %||% "sf_polygons",
output_type = out_type,
id_format = dot_args$id_format %||% "both",
axis_order = dot_args$axis_order %||% "NE",
include_llc = dot_args$include_llc %||% TRUE,
point_type = dot_args$point_type %||% "centroid"
include_llc = dot_args$include_llc %||% TRUE
)
if (out_type == "sf_points") {
args1$point_type <- pt_type
}

sample_grid_1 <- do.call(inspire_grid_from_extent_internal, args1)
size1 <- as.numeric(utils::object.size(sample_grid_1))

# Create sample 2
sample_extent_2 <- sf::st_bbox(
c(xmin = 0, ymin = 0, xmax = n2 * cellsize_m, ymax = cellsize_m),
crs = grid_crs
)
sample_grid_2 <- inspire_grid_from_extent_internal(
grid_extent = sample_extent_2,
cellsize_m = cellsize_m,
output_type = dot_args$output_type %||% "sf_polygons",
id_format = dot_args$id_format %||% "both",
axis_order = dot_args$axis_order %||% "NE",
include_llc = dot_args$include_llc %||% TRUE,
point_type = dot_args$point_type %||% "centroid"
)

args2 <- args1
args2$grid_extent <- sample_extent_2

sample_grid_2 <- do.call(inspire_grid_from_extent_internal, args2)
size2 <- as.numeric(utils::object.size(sample_grid_2))

# Calculate marginal bytes per cell
Expand Down Expand Up @@ -445,29 +460,7 @@ validate_disk_compatibility <- function(output_type, dsn) {
# Formats explicitly confirmed to NOT support append
no_append_formats <- c("kml", "gml")

# --- 1. Handle text outputs (csv, tsv, txt) ---
if (is_text) {
if (!requireNamespace("readr", quietly = TRUE)) {
stop(
"Package 'readr' is required to write to .csv/.tsv/.txt files. Please install it.",
call. = FALSE
)
}
return(TRUE)
}

# --- 2. Handle dataframe outputs ---
if (is_dataframe) {
stop(
sprintf(
"Output type 'dataframe' cannot be written to file extension '.%s'.\n Please use '.csv', '.tsv', or '.txt' for dataframes, or change output_type to 'sf_polygons'/'sf_points'.",
ext
),
call. = FALSE
)
}

# --- 3. Handle raster outputs (spatraster) ---
# --- 1. Handle raster outputs (spatraster) ---
if (is_raster) {
if (!nzchar(ext)) {
stop(
Expand All @@ -494,6 +487,28 @@ validate_disk_compatibility <- function(output_type, dsn) {
return(TRUE)
}

# --- 2. Handle text outputs (csv, tsv, txt) ---
if (is_text) {
if (!requireNamespace("readr", quietly = TRUE)) {
stop(
"Package 'readr' is required to write to .csv/.tsv/.txt files. Please install it.",
call. = FALSE
)
}
return(TRUE)
}

# --- 3. Handle dataframe outputs ---
if (is_dataframe) {
stop(
sprintf(
"Output type 'dataframe' cannot be written to file extension '.%s'.\n Please use '.csv', '.tsv', or '.txt' for dataframes, or change output_type to 'sf_polygons'/'sf_points'.",
ext
),
call. = FALSE
)
}

# --- 4. Handle vector outputs (sf_polygons, sf_points) ---
if (is_spatial_vector) {
driver_name <- .ext_to_driver(ext, "vector")
Expand Down
18 changes: 9 additions & 9 deletions man/inspire_grid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Gridmaker Options for Testing
options(gridmaker.quiet = TRUE)

library(sf)

# --- Test Data Preparation ---
Expand Down
Loading