From 31503440acd1eb70b9b0114386f71f00e60f4009 Mon Sep 17 00:00:00 2001 From: edzer Date: Thu, 21 Dec 2023 11:01:28 +0100 Subject: [PATCH] address #2298 --- NEWS.md | 8 +++++++- R/init.R | 52 ++++++++++++++++++++++++++++------------------------ 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/NEWS.md b/NEWS.md index 973cb336d..dfc274a66 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,12 @@ # version 1.0-16 -* add `st_line_project()` to find how far a point is when projected on a line, and `st_line_interpolate()` to obtain a point at a certain distance along a line; #2291 +* if environment variable `R_SF_USE_PROJ_DATA` is `true`, `GDAL_DATA`, `PROJ_DATA` (and deprecated `PROJ_LIB`) will not be ignored. + +* environment variables `PROJ_LIB` and `PROJ_DATA` are (again) ignored on `sf` binary CRAN installations (win + macos); #2298 + +* add `st_line_project()` to find how far a point is when projected on a line; #2291 + +* add `st_line_interpolate()` to obtain a point at a certain distance along a line; #2291 # version 1.0-15 diff --git a/R/init.R b/R/init.R index 3624a506f..d80ab61b6 100644 --- a/R/init.R +++ b/R/init.R @@ -72,19 +72,35 @@ sf_extSoftVersion = function() { names = c("GEOS", "GDAL", "proj.4", "GDAL_with_GEOS", "USE_PROJ_H", "PROJ")) } +if_exists_replace = function(var, new, where) { + if (Sys.getenv(var) != "") { + assign(paste0(".sf.", var), Sys.getenv(var), envir = where) + Sys.setenv(var = new) + } +} + +if_exists_restore = function(vars, where) { + fn = function(var, where) { + lname = paste0(".sf.", var) + if (!is.null(get0(lname, envir = where))) + Sys.setenv(var = get(lname, envir = where)) + } + lapply(vars, fn, where = where) +} + load_gdal <- function() { - if (file.exists(system.file("proj/nad.lst", package = "sf")[1])) { - # nocov start - prj = system.file("proj", package = "sf")[1] - if (! CPL_set_data_dir(prj)) { # if TRUE, uses C API to set path, leaving PROJ_LIB alone - assign(".sf.PROJ_LIB", Sys.getenv("PROJ_LIB"), envir=.sf_cache) - Sys.setenv("PROJ_LIB" = prj) + if (!identical(Sys.getenv("R_SF_USE_PROJ_DATA"), "true")) { + if (file.exists(prj <- system.file("proj/nad.lst", package = "sf")[1])) { + # nocov start + if (! CPL_set_data_dir(prj)) { # if TRUE, uses C API to set path, leaving PROJ_LIB / PROJ_DATA alone + if_exists_replace("PROJ_LIB", prj, .sf_cache) + if_exists_replace("PROJ_DATA", prj, .sf_cache) + } + CPL_use_proj4_init_rules(1L) + # nocov end } - CPL_use_proj4_init_rules(1L) - assign(".sf.GDAL_DATA", Sys.getenv("GDAL_DATA"), envir=.sf_cache) - gdl = system.file("gdal", package = "sf")[1] - Sys.setenv("GDAL_DATA" = gdl) - # nocov end + if (file.exists(gdl <- system.file("gdal", package = "sf")[1])) + if_exists_replace("GDAL_DATA", gdl, .sf_cache) } CPL_gdal_init() register_all_s3_methods() # dynamically registers non-imported pkgs (tidyverse) @@ -92,19 +108,7 @@ load_gdal <- function() { unload_gdal <- function() { CPL_gdal_cleanup_all() - if (file.exists(system.file("proj/nad.lst", package = "sf")[1])) { - # nocov start - if (! CPL_set_data_dir(system.file("proj", package = "sf")[1])) # set back: - Sys.setenv("PROJ_LIB"=get(".sf.PROJ_LIB", envir=.sf_cache)) - - Sys.setenv("GDAL_DATA"=get(".sf.GDAL_DATA", envir=.sf_cache)) - # nocov end - } - #units::remove_symbolic_unit("link") - #units::remove_symbolic_unit("us_in") - #units::remove_symbolic_unit("ind_yd") - #units::remove_symbolic_unit("ind_ft") - #units::remove_symbolic_unit("ind_ch") + if_exists_restore(c("PROJ_LIB", "PROJ_DATA", "GDAL_DATA"), .sf_cache) }