Skip to content

Commit

Permalink
read_GRTSmh***(): harden CRS representation *
Browse files Browse the repository at this point in the history
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
  • Loading branch information
florisvdh committed Sep 28, 2020
1 parent 2e4497d commit b7f6ce5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Remotes:
inbo/inborutils
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
Suggests:
googledrive,
knitr,
Expand All @@ -58,6 +58,7 @@ Suggests:
readxl,
remotes,
rmarkdown,
sp,
tidyverse,
units
VignetteBuilder: knitr
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 22 additions & 8 deletions R/GRTSmh.R
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,6 @@ convert_base4frac_to_dec <-
#' }
#'
#' @export
#' @importFrom sf
#' st_crs
#' @importFrom raster
#' raster
#' brick
Expand All @@ -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 {
Expand All @@ -337,7 +341,7 @@ read_GRTSmh <-
}
result <- r
}
crs(result) <- st_crs(31370)$proj4string
crs(result) <- sp::CRS(SRS_string = "EPSG:31370")
return(result)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -579,7 +588,6 @@ read_GRTSmh_base4frac <-
#' @importFrom stringr str_c
#' @importFrom sf
#' read_sf
#' st_crs
#' st_crs<-
#' @importFrom raster
#' raster
Expand Down Expand Up @@ -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

}
Expand Down

0 comments on commit b7f6ce5

Please sign in to comment.