| Title: | Create INSPIRE-Compliant Grids with IDs |
|---|---|
| Description: | Creates GISCO compatible and INSPIRE-compliant grids with IDs that look like 'CRS3035RES1000mN3497000E4448000' or '1kmN3497E4447'. Input can be 'sf', 'sfc' objects or bounding boxes. Output can be 'sf' polygons, 'sf' centroids, or just 'data.frame' with grid cell center or bottom left corner coordinates. The resulting grids are always aligned to rounded coordinates as per INSPIRE requirements. |
| Authors: | Egor Kotov [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-6690-5345>), Jonas Lieth [ctb] (ORCID: <https://orcid.org/0000-0002-3451-3176>) |
| Maintainer: | Egor Kotov <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-16 08:03:56 UTC |
| Source: | https://github.com/e-kotov/gridmaker |
Generates a standard-compliant spatial grid aligned to the CRS origin. This function acts as a unified interface:
If x is a spatial object (extent), it creates a new grid (calling inspire_grid_from_extent).
If x is a character vector (INSPIRE IDs), it reconstructs the grid (calling inspire_grid_from_ids).
It combines high performance for large areas (using sfheaders) with a
flexible and robust set of features for input handling and output formatting,
including INSPIRE-compliant grid IDs and automatic parallel processing with
mirai and future backends.
This function takes a vector of INSPIRE-compliant IDs and derives a
regular spatial grid from it. For generating a spatial grid from a spatial
extent, see inspire_grid_from_extent.
inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'sf' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'sfc' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'bbox' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'numeric' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'matrix' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'character' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "llc", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) inspire_grid_from_extent( grid_extent, cellsize_m, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) inspire_grid_from_ids( ids, point_type = c("llc", "centroid"), output_type = c("sf_polygons", "sf_points", "dataframe"), include_llc = TRUE, id_format = c("both", "long", "short"), axis_order = c("NE", "EN"), quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, ... )inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'sf' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'sfc' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'bbox' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'numeric' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'matrix' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) ## S3 method for class 'character' inspire_grid( x, cellsize_m = NULL, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "llc", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) inspire_grid_from_extent( grid_extent, cellsize_m, crs = NULL, output_type = "sf_polygons", clip_to_input = FALSE, use_convex_hull = FALSE, buffer_m = 0, id_format = "both", axis_order = "NE", include_llc = TRUE, point_type = "centroid", parallel = "auto", quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, max_memory_gb = NULL, include_rat = FALSE, ... ) inspire_grid_from_ids( ids, point_type = c("llc", "centroid"), output_type = c("sf_polygons", "sf_points", "dataframe"), include_llc = TRUE, id_format = c("both", "long", "short"), axis_order = c("NE", "EN"), quiet = getOption("gridmaker.quiet", FALSE), dsn = NULL, layer = NULL, ... )
x |
The main input object: either a spatial object (extent) or a character vector (INSPIRE IDs). |
cellsize_m |
A single integer representing the grid cell size in metres (e.g., 1000 for a 1 km grid). Required for spatial inputs. |
crs |
The coordinate reference system (CRS) for the output grid.
Accepts various formats handled by |
output_type |
The class of the output object: |
clip_to_input |
A logical value. If |
use_convex_hull |
A logical value. If |
buffer_m |
A numeric value. If |
id_format |
A character string specifying which grid cell IDs to generate.
Options are |
axis_order |
A character string specifying the coordinate order for the
output Short INSPIRE IDs. This parameter is only used when
|
include_llc |
A logical value. If |
point_type |
A character string determining the location of the points
when |
parallel |
Controls parallel execution. Options are:
For parallelism, you must configure a backend before calling this
function, for example:
|
quiet |
Logical value. If |
dsn |
The destination for the output grid. For sf objects, this is passed to
Supported vector formats for chunked disk writes:
Other formats not listed have not been tested and will generate a warning. |
layer |
The name of the grid layer, passed directly to |
max_memory_gb |
A numeric value. Maximum memory in gigabytes to use for grid creation. Default is |
include_rat |
Logical. If What is a RAT? A Raster Attribute Table stores metadata (like INSPIRE IDs) for each unique raster value. Without RAT, raster cells contain only numeric IDs (1, 2, 3...). With RAT, software like QGIS/R can display the IDs as human-readable labels. Format-specific behavior:
|
... |
Additional arguments passed to backend handlers.
When writing to text files (e.g., .csv, .tsv) via |
grid_extent |
The spatial object defining the extent. Can be an |
ids |
A character vector of INSPIRE-compliant grid cell IDs
(e.g., |
This function creates a spatial grid aligned to the CRS origin, with support for clipping to input geometries, parallel processing, and multiple output formats.
If dsn is NULL (the default), an sf object, data.frame,
or SpatRaster representing the grid. If dsn is specified, the function writes
the grid to a file and returns invisible(dsn).
An sf object or data.frame representing the grid derived from
the INSPIRE IDs. If dsn is specified, returns invisible(dsn).
library(sf) # Load the sample data from the sf package nc_raw <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) # Define target projected CRS and cell size target_crs <- 5070 # NAD83 / Conus Albers cellsize_m <- 10000 # 10 km # Project the data nc <- st_transform(nc_raw, target_crs) # Create a grid covering the data nc_grid <- inspire_grid_from_extent( grid_extent = nc, cellsize_m = cellsize_m, output_type = "sf_polygons", clip_to_input = TRUE ) # Or using the S3 generic nc_grid <- inspire_grid(nc, cellsize_m = cellsize_m, clip_to_input = TRUE) head(nc_grid, 3) library(sf) inspire <- c( "CRS3035RES100000mN26E43", "CRS3035RES100000mN26E44", "CRS3035RES100000mN27E41", "CRS3035RES100000mN27E42", "CRS3035RES100000mN27E43", "CRS3035RES100000mN27E44" ) grid <- inspire_grid_from_ids(inspire) plot(grid$geometry) # Or using the S3 generic grid <- inspire_grid(inspire) plot(grid$geometry)library(sf) # Load the sample data from the sf package nc_raw <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) # Define target projected CRS and cell size target_crs <- 5070 # NAD83 / Conus Albers cellsize_m <- 10000 # 10 km # Project the data nc <- st_transform(nc_raw, target_crs) # Create a grid covering the data nc_grid <- inspire_grid_from_extent( grid_extent = nc, cellsize_m = cellsize_m, output_type = "sf_polygons", clip_to_input = TRUE ) # Or using the S3 generic nc_grid <- inspire_grid(nc, cellsize_m = cellsize_m, clip_to_input = TRUE) head(nc_grid, 3) library(sf) inspire <- c( "CRS3035RES100000mN26E43", "CRS3035RES100000mN26E44", "CRS3035RES100000mN27E41", "CRS3035RES100000mN27E42", "CRS3035RES100000mN27E43", "CRS3035RES100000mN27E44" ) grid <- inspire_grid_from_ids(inspire) plot(grid$geometry) # Or using the S3 generic grid <- inspire_grid(inspire) plot(grid$geometry)
Given pairs of coordinates, generates their INSPIRE grid representation. Given INSPIRE identifiers, can also extract the X and Y coordinates.
An INSPIRE ID contains information about the CRS, cell size and the ETRS89-LAEA coordinates of the lower-left corner of the grid cell in its format.
CRS3035{cellsize}mN{y}E{x} # long format
{cellsize}N{y}E{x} # short format (NE order)
{cellsize}E{x}N{y} # short format (EN order)
The long format always uses meters while the short format aggregates cell sizes greater or equal to 1000m to km.
inspire_id_from_coords( coords, cellsize_m = NULL, short = FALSE, axis_order = "NE", llc = FALSE, tolerance = 1e-06, sample = 2000 ) inspire_id_to_coords(inspire, as_sf = FALSE, crs = NULL)inspire_id_from_coords( coords, cellsize_m = NULL, short = FALSE, axis_order = "NE", llc = FALSE, tolerance = 1e-06, sample = 2000 ) inspire_id_to_coords(inspire, as_sf = FALSE, crs = NULL)
coords |
A list, matrix, or dataframe where the X and Y coordinates are
either in the columns Can also be a |
cellsize_m |
A single integer representing the grid cell size in metres (e.g., 1000 for a 1 km grid). Required for spatial inputs. |
short |
If |
axis_order |
A character string specifying the coordinate order for the
output. This parameter is only used when
|
llc |
Do the coordinates in |
tolerance |
If |
sample |
If |
inspire |
A vector of INSPIRE IDs. Can be either legacy or non-legacy. |
as_sf |
Whether to return an object of class |
crs |
An optional numeric EPSG code or |
inspire_id_from_coords returns a character vector containing
the INSPIRE identifiers.
inspire_id_to_coords returns a dataframe or
sf dataframe (if as_sf = TRUE) containing
the points extracted from the INSPIRE identifiers and information about
the CRS and cell sizes. Note that the returned coordinates are always
the centers of the grid cells as opposed to the lower-left corners.
# Generate IDs from a dataframe coords <- data.frame(x = c(4334100, 4334200), y = 2684000) gen <- inspire_id_from_coords(coords, llc = TRUE, cellsize_m = 100) ext <- inspire_id_to_coords(gen)[c("x", "y")] # Note: inspire_id_to_coords gives cell centers, so this won't be identical if llc=TRUE # Generate long format IDs inspire_id_from_coords(coords, llc = TRUE, cellsize_m = 100) # Generate short format IDs with default "NE" axis order inspire_id_from_coords(coords, llc = TRUE, cellsize_m = 1000, short = TRUE) # Generate short format IDs with "EN" axis order inspire_id_from_coords(coords, llc = TRUE, cellsize_m = 1000, short = TRUE, axis_order = "EN") # Extract coordinates from short ID strings inspire_id_to_coords("100mN34000E44000", crs = 3035) # Generate IDs from an sf dataframe if (requireNamespace("sf", quietly = TRUE)) { coords_df <- data.frame(x = c(4334100, 4334200), y = 2684000) coords_sf <- sf::st_as_sf(coords_df, coords = c("x", "y"), crs = 3035) inspire_id_from_coords(coords_sf, cellsize_m = 1000) }# Generate IDs from a dataframe coords <- data.frame(x = c(4334100, 4334200), y = 2684000) gen <- inspire_id_from_coords(coords, llc = TRUE, cellsize_m = 100) ext <- inspire_id_to_coords(gen)[c("x", "y")] # Note: inspire_id_to_coords gives cell centers, so this won't be identical if llc=TRUE # Generate long format IDs inspire_id_from_coords(coords, llc = TRUE, cellsize_m = 100) # Generate short format IDs with default "NE" axis order inspire_id_from_coords(coords, llc = TRUE, cellsize_m = 1000, short = TRUE) # Generate short format IDs with "EN" axis order inspire_id_from_coords(coords, llc = TRUE, cellsize_m = 1000, short = TRUE, axis_order = "EN") # Extract coordinates from short ID strings inspire_id_to_coords("100mN34000E44000", crs = 3035) # Generate IDs from an sf dataframe if (requireNamespace("sf", quietly = TRUE)) { coords_df <- data.frame(x = c(4334100, 4334200), y = 2684000) coords_sf <- sf::st_as_sf(coords_df, coords = c("x", "y"), crs = 3035) inspire_id_from_coords(coords_sf, cellsize_m = 1000) }
This function converts a vector of INSPIRE-compliant grid IDs from their long format to the short format, or vice-versa. It automatically detects the input format and is fully vectorized to handle large inputs efficiently.
The long format is CRS{epsg}RES{cellsize}mN{y}E{x}. The short format can be
either {cellsize}N{y}E{x} or {cellsize}E{x}N{y}.
inspire_id_format(ids, crs = 3035, axis_order = "NE")inspire_id_format(ids, crs = 3035, axis_order = "NE")
ids |
A character vector of INSPIRE IDs. All IDs in the vector must be of the same format (either all long or all short). |
crs |
An integer representing the EPSG code. This parameter is only
used when converting from the short format to the long format. It
defaults to |
axis_order |
A character string specifying the coordinate order for the output. This parameter is only used when converting from the long format to the short format. It can be one of:
|
A character vector of the converted INSPIRE IDs.
long_ids <- c("CRS3035RES1000mN2684000E4334000", "CRS3035RES10000mN2700000E4400000") short_ids_ne <- c("1kmN2684E4334", "10kmN270E440") short_ids_en <- c("1kmE4334N2684", "10kmE440N270") # --- Long to Short --- # Convert long to short with default "NE" order inspire_id_format(long_ids) # Convert long to short with specified "EN" order inspire_id_format(long_ids, axis_order = "EN") # --- Short to Long --- # Convert short ("NE" format) to long with default CRS (3035) inspire_id_format(short_ids_ne) # The function also correctly parses the "EN" format inspire_id_format(short_ids_en) # Override the CRS when converting short to long inspire_id_format(short_ids_ne, crs = 3857)long_ids <- c("CRS3035RES1000mN2684000E4334000", "CRS3035RES10000mN2700000E4400000") short_ids_ne <- c("1kmN2684E4334", "10kmN270E440") short_ids_en <- c("1kmE4334N2684", "10kmE440N270") # --- Long to Short --- # Convert long to short with default "NE" order inspire_id_format(long_ids) # Convert long to short with specified "EN" order inspire_id_format(long_ids, axis_order = "EN") # --- Short to Long --- # Convert short ("NE" format) to long with default CRS (3035) inspire_id_format(short_ids_ne) # The function also correctly parses the "EN" format inspire_id_format(short_ids_en) # Override the CRS when converting short to long inspire_id_format(short_ids_ne, crs = 3857)