-
Couldn't load subscription status.
- Fork 1
add derive_grid() to generate grids from INSPIRE IDs (#3) #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JsLth
wants to merge
26
commits into
e-kotov:main
Choose a base branch
from
JsLth:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
b746110
add derive_grid() to generate grids from INSPIRE IDs (#3)
JsLth 15705e9
add regex_match() utility
JsLth e0c5b0e
add contributor
JsLth 62a2e65
remove trailing zeroes only in short form
JsLth aaa8ce8
prevent scientific notation in large INSPIRE-compliant cellsizes
JsLth 5c3296e
adjust inspire_extract() to fixed northing/easting generation in insp…
JsLth 9492c17
fix examples of inspire_ functions
JsLth 2ba1340
fix Authors@R in DESCRIPTION file
JsLth b74b513
fix a typo in as_grid_coordinates()
JsLth 6f86659
legacy -> short
JsLth c563a27
format eastings, northings and resolutions as integers when generatin…
JsLth c8834dc
let guess_resolution() handle non-standard grid sizes and increase ar…
JsLth 78b5d57
res -> cellsize_m for consistency
JsLth 9ef5027
add llc argument to inspire_generate() to control whether LLCs or cen…
JsLth c3c71c7
signal error if inspire_extract() parses more than one CRS
JsLth e167b60
fix typo error in inspire_extract()
JsLth 60a1fca
add inspire_convert
e-kotov 88bb95e
rewrite inspire_extract with additonal features and edge cases, also …
e-kotov 627ab65
add updated docs and NAMESPACE
e-kotov 372e3d3
update inspire_generate and add tests
e-kotov 6c01f16
fix derive grid seqeuntial
e-kotov d7a0b66
fix future backend ::: warning
e-kotov 3467884
fix mirai ::: warning
e-kotov fbe00f0
more robust inspire_convert
e-kotov 31c6a75
add support of reverse axis for short codes in inspire_generate
e-kotov 31776a3
add tests for inspire_derive
e-kotov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| # Generated by roxygen2: do not edit by hand | ||
|
|
||
| export(create_grid) | ||
| export(derive_grid) | ||
| export(inspire_convert) | ||
| export(inspire_extract) | ||
| export(inspire_generate) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| as_grid <- function( | ||
| coords, | ||
| cellsize, | ||
| crs, | ||
| output_type = "sf_polygons", | ||
| point_type = "centroid", | ||
| clipping_target = NULL | ||
| ) { | ||
| out_obj <- switch( | ||
| output_type, | ||
| sf_polygons = as_grid_polygons( | ||
| coords, | ||
| cellsize = cellsize, | ||
| crs = crs, | ||
| clipping_target = clipping_target | ||
| ), | ||
| sf_points = as_grid_points( | ||
| coords, | ||
| cellsize = cellsize, | ||
| crs = crs, | ||
| point_type = point_type | ||
| ), | ||
| dataframe = as_grid_coordinates(coords, cellsize = cellsize) | ||
| ) | ||
|
|
||
| if (!is.null(clipping_target) && !identical(output_type, "sf_polygons")) { | ||
| # For filtering, we always use centroids as the representative point | ||
| points_for_filter <- sf::st_as_sf( | ||
| data.frame( | ||
| x = coords$X_LLC + (cellsize / 2), | ||
| y = coords$Y_LLC + (cellsize / 2) | ||
| ), | ||
| coords = c("x", "y"), | ||
| crs = crs | ||
| ) | ||
| keep_indices <- sf::st_intersects( | ||
| points_for_filter, | ||
| clipping_target, | ||
| sparse = FALSE | ||
| ) | ||
| out_obj <- out_obj[keep_indices[, 1], ] | ||
| } | ||
|
|
||
| out_obj | ||
| } | ||
|
|
||
|
|
||
|
|
||
| as_grid_polygons <- function(coords, cellsize, crs, clipping_target = NULL) { | ||
| n_polygons <- nrow(coords) | ||
| x_llc_rep <- rep(coords$X_LLC, each = 5) | ||
| y_llc_rep <- rep(coords$Y_LLC, each = 5) | ||
| x_coords_poly <- x_llc_rep + c(0, cellsize, cellsize, 0, 0) | ||
| y_coords_poly <- y_llc_rep + c(0, 0, cellsize, cellsize, 0) | ||
|
|
||
| df_vertices <- data.frame( | ||
| id = rep(seq_len(n_polygons), each = 5), | ||
| x = x_coords_poly, | ||
| y = y_coords_poly | ||
| ) | ||
| grid_geoms <- sfheaders::sf_polygon( | ||
| obj = df_vertices, | ||
| x = "x", | ||
| y = "y", | ||
| polygon_id = "id" | ||
| ) | ||
| grid_sf <- sf::st_sf(geometry = grid_geoms, crs = crs) | ||
| grid_sf$X_LLC <- coords$X_LLC | ||
| grid_sf$Y_LLC <- coords$Y_LLC | ||
|
|
||
| if (!is.null(clipping_target)) { | ||
| intersects_list <- sf::st_intersects(grid_sf, clipping_target) | ||
| keep_indices <- lengths(intersects_list) > 0 | ||
| grid_sf <- grid_sf[keep_indices, ] | ||
| } | ||
|
|
||
| grid_sf | ||
| } | ||
|
|
||
|
|
||
| as_grid_points <- function( | ||
| coords, | ||
| cellsize, | ||
| crs, | ||
| point_type = "centroid" | ||
| ) { | ||
| coords_to_use <- if (point_type == "centroid") { | ||
| list( | ||
| x = coords$X_LLC + (cellsize / 2), | ||
| y = coords$Y_LLC + (cellsize / 2), | ||
| names = c("X_centroid", "Y_centroid") | ||
| ) | ||
| } else { | ||
| # llc | ||
| list(x = coords$X_LLC, y = coords$Y_LLC, names = c("X_LLC", "Y_LLC")) | ||
| } | ||
| points_df <- data.frame(x = coords_to_use$x, y = coords_to_use$y) | ||
| out_obj <- sf::st_as_sf(points_df, coords = c("x", "y"), crs = crs) | ||
| # Re-attach all original attributes for consistency | ||
| cbind(out_obj, coords) | ||
| } | ||
|
|
||
|
|
||
| as_grid_coordinates <- function(coords, cellsize) { | ||
| coords$X_centroid <- coords$X_LLC + (cellsize / 2) | ||
| coords$Y_centroid <- coords$Y_LLC + (cellsize / 2) | ||
| coords | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #' Convert INSPIRE IDs into a spatial grid | ||
| #' | ||
| #' @description{ | ||
| #' 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 \code{\link{create_grid}}.} | ||
| #' @param ids A vector containing character strings of INSPIRE-compliant IDs. | ||
| #' Can be either short or long INSPIRE IDs. | ||
| #' @param point_type A character string, used only when `output_type = "sf_points"`. | ||
| #' Determines the location of the points: `"centroid"` for the center | ||
| #' of the cell, or `"llc"` (default) for the lower-left corner. | ||
| #' | ||
| #' @inherit create_grid | ||
| #' | ||
| #' @export | ||
| #' | ||
| #' @examples | ||
| #' library(sf) | ||
| #' | ||
| #' inspire <- c( | ||
| #' "CRS3035RES100000mN26E43", "CRS3035RES100000mN26E44", | ||
| #' "CRS3035RES100000mN27E41", "CRS3035RES100000mN27E42", | ||
| #' "CRS3035RES100000mN27E43", "CRS3035RES100000mN27E44" | ||
| #' ) | ||
| #' | ||
| #' grid <- derive_grid(inspire) | ||
| #' plot(grid$geometry) | ||
| derive_grid <- function( | ||
e-kotov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ids, | ||
| point_type = c("llc", "centroid"), | ||
| output_type = c("sf_polygons", "sf_points", "dataframe"), | ||
| include_llc = TRUE, | ||
| parallel = FALSE, | ||
| quiet = FALSE | ||
| ) { | ||
| if (!isFALSE(parallel)) { | ||
| warning("Parallel processing is not yet supported for `derive_grid()`.") | ||
| } | ||
|
|
||
| if (!is.logical(quiet) || length(quiet) != 1) { | ||
| stop( | ||
| "'quiet' must be a single logical value (TRUE or FALSE).", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
|
|
||
| backend_args <- list(point_type = point_type, output_type = output_type) | ||
| do.call(derive_grid_internal, c(list(ids = ids), backend_args)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| derive_grid_internal <- function( | ||
| ids, | ||
| point_type = c("llc", "centroid"), | ||
| output_type = c("sf_polygons", "sf_points", "dataframe"), | ||
| quiet = FALSE | ||
| ) { | ||
| output_type <- match.arg(output_type) | ||
| point_type <- match.arg(point_type) | ||
|
|
||
| if (!requireNamespace("sf", quietly = TRUE)) { | ||
| stop("The 'sf' package is required. Please install it.", call. = FALSE) | ||
| } | ||
|
|
||
| if ( | ||
| output_type == "sf_polygons" && | ||
| !requireNamespace("sfheaders", quietly = TRUE) | ||
| ) { | ||
| stop( | ||
| "Package 'sfheaders' is required for 'sf' output. Please install it.", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
|
|
||
| grid_df <- inspire_extract(ids, as_sf = FALSE) | ||
| names(grid_df) <- c("crs", "cellsize", "Y_LLC", "X_LLC") | ||
|
|
||
| if (length(unique(grid_df$crs)) > 1) { | ||
| stop( | ||
| "Invalid CRS: Multiple coordinate reference systems found. Please ensure that all INSPIRE IDs have the same CRS.", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
|
|
||
| if (length(unique(grid_df$cellsize)) > 1) { | ||
| stop( | ||
| "Invalid cell size: Multiple different cell sizes found. Please ensure that all INSPIRE IDs refer to the same cell size.", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
|
|
||
| grid_crs <- sf::st_crs(grid_df$crs[[1]]) | ||
| if (is.na(grid_crs) || sf::st_is_longlat(grid_crs)) { | ||
| stop( | ||
| "Invalid CRS: The coordinate reference system must be a projected system (e.g., EPSG:3035) and not a geographic one (like WGS84, EPSG:4326).", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
|
|
||
| out_obj <- as_grid( | ||
| grid_df, | ||
| cellsize = grid_df$cellsize[[1]], | ||
| crs = grid_crs, | ||
| output_type = output_type, | ||
| point_type = point_type | ||
| ) | ||
|
|
||
| out_obj$id <- ids | ||
| out_obj | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.