Skip to content

Commit bd0ff49

Browse files
committed
fix: sync GDAL/PROJ search path
1 parent 7f99d0b commit bd0ff49

File tree

4 files changed

+56
-35
lines changed

4 files changed

+56
-35
lines changed

R/RcppExports.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ rgb2hex <- function(x) {
113113
.Call(`_terra_get_proj_search_paths`)
114114
}
115115

116-
.set_proj_search_paths <- function(paths) {
117-
.Call(`_terra_set_proj_search_paths`, paths)
116+
.set_proj_search_paths <- function(paths, with_proj = FALSE) {
117+
.Call(`_terra_set_proj_search_paths`, paths, with_proj)
118118
}
119119

120120
.PROJ_network <- function(enable, url) {

R/zzz.R

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@
2424

2525
.gdinit <- function() {
2626
path = ""
27-
sf <- system.file("", package="terra")
28-
if (file.exists(file.path(sf, "proj/nad.lst"))) {
29-
path <- system.file("proj", package="terra")
30-
.gdalinit(path, file.path(sf, "gdal"))
31-
if ( Sys.info()["sysname"] == "Windows" ) {
32-
.set_proj_search_paths(path)
33-
}
34-
} else {
35-
.gdalinit(path, file.path(sf, "gdal"))
27+
proj_path <- system.file("proj", package="terra")
28+
gdal_path <- system.file("gdal", package="terra")
29+
30+
has_proj <- dir.exists(proj_path) && (file.exists(file.path(proj_path, "proj.db")) || file.exists(file.path(proj_path, "nad.lst")))
31+
has_gdal <- dir.exists(gdal_path)
32+
33+
if (has_proj) {
34+
projPaths(proj_path, TRUE)
3635
}
36+
37+
.gdalinit(path, ifelse(has_gdal, gdal_path, ""))
38+
3739
if (libVersion("gdal") == "3.6.0") {
3840
message("Using GDAL version 3.6.0 which was retracted because it cannot write large GPKG files")
3941
}

src/RcppExports.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,23 +319,24 @@ BEGIN_RCPP
319319
END_RCPP
320320
}
321321
// set_proj_search_paths
322-
bool set_proj_search_paths(std::vector<std::string> paths);
323-
RcppExport SEXP _terra_set_proj_search_paths(SEXP pathsSEXP) {
322+
bool set_proj_search_paths(std::vector<std::string> paths, bool with_proj);
323+
RcppExport SEXP _terra_set_proj_search_paths(SEXP pathsSEXP, SEXP with_projSEXP) {
324324
BEGIN_RCPP
325325
Rcpp::RObject rcpp_result_gen;
326326
Rcpp::RNGScope rcpp_rngScope_gen;
327327
Rcpp::traits::input_parameter< std::vector<std::string> >::type paths(pathsSEXP);
328-
rcpp_result_gen = Rcpp::wrap(set_proj_search_paths(paths));
328+
Rcpp::traits::input_parameter< bool >::type with_proj(with_projSEXP);
329+
rcpp_result_gen = Rcpp::wrap(set_proj_search_paths(paths, with_proj));
329330
return rcpp_result_gen;
330331
END_RCPP
331332
}
332333
// PROJ_network
333-
std::string PROJ_network(bool enable, std::string url);
334+
std::string PROJ_network(int enable, std::string url);
334335
RcppExport SEXP _terra_PROJ_network(SEXP enableSEXP, SEXP urlSEXP) {
335336
BEGIN_RCPP
336337
Rcpp::RObject rcpp_result_gen;
337338
Rcpp::RNGScope rcpp_rngScope_gen;
338-
Rcpp::traits::input_parameter< bool >::type enable(enableSEXP);
339+
Rcpp::traits::input_parameter< int >::type enable(enableSEXP);
339340
Rcpp::traits::input_parameter< std::string >::type url(urlSEXP);
340341
rcpp_result_gen = Rcpp::wrap(PROJ_network(enable, url));
341342
return rcpp_result_gen;
@@ -480,7 +481,7 @@ static const R_CallMethodDef CallEntries[] = {
480481
{"_terra_setGDALCacheSizeMB", (DL_FUNC) &_terra_setGDALCacheSizeMB, 2},
481482
{"_terra_getGDALCacheSizeMB", (DL_FUNC) &_terra_getGDALCacheSizeMB, 1},
482483
{"_terra_get_proj_search_paths", (DL_FUNC) &_terra_get_proj_search_paths, 0},
483-
{"_terra_set_proj_search_paths", (DL_FUNC) &_terra_set_proj_search_paths, 1},
484+
{"_terra_set_proj_search_paths", (DL_FUNC) &_terra_set_proj_search_paths, 2},
484485
{"_terra_PROJ_network", (DL_FUNC) &_terra_PROJ_network, 2},
485486
{"_terra_removeDriver", (DL_FUNC) &_terra_removeDriver, 1},
486487
{"_terra_pearson_cor", (DL_FUNC) &_terra_pearson_cor, 3},

src/RcppFunctions.cpp

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -434,14 +434,14 @@ void gdal_init(std::string projpath, std::string datapath) {
434434
//GDAL_NETCDF_IGNORE_XY_AXIS_NAME_CHECKS
435435

436436
//GDALregistred = true;
437-
#if GDAL_VERSION_MAJOR >= 3
438-
#ifdef PROJ_6
439437
if (!projpath.empty()) {
440-
const char *cp = projpath.c_str();
441-
proj_context_set_search_paths(PJ_DEFAULT_CTX, 1, &cp);
442-
}
443-
#endif
438+
#if GDAL_VERSION_NUM >= 3000000
439+
std::vector<char *> cpaths(2);
440+
cpaths[0] = (char *)projpath.c_str();
441+
cpaths[1] = NULL;
442+
OSRSetPROJSearchPaths(cpaths.data());
444443
#endif
444+
}
445445
#ifdef PROJ_71
446446
#ifndef __EMSCRIPTEN__
447447
proj_context_set_enable_network(PJ_DEFAULT_CTX, 1);
@@ -567,36 +567,54 @@ std::vector<std::string> get_proj_search_paths() {
567567

568568

569569
// [[Rcpp::export(name = ".set_proj_search_paths")]]
570-
bool set_proj_search_paths(std::vector<std::string> paths) {
570+
bool set_proj_search_paths(std::vector<std::string> paths, bool with_proj = false) {
571571
if (paths.empty()) {
572572
return false;
573573
}
574+
if (with_proj) {
575+
// Set for PROJ library
576+
if (paths.size() == 1) {
577+
const char *cp = paths[0].c_str();
578+
proj_context_set_search_paths(PJ_DEFAULT_CTX, 1, &cp);
579+
}
580+
return true;
581+
} else {
582+
// Set for GDAL
574583
#if GDAL_VERSION_NUM >= 3000000
575-
std::vector <char *> cpaths(paths.size()+1);
576-
for (size_t i = 0; i < paths.size(); i++) {
577-
cpaths[i] = (char *) (paths[i].c_str());
578-
}
579-
cpaths[cpaths.size()-1] = NULL;
580-
OSRSetPROJSearchPaths(cpaths.data());
581-
return true;
584+
std::vector <char *> cpaths(paths.size()+1);
585+
for (size_t i = 0; i < paths.size(); i++) {
586+
cpaths[i] = (char *) (paths[i].c_str());
587+
}
588+
cpaths[cpaths.size()-1] = NULL;
589+
OSRSetPROJSearchPaths(cpaths.data());
590+
return true;
582591
#else
583-
return false;
592+
return false;
584593
#endif
594+
}
585595
}
586596

587597

588598
// [[Rcpp::export(name = ".PROJ_network")]]
589-
std::string PROJ_network(bool enable, std::string url) {
599+
std::string PROJ_network(int enable, std::string url) {
590600
std::string s = "";
591601
#ifdef PROJ_71
592-
if (enable) {
602+
if (enable == -1) { // get current status
603+
return std::to_string(proj_context_is_network_enabled(PJ_DEFAULT_CTX));
604+
} else if (enable == 1) {
593605
proj_context_set_enable_network(PJ_DEFAULT_CTX, 1);
606+
#if GDAL_VERSION_NUM >= 3040000
607+
OSRSetPROJEnableNetwork(1);
608+
#endif
594609
if (url.size() > 5) {
595610
proj_context_set_url_endpoint(PJ_DEFAULT_CTX, url.c_str());
596611
}
597612
s = proj_context_get_url_endpoint(PJ_DEFAULT_CTX);
598-
} else { // disable:
613+
} else if (enable == 0) { // disable:
599614
proj_context_set_enable_network(PJ_DEFAULT_CTX, 0);
615+
#if GDAL_VERSION_NUM >= 3040000
616+
OSRSetPROJEnableNetwork(0);
617+
#endif
600618
}
601619
#endif
602620
return s;

0 commit comments

Comments
 (0)