Skip to content

Commit 5147273

Browse files
committed
Refactored S3 method signatures for inspire_grid; improved argument documentation and API safety for character input; updated tests and Rd files for consistency.
1 parent 7863e48 commit 5147273

5 files changed

Lines changed: 513 additions & 109 deletions

File tree

R/inspire_grid.R

Lines changed: 274 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,29 @@
1313
#' including INSPIRE-compliant grid IDs and automatic parallel processing with
1414
#' `mirai` and `future` backends.
1515
#'
16-
#' @param x,grid_extent,ids The main input object.
17-
#' \itemize{
18-
#' \item For \code{inspire_grid}: The generic input (either a spatial object or character vector).
19-
#' \item For \code{inspire_grid_from_extent}: The spatial object (\code{grid_extent}) defining the extent.
20-
#' \item For \code{inspire_grid_from_ids}: The character vector of IDs (\code{ids}).
21-
#' }
16+
#' @param x The main input object: either a spatial object (extent) or a character vector (INSPIRE IDs).
2217
#' @param cellsize_m A single integer representing the grid cell size in metres
23-
#' (e.g., 1000 for a 1 km grid). Required when \code{x} is a spatial extent.
18+
#' (e.g., 1000 for a 1 km grid). Required for spatial inputs.
2419
#' @param crs The coordinate reference system (CRS) for the output grid.
2520
#' Accepts various formats handled by `sf::st_crs()`: an integer or numeric
2621
#' EPSG code (e.g., `3035`), a string representation like `"epsg:3035"`, or
2722
#' a `crs` object. If `NULL` (default), the CRS is inherited from
28-
#' \code{x}. If \code{x} also lacks a CRS, the function will stop
29-
#' with an error. (Used only when \code{x} is an extent.)
23+
#' the spatial input. If the input also lacks a CRS, the function will stop
24+
#' with an error.
3025
#' @param output_type The class of the output object: `"sf_polygons"` (default) creates
3126
#' a spatial object with polygon geometries, `"sf_points"` creates an `sf`
3227
#' object with point geometries, `"dataframe"` creates a data frame with
3328
#' grid cell centroid coordinates (`X_centroid`, `Y_centroid`), and
3429
#' `"spatraster"` creates a `terra::SpatRaster` object with grid cell IDs
3530
#' stored as factor levels (Raster Attribute Table).
3631
#' @param clip_to_input A logical value. If `TRUE`, the grid is filtered to
37-
#' include only cells that intersect \code{x}. This does not cut
38-
#' cell geometries. (Used only when \code{x} is an extent.)
32+
#' include only cells that intersect the spatial input. This does not cut
33+
#' cell geometries.
3934
#' @param use_convex_hull A logical value. If `TRUE` and `clip_to_input` is
4035
#' `TRUE`, the grid is clipped to the convex hull of the input geometry,
4136
#' which can be faster and simpler than using a complex polygon.
4237
#' @param buffer_m A numeric value. If `clip_to_input` is `TRUE`, this specifies
43-
#' a buffer distance in metres to apply to \code{x} before clipping.
38+
#' a buffer distance in metres to apply to the spatial input before clipping.
4439
#' Defaults to `0` (no buffer).
4540
#' @param id_format A character string specifying which grid cell IDs to generate.
4641
#' Options are `"both"` (default), `"long"`, `"short"`, or `"none"`.
@@ -56,16 +51,6 @@
5651
#' in the output.
5752
#' @param point_type A character string, used only when `output_type = "sf_points"`.
5853
#' Determines the location of the points: `"centroid"` for the center of the cell, or `"llc"` for the lower-left corner.
59-
#' @param dsn The destination for the output grid. For sf objects, this is passed to
60-
#' `sf::st_write`. For `spatraster` output, this uses `terra::writeRaster`.
61-
#' This can be a file path (e.g., `"path/to/grid.gpkg"` for vector data or
62-
#' `"path/to/grid.tif"` for raster data) or a database connection string.
63-
#' If \code{dsn} is provided, the grid is written to the specified location
64-
#' instead of being returned as an object.
65-
#' @param layer The name of the grid layer, passed directly to `sf::st_write`.
66-
#' Its interpretation depends on the destination driver. For a GeoPackage
67-
#' file, this will be the layer name. If \code{dsn} is a file path and `layer` is
68-
#' not specified, it defaults to the file's base name.
6954
#' @param parallel Controls parallel execution. Options are:
7055
#' \itemize{
7156
#' \item **`'auto'` (default):** Automatically detects and uses a configured
@@ -86,22 +71,24 @@
8671
#' <50k cells use max 4 workers, <500k cells use max 8 workers, <2M cells use max 16 workers.
8772
#' This automatic limiting can be overridden by setting `options(gridmaker.tile_multiplier)`.
8873
#' **Note:** Parallel processing is not supported when `output_type = "spatraster"`.
89-
#' Raster output will always run sequentially. (Used only when \code{x} is an extent.)
74+
#' Raster output will always run sequentially.
9075
#' @param quiet Logical value. If `TRUE`, all progress messages and progress bars are suppressed. Defaults to `FALSE`.
91-
#' @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 of available system memory. The available memory detection may fail on certain HPC (High Performance Computing) systems where jobs are allocated a fixed amount of memory that is less than the total system memory of the allocated node. (Used only when \code{x} is an extent.)
92-
#' @param ... Additional arguments passed to the specific method.
93-
#' \itemize{
94-
#' \item For extent-based generation, these are passed to the backend handlers.
95-
#' \item When writing to text files (e.g., .csv, .tsv) via \code{dsn}, these arguments are passed to \code{\link[readr]{write_delim}} (e.g., \code{na = "NA"}, \code{quote = "all"}).
96-
#' \item When writing to spatial files via \code{dsn}, these are passed to \code{\link[sf]{st_write}}.
97-
#' \item For \code{output_type = "spatraster"} writing, these are passed to \code{\link[terra]{writeRaster}}.
98-
#' \item For streaming backends (`mirai` or sequential), this can include \code{max_cells_per_chunk} to control memory usage.
99-
#' }
100-
#'
101-
#' @details
102-
#' The `...` parameter allows you to pass the parameters listed above (like `cellsize_m`,
103-
#' `crs`, `output_type`, etc.) to the appropriate backend method depending on whether
104-
#' you're creating a grid from an extent or reconstructing from INSPIRE IDs.
76+
#' @param dsn The destination for the output grid. For sf objects, this is passed to
77+
#' `sf::st_write`. For `spatraster` output, this uses `terra::writeRaster`.
78+
#' This can be a file path (e.g., `"path/to/grid.gpkg"` for vector data or
79+
#' `"path/to/grid.tif"` for raster data) or a database connection string.
80+
#' If \code{dsn} is provided, the grid is written to the specified location
81+
#' instead of being returned as an object.
82+
#' @param layer The name of the grid layer, passed directly to `sf::st_write`.
83+
#' Its interpretation depends on the destination driver. For a GeoPackage
84+
#' file, this will be the layer name. If \code{dsn} is a file path and `layer` is
85+
#' not specified, it defaults to the file's base name.
86+
#' @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 of available system memory. The available memory detection may fail on certain HPC (High Performance Computing) systems where jobs are allocated a fixed amount of memory that is less than the total system memory of the allocated node.
87+
#' @param ... Additional arguments passed to backend handlers.
88+
#' When writing to text files (e.g., .csv, .tsv) via \code{dsn}, these arguments are passed to \code{\link[readr]{write_delim}} (e.g., \code{na = "NA"}, \code{quote = "all"}).
89+
#' When writing to spatial files via \code{dsn}, these are passed to \code{\link[sf]{st_write}}.
90+
#' For \code{output_type = "spatraster"} writing, these are passed to \code{\link[terra]{writeRaster}}.
91+
#' For streaming backends (`mirai` or sequential), this can include \code{max_cells_per_chunk} to control memory usage.
10592
#'
10693
#' @return If \code{dsn} is \code{NULL} (the default), an \code{sf} object, \code{data.frame},
10794
#' or \code{SpatRaster} representing the grid. If \code{dsn} is specified, the function writes
@@ -132,31 +119,275 @@ inspire_grid <- function(
132119

133120
#' @export
134121
#' @rdname inspire_grid
135-
inspire_grid.sf <- function(x, ...) inspire_grid_from_extent(grid_extent = x, ...)
122+
inspire_grid.sf <- function(
123+
x,
124+
cellsize_m = NULL,
125+
crs = NULL,
126+
output_type = "sf_polygons",
127+
clip_to_input = FALSE,
128+
use_convex_hull = FALSE,
129+
buffer_m = 0,
130+
id_format = "both",
131+
axis_order = "NE",
132+
include_llc = TRUE,
133+
point_type = "centroid",
134+
parallel = "auto",
135+
quiet = FALSE,
136+
dsn = NULL,
137+
layer = NULL,
138+
max_memory_gb = NULL,
139+
...
140+
) {
141+
inspire_grid_from_extent(
142+
grid_extent = x,
143+
cellsize_m = cellsize_m,
144+
crs = crs,
145+
output_type = output_type,
146+
clip_to_input = clip_to_input,
147+
use_convex_hull = use_convex_hull,
148+
buffer_m = buffer_m,
149+
id_format = id_format,
150+
axis_order = axis_order,
151+
include_llc = include_llc,
152+
point_type = point_type,
153+
parallel = parallel,
154+
quiet = quiet,
155+
dsn = dsn,
156+
layer = layer,
157+
max_memory_gb = max_memory_gb,
158+
...
159+
)
160+
}
136161

137162
#' @export
138163
#' @rdname inspire_grid
139-
inspire_grid.sfc <- function(x, ...) inspire_grid_from_extent(grid_extent = x, ...)
164+
inspire_grid.sfc <- function(
165+
x,
166+
cellsize_m = NULL,
167+
crs = NULL,
168+
output_type = "sf_polygons",
169+
clip_to_input = FALSE,
170+
use_convex_hull = FALSE,
171+
buffer_m = 0,
172+
id_format = "both",
173+
axis_order = "NE",
174+
include_llc = TRUE,
175+
point_type = "centroid",
176+
parallel = "auto",
177+
quiet = FALSE,
178+
dsn = NULL,
179+
layer = NULL,
180+
max_memory_gb = NULL,
181+
...
182+
) {
183+
inspire_grid_from_extent(
184+
grid_extent = x,
185+
cellsize_m = cellsize_m,
186+
crs = crs,
187+
output_type = output_type,
188+
clip_to_input = clip_to_input,
189+
use_convex_hull = use_convex_hull,
190+
buffer_m = buffer_m,
191+
id_format = id_format,
192+
axis_order = axis_order,
193+
include_llc = include_llc,
194+
point_type = point_type,
195+
parallel = parallel,
196+
quiet = quiet,
197+
dsn = dsn,
198+
layer = layer,
199+
max_memory_gb = max_memory_gb,
200+
...
201+
)
202+
}
140203

141204
#' @export
142205
#' @rdname inspire_grid
143-
inspire_grid.bbox <- function(x, ...) inspire_grid_from_extent(grid_extent = x, ...)
206+
inspire_grid.bbox <- function(
207+
x,
208+
cellsize_m = NULL,
209+
crs = NULL,
210+
output_type = "sf_polygons",
211+
clip_to_input = FALSE,
212+
use_convex_hull = FALSE,
213+
buffer_m = 0,
214+
id_format = "both",
215+
axis_order = "NE",
216+
include_llc = TRUE,
217+
point_type = "centroid",
218+
parallel = "auto",
219+
quiet = FALSE,
220+
dsn = NULL,
221+
layer = NULL,
222+
max_memory_gb = NULL,
223+
...
224+
) {
225+
inspire_grid_from_extent(
226+
grid_extent = x,
227+
cellsize_m = cellsize_m,
228+
crs = crs,
229+
output_type = output_type,
230+
clip_to_input = clip_to_input,
231+
use_convex_hull = use_convex_hull,
232+
buffer_m = buffer_m,
233+
id_format = id_format,
234+
axis_order = axis_order,
235+
include_llc = include_llc,
236+
point_type = point_type,
237+
parallel = parallel,
238+
quiet = quiet,
239+
dsn = dsn,
240+
layer = layer,
241+
max_memory_gb = max_memory_gb,
242+
...
243+
)
244+
}
144245

145246
#' @export
146247
#' @rdname inspire_grid
147-
inspire_grid.numeric <- function(x, ...) inspire_grid_from_extent(grid_extent = x, ...)
248+
inspire_grid.numeric <- function(
249+
x,
250+
cellsize_m = NULL,
251+
crs = NULL,
252+
output_type = "sf_polygons",
253+
clip_to_input = FALSE,
254+
use_convex_hull = FALSE,
255+
buffer_m = 0,
256+
id_format = "both",
257+
axis_order = "NE",
258+
include_llc = TRUE,
259+
point_type = "centroid",
260+
parallel = "auto",
261+
quiet = FALSE,
262+
dsn = NULL,
263+
layer = NULL,
264+
max_memory_gb = NULL,
265+
...
266+
) {
267+
inspire_grid_from_extent(
268+
grid_extent = x,
269+
cellsize_m = cellsize_m,
270+
crs = crs,
271+
output_type = output_type,
272+
clip_to_input = clip_to_input,
273+
use_convex_hull = use_convex_hull,
274+
buffer_m = buffer_m,
275+
id_format = id_format,
276+
axis_order = axis_order,
277+
include_llc = include_llc,
278+
point_type = point_type,
279+
parallel = parallel,
280+
quiet = quiet,
281+
dsn = dsn,
282+
layer = layer,
283+
max_memory_gb = max_memory_gb,
284+
...
285+
)
286+
}
148287

149288
#' @export
150289
#' @rdname inspire_grid
151-
inspire_grid.matrix <- function(x, ...) inspire_grid_from_extent(grid_extent = x, ...)
290+
inspire_grid.matrix <- function(
291+
x,
292+
cellsize_m = NULL,
293+
crs = NULL,
294+
output_type = "sf_polygons",
295+
clip_to_input = FALSE,
296+
use_convex_hull = FALSE,
297+
buffer_m = 0,
298+
id_format = "both",
299+
axis_order = "NE",
300+
include_llc = TRUE,
301+
point_type = "centroid",
302+
parallel = "auto",
303+
quiet = FALSE,
304+
dsn = NULL,
305+
layer = NULL,
306+
max_memory_gb = NULL,
307+
...
308+
) {
309+
inspire_grid_from_extent(
310+
grid_extent = x,
311+
cellsize_m = cellsize_m,
312+
crs = crs,
313+
output_type = output_type,
314+
clip_to_input = clip_to_input,
315+
use_convex_hull = use_convex_hull,
316+
buffer_m = buffer_m,
317+
id_format = id_format,
318+
axis_order = axis_order,
319+
include_llc = include_llc,
320+
point_type = point_type,
321+
parallel = parallel,
322+
quiet = quiet,
323+
dsn = dsn,
324+
layer = layer,
325+
max_memory_gb = max_memory_gb,
326+
...
327+
)
328+
}
152329

153330
#' @export
154331
#' @rdname inspire_grid
155-
inspire_grid.character <- function(x, ...) inspire_grid_from_ids(ids = x, ...)
332+
inspire_grid.character <- function(
333+
x,
334+
# Relevant arguments (passed to inspire_grid_from_ids)
335+
crs = NULL,
336+
point_type = "centroid",
337+
output_type = "sf_polygons",
338+
include_llc = TRUE,
339+
id_format = "both",
340+
axis_order = "NE",
341+
quiet = FALSE,
342+
dsn = NULL,
343+
layer = NULL,
344+
# Irrelevant arguments (sink these to prevent them from entering '...')
345+
cellsize_m = NULL,
346+
clip_to_input = FALSE,
347+
use_convex_hull = FALSE,
348+
buffer_m = 0,
349+
parallel = "auto",
350+
max_memory_gb = NULL,
351+
# Everything else (e.g., backend options for st_write)
352+
...
353+
) {
354+
# Warn if user provided irrelevant arguments
355+
if (!is.null(cellsize_m) || isTRUE(clip_to_input) ||
356+
isTRUE(use_convex_hull) || buffer_m != 0 || parallel != "auto" ||
357+
!is.null(max_memory_gb)) {
358+
warning(
359+
"Arguments 'cellsize_m', 'clip_to_input', 'use_convex_hull', ",
360+
"'buffer_m', 'parallel', and 'max_memory_gb' are ignored for INSPIRE ID reconstruction.",
361+
call. = FALSE
362+
)
363+
}
364+
365+
inspire_grid_from_ids(
366+
ids = x,
367+
crs = crs,
368+
point_type = point_type,
369+
output_type = output_type,
370+
include_llc = include_llc,
371+
id_format = id_format,
372+
axis_order = axis_order,
373+
quiet = quiet,
374+
dsn = dsn,
375+
layer = layer,
376+
...
377+
)
378+
}
156379

157380
#' @rdname inspire_grid
158381
#' @export
159382
#'
383+
#' @param grid_extent The spatial object defining the extent. Can be an \code{sf} object,
384+
#' \code{sfc} geometry collection, \code{bbox}, \code{numeric} vector (as c(xmin, ymin, xmax, ymax)),
385+
#' or \code{matrix}.
386+
#'
387+
#' @details
388+
#' This function creates a spatial grid aligned to the CRS origin, with support for
389+
#' clipping to input geometries, parallel processing, and multiple output formats.
390+
#'
160391
#' @examples
161392
#' library(sf)
162393
#' # Load the sample data from the sf package

0 commit comments

Comments
 (0)