Skip to content

Commit f95b7ab

Browse files
committed
reverse axis order for short codes in generated grids
1 parent d2d255a commit f95b7ab

4 files changed

Lines changed: 92 additions & 2 deletions

File tree

R/backend_sequential.R

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ create_grid_internal <- function(
1010
use_convex_hull = FALSE,
1111
buffer_m = 0,
1212
id_format = c("both", "long", "short", "none"),
13+
axis_order = c("NE", "EN"),
1314
include_llc = TRUE,
1415
point_type = c("centroid", "llc")
1516
) {
1617
# --- 1. PRE-CHECKS AND HELPERS ---
1718
output_type <- match.arg(output_type)
1819
id_format <- match.arg(id_format)
20+
axis_order <- match.arg(axis_order)
1921
point_type <- match.arg(point_type)
2022

2123
if (!requireNamespace("sf", quietly = TRUE)) {
@@ -40,12 +42,21 @@ create_grid_internal <- function(
4042
}
4143
n
4244
}
43-
make_ids <- function(x_llc, y_llc, cs, epsg = 3035) {
45+
make_ids <- function(x_llc, y_llc, cs, axis_order, epsg = 3035) {
4446
nzeros <- tz_count(cs)
4547
div <- as.integer(10^nzeros)
4648
size_lbl <- if (cs >= 1000) paste0(cs / 1000, "km") else paste0(cs, "m")
49+
50+
# Long IDs are strictly N...E according to INSPIRE spec, so we don't change this.
4751
id_long <- sprintf("CRS%sRES%smN%.0fE%.0f", epsg, cs, y_llc, x_llc)
48-
id_short <- sprintf("%sN%.0fE%.0f", size_lbl, y_llc / div, x_llc / div)
52+
53+
# Short IDs support swapping order
54+
id_short <- if (axis_order == "NE") {
55+
sprintf("%sN%.0fE%.0f", size_lbl, y_llc / div, x_llc / div)
56+
} else {
57+
sprintf("%sE%.0fN%.0f", size_lbl, x_llc / div, y_llc / div)
58+
}
59+
4960
list(long = id_long, short = id_short)
5061
}
5162

@@ -197,6 +208,7 @@ create_grid_internal <- function(
197208
out_obj$X_LLC,
198209
out_obj$Y_LLC,
199210
cellsize_m,
211+
axis_order = axis_order,
200212
epsg = grid_crs$epsg %||% 3035
201213
)
202214
if (id_format == "long") {

R/create_grid.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
#' clipping. Defaults to `0` (no buffer).
3636
#' @param id_format A character string specifying which grid cell IDs to generate.
3737
#' Options are `"both"` (default), `"long"`, `"short"`, or `"none"`.
38+
#' @param axis_order A character string specifying the coordinate order for the
39+
#' output Short INSPIRE IDs. This parameter is **only used when `id_format` is
40+
#' `"short"` or `"both"`**. It can be one of:
41+
#' \itemize{
42+
#' \item `"NE"` (the default) to produce the format `{cellsize}N{y}E{x}`.
43+
#' \item `"EN"` to produce the format `{cellsize}E{x}N{y}` (e.g. this format is used in [Danish national grid](https://www.dst.dk/en/TilSalg/produkter/geodata/kvadratnet)).
44+
#' }
3845
#' @param include_llc A logical value. If `TRUE` (default), columns for the
3946
#' lower-left corner coordinates (`X_LLC`, `Y_LLC`) of each cell are included
4047
#' in the output.
@@ -108,6 +115,7 @@ create_grid <- function(
108115
use_convex_hull = FALSE,
109116
buffer_m = 0,
110117
id_format = "both",
118+
axis_order = "NE",
111119
include_llc = TRUE,
112120
point_type = "centroid",
113121
parallel = "auto",
@@ -176,6 +184,7 @@ create_grid <- function(
176184
use_convex_hull = use_convex_hull,
177185
buffer_m = buffer_m,
178186
id_format = id_format,
187+
axis_order = axis_order,
179188
include_llc = include_llc,
180189
point_type = point_type,
181190
...

man/create_grid.Rd

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-create_grid.R

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,63 @@ test_that("Memory warning is triggered with insufficient (fake) RAM", {
416416
# Ensure the option is unset and doesn't interfere with other tests
417417
expect_null(getOption("gridmaker.fake_ram"))
418418
})
419+
420+
test_that("create_grid respects axis_order argument for Short IDs", {
421+
# Use a simple bounding box for testing
422+
# 10km grid
423+
# LLC at 0,0
424+
simple_extent <- sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 20000, ymax = 10000), crs = 3035)
425+
cellsize <- 10000
426+
427+
# 1. Test Default (NE)
428+
grid_ne <- create_grid(
429+
simple_extent,
430+
cellsize_m = cellsize,
431+
id_format = "short",
432+
axis_order = "NE",
433+
quiet = TRUE
434+
)
435+
436+
# Expect format: 10kmN0E0 (Northing then Easting)
437+
# Check the first ID (LLC 0,0)
438+
expected_ne <- "10kmN0E0"
439+
expect_true(expected_ne %in% grid_ne$GRD_ID)
440+
441+
# 2. Test EN Order
442+
grid_en <- create_grid(
443+
simple_extent,
444+
cellsize_m = cellsize,
445+
id_format = "short",
446+
axis_order = "EN",
447+
quiet = TRUE
448+
)
449+
450+
# Expect format: 10kmE0N0 (Easting then Northing)
451+
expected_en <- "10kmE0N0"
452+
expect_true(expected_en %in% grid_en$GRD_ID)
453+
454+
# 3. Test that Long IDs are NOT affected (Must remain N...E for standard compliance)
455+
grid_both_en <- create_grid(
456+
simple_extent,
457+
cellsize_m = cellsize,
458+
id_format = "both",
459+
axis_order = "EN",
460+
quiet = TRUE
461+
)
462+
463+
# Short ID should be EN
464+
expect_true("10kmE0N0" %in% grid_both_en$GRD_ID_SHORT)
465+
466+
# Long ID should still be NE (CRS...RES...N...E...)
467+
# 0,0 is usually N0E0
468+
long_id <- grid_both_en$GRD_ID_LONG[grid_both_en$GRD_ID_SHORT == "10kmE0N0"]
469+
expect_true(grepl("N0E0", long_id))
470+
expect_false(grepl("E0N0$", long_id)) # Should not end in E...N...
471+
})
472+
473+
test_that("create_grid throws error for invalid axis_order", {
474+
expect_error(
475+
create_grid(nc, CELLSIZE, axis_order = "XY"),
476+
regexp = "'arg' should be one of"
477+
)
478+
})

0 commit comments

Comments
 (0)