From b7f6ce55a52ec2cfa9efe03e593afe3a15ccb4f8 Mon Sep 17 00:00:00 2001 From: florisvdh Date: Mon, 28 Sep 2020 17:07:54 +0200 Subject: [PATCH] read_GRTSmh***(): harden CRS representation * Further follow-up to #67. By using sp::CRS(SRS_string = "EPSG:31370"), compatibility is offered: - both for raster from CRAN and raster from GitHub - both for PROJ4/GDAL2 and PROJ6+/GDAL3 To be revisited once raster from CRAN accepts simple EPSG-code as input for crs<- (as on GitHub), i.e. with raster>=3.3-16 --- DESCRIPTION | 3 ++- NAMESPACE | 1 - R/GRTSmh.R | 30 ++++++++++++++++++++++-------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7093a66c..96c25c60 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -49,7 +49,7 @@ Remotes: inbo/inborutils Encoding: UTF-8 LazyData: true -RoxygenNote: 7.1.0 +RoxygenNote: 7.1.1 Suggests: googledrive, knitr, @@ -58,6 +58,7 @@ Suggests: readxl, remotes, rmarkdown, + sp, tidyverse, units VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index e5ee8de1..4116d49b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -78,7 +78,6 @@ importFrom(rprojroot,is_rstudio_project) importFrom(sf,"st_crs<-") importFrom(sf,read_sf) importFrom(sf,st_centroid) -importFrom(sf,st_crs) importFrom(sf,st_drop_geometry) importFrom(sf,st_transform) importFrom(stats,setNames) diff --git a/R/GRTSmh.R b/R/GRTSmh.R index d5ad3921..0cc8368d 100644 --- a/R/GRTSmh.R +++ b/R/GRTSmh.R @@ -309,8 +309,6 @@ convert_base4frac_to_dec <- #' } #' #' @export -#' @importFrom sf -#' st_crs #' @importFrom raster #' raster #' brick @@ -323,6 +321,12 @@ read_GRTSmh <- "20_processed/GRTSmh_brick/GRTSmh_brick.tif"), brick = FALSE) { + if (!requireNamespace("sp", quietly = TRUE)) { + stop("Package \"sp\" is needed when using this function. ", + "Please install it.", + call. = FALSE) + } + if (brick) { if (missing(file)) { b <- brick(file.path(path, file[2]))} else { @@ -337,7 +341,7 @@ read_GRTSmh <- } result <- r } - crs(result) <- st_crs(31370)$proj4string + crs(result) <- sp::CRS(SRS_string = "EPSG:31370") return(result) } @@ -427,16 +431,21 @@ read_GRTSmh <- #' } #' #' @export -#' @importFrom sf -#' st_crs #' @importFrom raster #' raster #' crs<- read_GRTSmh_base4frac <- function(path = fileman_up("n2khab_data"), file = "20_processed/GRTSmh_base4frac/GRTSmh_base4frac.tif") { + + if (!requireNamespace("sp", quietly = TRUE)) { + stop("Package \"sp\" is needed when using this function. ", + "Please install it.", + call. = FALSE) + } + r <- raster(file.path(path, file)) - crs(r) <- st_crs(31370)$proj4string + crs(r) <- sp::CRS(SRS_string = "EPSG:31370") return(r) } @@ -579,7 +588,6 @@ read_GRTSmh_base4frac <- #' @importFrom stringr str_c #' @importFrom sf #' read_sf -#' st_crs #' st_crs<- #' @importFrom raster #' raster @@ -608,11 +616,17 @@ read_GRTSmh_diffres <- } else { + if (!requireNamespace("sp", quietly = TRUE)) { + stop("Package \"sp\" is needed when using this function. ", + "Please install it.", + call. = FALSE) + } + r <- raster(file.path(path, subdir, str_c("GRTSmh_diffres.", level, ".tif"))) names(r) <- str_c("level", level) - crs(r) <- st_crs(31370)$proj4string + crs(r) <- sp::CRS(SRS_string = "EPSG:31370") r }