Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
94 changes: 69 additions & 25 deletions R/jsonld_to_mustache.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,76 @@
#' Parse spatialCoverage section for use in a Leaflet map
#'
#' Also returns table rows for displaying spatial coverage properties.
#'
#' @param spatialCoverage (list) spatialCoverage section of the JSONLD
#'
#' @return (list) Template-specific variables for Leaflet
#' @return (list) Template-specific variables for Leaflet and spatial coverage text
parse_spatialCoverage <- function(spatialCoverage) {
json <- list(showmap = "geo" %in% names(spatialCoverage) &&
"type" %in% names(spatialCoverage$geo),
isgeoshape = ifelse(
spatialCoverage$geo$type == "GeoShape",
TRUE,
FALSE),
isgeopoints = ifelse(
spatialCoverage$geo$type == "GeoPoints",
TRUE,
FALSE))

if (json$isgeoshape) {
json <- c(json, parse_GeoShape_box(spatialCoverage$geo$box))
} else if (json$isgeopoints) {
json <- c(json, parse_GeoShape_box(spatialCoverage$geo$points))

} else {
stop("Specified spatialCoverage not supported.", call. = FALSE)
# Keep the map available for every dataset; unsupported shapes have no
# drawable polygon, but their coverage properties are still rendered.
result <- list(spatialCoverageRows = spatial_coverage_rows(spatialCoverage),
mapcenterstring = "[0, 0]",
mappolystring = "[]")

# The current map implementation understands only Schema.org GeoShape boxes.
if (is.list(spatialCoverage) && !is.null(spatialCoverage$geo) &&
is.list(spatialCoverage$geo) &&
identical(spatialCoverage$geo$type %||% spatialCoverage$geo$`@type`, "GeoShape") &&
is.character(spatialCoverage$geo$box) &&
length(spatialCoverage$geo$box) == 1L) {
coordinates <- suppressWarnings(as.numeric(
stringr::str_split(stringr::str_trim(spatialCoverage$geo$box), "[[:space:]]+")[[1]]))
if (length(coordinates) == 4L && all(is.finite(coordinates))) {
map <- parse_GeoShape_box(spatialCoverage$geo$box)
result$mapcenterstring <- map$mapcenterstring
result$mappolystring <- map$mappolystring
}
}

json
result
}

`%||%` <- function(x, y) if (is.null(x)) y else x

# Flatten nested JSON-LD properties into table rows while retaining their path.
spatial_coverage_rows <- function(x, prefix = "") {
if (is.null(x) || !length(x)) {
if (nzchar(prefix)) return(list())
return(list(list(label = "Coverage", value = "No spatial coverage specified.")))
}

if (is.atomic(x)) {
value <- paste(as.character(x), collapse = ", ")
return(list(list(label = if (nzchar(prefix)) prefix else "Coverage",
value = value)))
}

if (!is.list(x)) {
return(list(list(label = if (nzchar(prefix)) prefix else "Coverage",
value = as.character(x))))
}

if (is.null(names(x))) {
rows <- list()
for (i in seq_along(x)) {
item_prefix <- if (nzchar(prefix)) paste0(prefix, " (", i, ")") else
paste("Coverage", i)
rows <- c(rows, spatial_coverage_rows(x[[i]], item_prefix))
}
return(rows)
}

rows <- list()
for (field in names(x)) {
value <- x[[field]]
if (is.null(value)) next
label <- gsub("([a-z])([A-Z])", "\\1 \\2", field)
label <- paste0(toupper(substr(label, 1, 1)), substr(label, 2, nchar(label)))
path <- if (nzchar(prefix)) paste(prefix, label, sep = " / ") else label
rows <- c(rows, spatial_coverage_rows(value, path))
}
if (length(rows)) rows else
list(list(label = "Coverage", value = "Spatial coverage information is provided."))
}

#' Parse spatialCoverage$geo$box section for use in a Leaflet map
Expand All @@ -33,17 +79,15 @@ parse_spatialCoverage <- function(spatialCoverage) {
#'
#' @return (list) Template-specific variables for Leaflet
parse_GeoShape_box <- function(box) {
tokens <- stringr::str_split(box, " ")

if (!length(tokens) == 1) {
tokens <- stringr::str_split(stringr::str_trim(box), "[[:space:]]+")[[1]]
tokens <- suppressWarnings(as.numeric(tokens))
if (length(tokens) != 4L || any(!is.finite(tokens))) {
stop(
"Failed to parse box in spatialCoverage$geo$box of '",
box, "'.",
call. = FALSE)
}

tokens <- vapply(tokens[[1]], as.numeric, 0.0, USE.NAMES = FALSE)

list(
mapcenterstring =
paste0(
Expand Down
4 changes: 4 additions & 0 deletions experiments/static_maps/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
^\.Rprofile$
^renv$
^renv\.lock$
^reports$
1 change: 1 addition & 0 deletions experiments/static_maps/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
24 changes: 24 additions & 0 deletions experiments/static_maps/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Package: staticmaps
Title: Static Map Projection Experiment
Version: 0.0.0.9000
Authors@R: person("Bryce", "Mecum", email = "petridish@gmail.com", role = c("aut", "cre"))
Description: Prototypes and comparison tools for generating self-contained static
maps from geographic bounding boxes, with projection selection delegated to
sf and PROJ.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Imports:
base64enc,
ggplot2,
sf
Suggests:
pak,
crsuggest,
htmltools,
maps,
projWiz,
testthat (>= 3.0.0)
Config/testthat/edition: 3
Remotes:
sToney239/projWiz
2 changes: 2 additions & 0 deletions experiments/static_maps/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2026
COPYRIGHT HOLDER: Bryce Mecum
3 changes: 3 additions & 0 deletions experiments/static_maps/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export(generate_projection_comparison)
export(project_xy)
export(static_map_ggplot)
Loading
Loading