diff --git a/R/gdalwarp.R b/R/gdalwarp.R index 5eb7ba2..151a5e7 100644 --- a/R/gdalwarp.R +++ b/R/gdalwarp.R @@ -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) - } -} \ No newline at end of file + 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) + } +} diff --git a/R/set_projlibpath.R b/R/set_projlibpath.R new file mode 100644 index 0000000..a26d318 --- /dev/null +++ b/R/set_projlibpath.R @@ -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() + ) + } +} diff --git a/man/set_projlibpath.Rd b/man/set_projlibpath.Rd new file mode 100644 index 0000000..6f752c3 --- /dev/null +++ b/man/set_projlibpath.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/set_projlibpath.R +\name{set_projlibpath} +\alias{set_projlibpath} +\title{set_projlibpath} +\usage{ +set_projlibpath() +} +\value{ +The function is used for its side effects +} +\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) +} +\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. +}