Skip to content

Commit 2a89741

Browse files
committed
fixes #2313
1 parent a893a84 commit 2a89741

File tree

9 files changed

+68
-52
lines changed

9 files changed

+68
-52
lines changed

R/RcppExports.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ CPL_get_pipelines <- function(crs, authority, AOI, Use, grid_availability, accur
301301
.Call(`_sf_CPL_get_pipelines`, crs, authority, AOI, Use, grid_availability, accuracy, strict_containment, axis_order_auth_compl)
302302
}
303303

304-
CPL_get_data_dir <- function(b = FALSE) {
305-
.Call(`_sf_CPL_get_data_dir`, b)
304+
CPL_get_data_dir <- function(from_proj = FALSE) {
305+
.Call(`_sf_CPL_get_data_dir`, from_proj)
306306
}
307307

308308
CPL_is_network_enabled <- function(b = FALSE) {
@@ -313,8 +313,8 @@ CPL_enable_network <- function(url, enable = TRUE) {
313313
.Call(`_sf_CPL_enable_network`, url, enable)
314314
}
315315

316-
CPL_set_data_dir <- function(data_dir) {
317-
.Call(`_sf_CPL_set_data_dir`, data_dir)
316+
CPL_set_data_dir <- function(data_dir, with_proj) {
317+
.Call(`_sf_CPL_set_data_dir`, data_dir, with_proj)
318318
}
319319

320320
CPL_use_proj4_init_rules <- function(v) {

R/geom-measures.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ st_perimeter = function(x, ...) {
128128
if (!requireNamespace("lwgeom", quietly = TRUE))
129129
stop("package lwgeom required, please install it first")
130130
# note that units are handled appropriately by lwgeom
131-
lwgeom::st_perimeter(x)
131+
lwgeom::st_perimeter_lwgeom(x)
132132
}
133133
}
134134

R/init.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ load_gdal <- function() {
9292
if (!identical(Sys.getenv("R_SF_USE_PROJ_DATA"), "true")) {
9393
if (file.exists(prj <- system.file("proj", package = "sf")[1])) {
9494
# nocov start
95-
if (! CPL_set_data_dir(prj)) { # if TRUE, uses C API to set path, leaving PROJ_LIB / PROJ_DATA alone
95+
if (! sf_proj_search_paths(prj)) { # if TRUE, uses C API to set path, leaving PROJ_LIB / PROJ_DATA alone
9696
save_and_replace("PROJ_LIB", prj, .sf_cache)
9797
save_and_replace("PROJ_DATA", prj, .sf_cache)
9898
}

R/proj.R

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' @param type character; one of `have_datum_files`, `proj`, `ellps`, `datum`, `units`, `path`, or `prime_meridians`; see Details.
44
#' @param path character; PROJ search path to be set
55
#' @export
6-
#' @details \code{sf_proj_info} lists the available projections, ellipses, datums, units, or data search path of the PROJ library when \code{type} is equal to proj, ellps, datum, units or path; when \code{type} equals \code{have_datum_files} a boolean is returned indicating whether datum files are installed and accessible (checking for \code{conus}).
6+
#' @details \code{sf_proj_info} lists the available projections, ellipses, datums, units, or data search path of the PROJ library when \code{type} is equal to proj, ellps, datum, units or path; when \code{type} equals \code{have_datum_files} a boolean is returned indicating whether datum files are installed and accessible (checking for \code{conus}). `path` returns the `PROJ_INFO.searchpath` field directly, as a single string with path separaters (`:` or `;`).
77
#'
88
#' for PROJ >= 6, \code{sf_proj_info} does not provide option \code{type = "datums"}.
99
#' PROJ < 6 does not provide the option \code{type = "prime_meridians"}.
@@ -18,10 +18,10 @@ sf_proj_info = function(type = "proj", path) {
1818
return(CPL_have_datum_files(0))
1919

2020
if (type == "path")
21-
return(CPL_get_data_dir(FALSE))
21+
return(CPL_get_data_dir(TRUE))
2222

2323
if (!missing(path) && is.character(path))
24-
return(invisible(unique(CPL_set_data_dir(path))))
24+
return(invisible(unique(CPL_set_data_dir(path, TRUE))))
2525

2626
if (type == "network")
2727
return(CPL_is_network_enabled(TRUE))
@@ -81,16 +81,26 @@ sf_project = function(from = character(0), to = character(0), pts, keep = FALSE,
8181

8282
#' Manage PROJ settings
8383
#'
84-
#' Manage PROJ search path and network settings
85-
#' @param paths the search path to be set; omit if no paths need to be set
84+
#' Query or manage PROJ search path and network settings
85+
#' @param paths the search path to be set; omit if paths need to be queried
86+
#' @param with_proj logical; if `NA` set for both GDAL and PROJ, otherwise set either for PROJ (TRUE) or GDAL (FALSE)
8687
#' @return `sf_proj_search_paths()` returns the search path (possibly after setting it)
8788
#' @name proj_tools
8889
#' @export
89-
sf_proj_search_paths = function(paths = character(0)) {
90+
sf_proj_search_paths = function(paths = character(0), with_proj = NA) {
9091
if (length(paths) == 0)
9192
CPL_get_data_dir(FALSE)
92-
else
93-
CPL_set_data_dir(as.character(paths)) # set
93+
else {
94+
if (is.na(with_proj) || !isTRUE(with_proj))
95+
CPL_set_data_dir(as.character(paths), FALSE) # set GDAL
96+
if (is.na(with_proj) || isTRUE(with_proj)) { # set for PROJ
97+
if (length(paths) > 1) {
98+
paths = paste0(paths, collapse = .Platform$path.sep)
99+
message(paste("setting proj path(s) to", paths))
100+
}
101+
CPL_set_data_dir(as.character(paths), TRUE)
102+
}
103+
}
94104
}
95105

96106
#' @param enable logical; set this to enable (TRUE) or disable (FALSE) the proj network search facility

man/proj_tools.Rd

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

man/st_transform.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RcppExports.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,13 +1053,13 @@ BEGIN_RCPP
10531053
END_RCPP
10541054
}
10551055
// CPL_get_data_dir
1056-
Rcpp::CharacterVector CPL_get_data_dir(bool b);
1057-
RcppExport SEXP _sf_CPL_get_data_dir(SEXP bSEXP) {
1056+
Rcpp::CharacterVector CPL_get_data_dir(bool from_proj);
1057+
RcppExport SEXP _sf_CPL_get_data_dir(SEXP from_projSEXP) {
10581058
BEGIN_RCPP
10591059
Rcpp::RObject rcpp_result_gen;
10601060
Rcpp::RNGScope rcpp_rngScope_gen;
1061-
Rcpp::traits::input_parameter< bool >::type b(bSEXP);
1062-
rcpp_result_gen = Rcpp::wrap(CPL_get_data_dir(b));
1061+
Rcpp::traits::input_parameter< bool >::type from_proj(from_projSEXP);
1062+
rcpp_result_gen = Rcpp::wrap(CPL_get_data_dir(from_proj));
10631063
return rcpp_result_gen;
10641064
END_RCPP
10651065
}
@@ -1087,13 +1087,14 @@ BEGIN_RCPP
10871087
END_RCPP
10881088
}
10891089
// CPL_set_data_dir
1090-
Rcpp::LogicalVector CPL_set_data_dir(Rcpp::CharacterVector data_dir);
1091-
RcppExport SEXP _sf_CPL_set_data_dir(SEXP data_dirSEXP) {
1090+
Rcpp::LogicalVector CPL_set_data_dir(Rcpp::CharacterVector data_dir, bool with_proj);
1091+
RcppExport SEXP _sf_CPL_set_data_dir(SEXP data_dirSEXP, SEXP with_projSEXP) {
10921092
BEGIN_RCPP
10931093
Rcpp::RObject rcpp_result_gen;
10941094
Rcpp::RNGScope rcpp_rngScope_gen;
10951095
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type data_dir(data_dirSEXP);
1096-
rcpp_result_gen = Rcpp::wrap(CPL_set_data_dir(data_dir));
1096+
Rcpp::traits::input_parameter< bool >::type with_proj(with_projSEXP);
1097+
rcpp_result_gen = Rcpp::wrap(CPL_set_data_dir(data_dir, with_proj));
10971098
return rcpp_result_gen;
10981099
END_RCPP
10991100
}
@@ -1534,7 +1535,7 @@ static const R_CallMethodDef CallEntries[] = {
15341535
{"_sf_CPL_get_data_dir", (DL_FUNC) &_sf_CPL_get_data_dir, 1},
15351536
{"_sf_CPL_is_network_enabled", (DL_FUNC) &_sf_CPL_is_network_enabled, 1},
15361537
{"_sf_CPL_enable_network", (DL_FUNC) &_sf_CPL_enable_network, 2},
1537-
{"_sf_CPL_set_data_dir", (DL_FUNC) &_sf_CPL_set_data_dir, 1},
1538+
{"_sf_CPL_set_data_dir", (DL_FUNC) &_sf_CPL_set_data_dir, 2},
15381539
{"_sf_CPL_use_proj4_init_rules", (DL_FUNC) &_sf_CPL_use_proj4_init_rules, 1},
15391540
{"_sf_CPL_proj_version", (DL_FUNC) &_sf_CPL_proj_version, 1},
15401541
{"_sf_CPL_proj_is_valid", (DL_FUNC) &_sf_CPL_proj_is_valid, 1},

src/proj.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,20 @@ Rcpp::DataFrame CPL_get_pipelines(Rcpp::CharacterVector crs, Rcpp::CharacterVect
185185
}
186186

187187
// [[Rcpp::export]]
188-
Rcpp::CharacterVector CPL_get_data_dir(bool b = false) {
189-
Rcpp::CharacterVector ret(2);
190-
ret[0] = proj_info().searchpath;
188+
Rcpp::CharacterVector CPL_get_data_dir(bool from_proj = false) {
189+
if (from_proj) {
190+
Rcpp::CharacterVector ret(proj_info().searchpath);
191+
return ret;
192+
} else {
191193
#if GDAL_VERSION_NUM >= 3000300
192-
char **ogr_sp = OSRGetPROJSearchPaths();
193-
Rcpp::CharacterVector ogr_sp_sf = charpp2CV(ogr_sp);
194-
ret[1] = ogr_sp_sf[0];
195-
CSLDestroy(ogr_sp);
194+
char **ogr_sp = OSRGetPROJSearchPaths();
195+
Rcpp::CharacterVector ogr_sp_sf = charpp2CV(ogr_sp);
196+
CSLDestroy(ogr_sp);
197+
return ogr_sp_sf;
196198
#else
197-
ret[1] = "requires GDAL >= 3.0.3";
199+
Rcpp::stop("requires GDAL >= 3.0.3");
198200
#endif
199-
Rcpp::CharacterVector nms(2);
200-
nms(0) = "PROJ";
201-
nms(1) = "GDAL";
202-
ret.attr("names") = nms;
203-
return ret;
201+
}
204202
}
205203

206204
// [[Rcpp::export]]
@@ -240,16 +238,21 @@ Rcpp::CharacterVector CPL_enable_network(Rcpp::CharacterVector url, bool enable
240238
}
241239

242240
// [[Rcpp::export]]
243-
Rcpp::LogicalVector CPL_set_data_dir(Rcpp::CharacterVector data_dir) {
244-
if (data_dir.size() != 1)
245-
Rcpp::stop("data_dir should be size 1 character vector"); // #nocov
246-
std::string dd = Rcpp::as<std::string>(data_dir);
247-
const char *cp = dd.c_str();
248-
proj_context_set_search_paths(PJ_DEFAULT_CTX, 1, &cp);
241+
Rcpp::LogicalVector CPL_set_data_dir(Rcpp::CharacterVector data_dir, bool with_proj) {
242+
if (with_proj) {
243+
if (data_dir.size() != 1)
244+
Rcpp::stop("data_dir should be size 1 character vector"); // #nocov
245+
std::string dd = Rcpp::as<std::string>(data_dir);
246+
const char *cp = dd.c_str();
247+
proj_context_set_search_paths(PJ_DEFAULT_CTX, 1, &cp);
248+
} else {
249249
#if GDAL_VERSION_NUM >= 3000000
250-
std::vector <char *> dirs = create_options(data_dir, true);
251-
OSRSetPROJSearchPaths(dirs.data());
250+
std::vector <char *> dirs = create_options(data_dir, true);
251+
OSRSetPROJSearchPaths(dirs.data());
252+
#else
253+
Rcpp::stop("setting proj search path for GDAL requires GDAL >= 3.0.0");
252254
#endif
255+
}
253256
return true;
254257
}
255258

@@ -452,7 +455,7 @@ Rcpp::CharacterVector CPL_enable_network(Rcpp::CharacterVector url, bool enable
452455
#endif
453456
}
454457

455-
Rcpp::CharacterVector CPL_get_data_dir(bool b = false) {
458+
Rcpp::CharacterVector CPL_get_data_dir(bool from_proj = false) {
456459
#if PROJ_VERSION_MAJOR >= 7
457460
return Rcpp::CharacterVector(proj_info().searchpath);
458461
#else

tests/spatstat.Rout.save

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
R version 4.3.1 (2023-06-16) -- "Beagle Scouts"
2+
R version 4.3.2 (2023-10-31) -- "Eye Holes"
33
Copyright (C) 2023 The R Foundation for Statistical Computing
44
Platform: x86_64-pc-linux-gnu (64-bit)
55

@@ -144,9 +144,9 @@ First 10 features:
144144
8 point POINT (843278.5 287241.6)
145145
9 point POINT (648477.7 235466.6)
146146
10 point POINT (852593 267248.3)
147-
Error in st_poly_sample(x, size = size, ..., type = type, by_polygon = by_polygon) :
147+
Error in st_poly_sample(x, size = size, ..., type = type, by_polygon = by_polygon, :
148148
rthomas is not an exported function from spatstat.random.
149-
Error in st_poly_sample(x, size = size, ..., type = type, by_polygon = by_polygon) :
149+
Error in st_poly_sample(x, size = size, ..., type = type, by_polygon = by_polygon, :
150150
The spatstat function rThomas did not return a valid result. Consult the help file.
151151
Error message from spatstat:
152152
Error in spatstat_fun(..., win = spatstat.geom::as.owin(x)) :
@@ -166,4 +166,4 @@ In st_as_sfc.owin(spatstat.geom::as.owin(x)) :
166166
>
167167
> proc.time()
168168
user system elapsed
169-
2.011 0.116 2.121
169+
2.039 1.402 1.935

0 commit comments

Comments
 (0)