Skip to content

Commit

Permalink
Adds function set_projlibpath
Browse files Browse the repository at this point in the history
Function needed to avoid problems related to side effects upon loading
of rgdal/sf on Windows systems with GDAL 3 installed (see r-spatial/discuss#31)

The function automatically resets the PROJ_LIB environment variable to the 
"correct" folder for a GDAL3 installation, and restores the previous value upon 
exiting from the caller function (to avoid breaking rgdal/sf functionality). 

The function needs to be called within each function calling a gdal binary requiring the PROJ_LIB environment variable to be properly set.
  • Loading branch information
lbusett committed Jan 17, 2020
1 parent 8f15118 commit 5d6a1ae
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 93 deletions.
188 changes: 95 additions & 93 deletions R/gdalwarp.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,97 +105,99 @@
#' @export

gdalwarp <- function(
#help_general,formats, # Need to fix these
srcfile,dstfile,
s_srs,t_srs,to,
order,tps,rpc,geoloc,et,refine_gcps,te,te_srs,tr,tap,ts,ovr,wo,ot,wt,r,srcnodata,dstnodata,
dstalpha,wm,multi,q,of="GTiff",co,cutline,cl,cwhere,csql,cblend,crop_to_cutline,
overwrite,nomd,cvmd,setci,oo,doo,
# additional_commands,
output_Raster=FALSE,
ignore.full_scan=TRUE,
verbose=FALSE,
...)
#help_general,formats, # Need to fix these
srcfile,dstfile,
s_srs,t_srs,to,
order,tps,rpc,geoloc,et,refine_gcps,te,te_srs,tr,tap,ts,ovr,wo,ot,wt,r,srcnodata,dstnodata,
dstalpha,wm,multi,q,of="GTiff",co,cutline,cl,cwhere,csql,cblend,crop_to_cutline,
overwrite,nomd,cvmd,setci,oo,doo,
# additional_commands,
output_Raster=FALSE,
ignore.full_scan=TRUE,
verbose=FALSE,
...)
{
if(output_Raster && (!requireNamespace("raster") || !requireNamespace("rgdal")))
{
warning("rgdal and/or raster not installed. Please install.packages(c('rgdal','raster')) or set output_Raster=FALSE")
return(NULL)
}

parameter_values <- as.list(environment())

if(verbose) message("Checking gdal_installation...")
gdal_setInstallation(ignore.full_scan=ignore.full_scan,verbose=verbose)
if(is.null(getOption("gdalUtils_gdalPath"))) return()

# Place all gdal function variables into these groupings:
parameter_variables <- list(
logical = list(
varnames <- c(
"tps","rpc","geoloc","tap","dstalpha",
"multi","q","crop_to_cutline","overwrite","nomd",
"setci"
)),
vector = list(
varnames <- c(
"te","tr","ts"
)),
scalar = list(
varnames <- c(
"order","et","refine_gcps","wm",
"cblend"
)),
character = list(
varnames <- c(
"s_srs","t_srs","to","te_srs","ovr","ot","wt","r",
"srcnodata","dstnodata","of","cutline","cl",
"cwhere","csql","cvmd","oo","doo","dstfile"
)),
repeatable = list(
varnames <- c(
"wo","co","srcfile"
))
)

parameter_order <- c(
"tps","rpc","geoloc","tap","dstalpha",
"multi","q","crop_to_cutline","overwrite","nomd",
"setci",
"te","te_srs","tr","ts","ovr",
"order","et","refine_gcps","wm",
"cblend",
"s_srs","t_srs","to","ot","wt","r",
"srcnodata","dstnodata","of","cutline","cl",
"cwhere","csql","cvmd",
"wo","co","oo","doo",
"srcfile","dstfile"
)

parameter_noflags <- c("srcfile","dstfile")

parameter_noquotes <- unlist(parameter_variables$vector)

executable <- "gdalwarp"

cmd <- gdal_cmd_builder(
executable=executable,
parameter_variables=parameter_variables,
parameter_values=parameter_values,
parameter_order=parameter_order,
parameter_noflags=parameter_noflags,
parameter_noquotes=parameter_noquotes,
gdal_installation_id=gdal_chooseInstallation(hasDrivers=of))

if(verbose) message(paste("GDAL command being used:",cmd))

cmd_output <- system(cmd,intern=TRUE)

if(output_Raster)
{
return(brick(dstfile))
} else
{
return(NULL)
}
}
if(output_Raster && (!requireNamespace("raster") || !requireNamespace("rgdal")))
{
warning("rgdal and/or raster not installed. Please install.packages(c('rgdal','raster')) or set output_Raster=FALSE")
return(NULL)
}

parameter_values <- as.list(environment())

if(verbose) message("Checking gdal_installation...")
gdal_setInstallation(ignore.full_scan=ignore.full_scan,verbose=verbose)
if(is.null(getOption("gdalUtils_gdalPath"))) return()

set_projlibpath()

# Place all gdal function variables into these groupings:
parameter_variables <- list(
logical = list(
varnames <- c(
"tps","rpc","geoloc","tap","dstalpha",
"multi","q","crop_to_cutline","overwrite","nomd",
"setci"
)),
vector = list(
varnames <- c(
"te","tr","ts"
)),
scalar = list(
varnames <- c(
"order","et","refine_gcps","wm",
"cblend"
)),
character = list(
varnames <- c(
"s_srs","t_srs","to","te_srs","ovr","ot","wt","r",
"srcnodata","dstnodata","of","cutline","cl",
"cwhere","csql","cvmd","oo","doo","dstfile"
)),
repeatable = list(
varnames <- c(
"wo","co","srcfile"
))
)

parameter_order <- c(
"tps","rpc","geoloc","tap","dstalpha",
"multi","q","crop_to_cutline","overwrite","nomd",
"setci",
"te","te_srs","tr","ts","ovr",
"order","et","refine_gcps","wm",
"cblend",
"s_srs","t_srs","to","ot","wt","r",
"srcnodata","dstnodata","of","cutline","cl",
"cwhere","csql","cvmd",
"wo","co","oo","doo",
"srcfile","dstfile"
)

parameter_noflags <- c("srcfile","dstfile")

parameter_noquotes <- unlist(parameter_variables$vector)

executable <- "gdalwarp"

cmd <- gdal_cmd_builder(
executable=executable,
parameter_variables=parameter_variables,
parameter_values=parameter_values,
parameter_order=parameter_order,
parameter_noflags=parameter_noflags,
parameter_noquotes=parameter_noquotes,
gdal_installation_id=gdal_chooseInstallation(hasDrivers=of))

if(verbose) message(paste("GDAL command being used:",cmd))

cmd_output <- system(cmd,intern=TRUE)

if(output_Raster)
{
return(brick(dstfile))
} else
{
return(NULL)
}
}
21 changes: 21 additions & 0 deletions R/set_projlibpath.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#' @title set_projlibpath
#' @description Function needed to avoid problems related to side effects upon loading
#' of rgdal/sf on Windows systems with GDAL 3 installed (see https://github.com/r-spatial/discuss/issues/31)
#' @return The function is used for its side effects
#' @details The function automatically resets the PROJ_LIB environment variable to the
#' "correct" folder for a GDAL3 installation, and restores the previous value upon
#' exiting from the caller function.
#' @rdname set_projlibpath

set_projlibpath <- function() {
# reset properly the PROJ_LIB environment library
if (substr(getOption("gdalUtils_gdalPath")[[1]]$version, 1, 1) >= 3 && Sys.info()[["sysname"]] == "Windows") {
rgdal_projlibpath <- Sys.getenv("PROJ_LIB")
projlib_path_ok <- file.path(dirname(getOption("gdalUtils_gdalPath")[[1]]$path), "share/proj")
Sys.setenv("PROJ_LIB" = projlib_path_ok)
do.call(on.exit,
list(substitute(Sys.setenv("PROJ_LIB" = rgdal_projlibpath))),
envir = parent.frame()
)
}
}
20 changes: 20 additions & 0 deletions man/set_projlibpath.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d6a1ae

Please sign in to comment.