Skip to content

Commit

Permalink
Raster functions: use terra by default
Browse files Browse the repository at this point in the history
  • Loading branch information
florisvdh committed Nov 24, 2023
1 parent f4f8429 commit 0d78032
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 72 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Suggests:
raster (>= 3.6-3),
readxl,
rmarkdown,
terra,
testthat (>= 3.0.0),
tidyverse,
tools,
Expand Down
119 changes: 80 additions & 39 deletions R/GRTSmh.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ convert_base4frac_to_dec <-



#' Return the \code{GRTSmaster_habitats} data source as a RasterLayer or a
#' 10-layered variant as a RasterBrick
#' Return the \code{GRTSmaster_habitats} data source or a
#' 10-layered variant as a SpatRaster object
#'
#' By default, the \code{GRTSmaster_habitats} data source is returned as a
#' RasterLayer with decimal integer ranking numbers as values.
#' If \code{brick = TRUE}, a 10-layered RasterBrick is
#' single-layered SpatRaster object with decimal integer ranking numbers as values.
#' If \code{brick = TRUE}, a ten-layered SpatRaster is
#' returned (data source \code{GRTSmh_brick}; resolution 32 m)
#' with the decimal integer ranking numbers of 10 hierarchical levels of the
#' GRTS cell addresses, including the one from \code{GRTSmaster_habitats}
Expand Down Expand Up @@ -257,12 +257,13 @@ convert_base4frac_to_dec <-
#'
#' Depending on the value of the \code{brick} argument, the function either
#' returns the \code{GRTSmaster_habitats} data source as a
#' RasterLayer (\code{brick = FALSE}), or (\code{brick = TRUE}) returns the
#' 10-layered RasterBrick \code{GRTSmh_brick} (resolution 32 m)
#' single-layered SpatRaster (\code{brick = FALSE}), or (\code{brick = TRUE})
#' returns the \code{GRTSmh_brick} data source as a
#' ten-layered SpatRaster (resolution 32 m)
#' with the decimal integer ranking numbers of 10 hierarchical levels of the
#' GRTS cell addresses, including the one from \code{GRTSmaster_habitats}
#' (with GRTS cell addresses at the resolution level).
#' The \code{GRTSmh_brick} data source is a processed dataset (10-layered
#' The \code{GRTSmh_brick} data source is a processed dataset (ten-layered
#' GeoTIFF), available at
#' \href{https://doi.org/10.5281/zenodo.3354403}{Zenodo}, and can only be
#' returned by the function when it is already present as a file.
Expand All @@ -273,20 +274,26 @@ convert_base4frac_to_dec <-
#' Both GeoTIFFs (\code{GRTSmaster_habitats}, \code{GRTSmh_brick}) use the
#' \code{INT4S} datatype.
#'
#' The higher-level ranking numbers of the RasterBrick allow spatially balanced
#' The higher-level ranking numbers of the ten-layered variant allow spatially
#' balanced
#' samples at lower spatial resolution than that of 32 m, and can also be
#' used for aggregation purposes.
#' The provided hierarchical levels correspond to the resolutions vector
#' \code{32 * 2^(0:9)} (minimum: 32 meters, maximum: 16384 meters), with
#' the corresponding RasterBrick layers named as \code{level0} to \code{level9}.
#' the corresponding SpatRaster layers named as \code{level0} to \code{level9}.
#'
#' @param brick Logical; determines whether the RasterLayer or RasterBrick data
#' source is returned. See the Details section.
#' @param brick Logical; determines whether the single- or ten-layered
#' SpatRaster is returned. See the Details section.
#'
#' @inheritParams read_habitatmap_stdized
#'
#' @return
#' Either a RasterLayer or a 10-layered RasterBrick, always with 21041043 cells.
#' A single- or a ten-layered SpatRaster object, always with 21041043
#' cells.
#'
#' If the package is configured to use the raster package
#' (see \code{\link[=n2khab_options]{n2khab_options()}}), a RasterLayer is
#' returned if \code{brick = FALSE} and a RasterBrick if \code{brick = TRUE}.
#'
#' @family functions involved in processing the 'GRTSmaster_habitats' data source
#'
Expand All @@ -310,6 +317,8 @@ convert_base4frac_to_dec <-
#' # what to do.
#' r <- read_GRTSmh()
#' r
#' r10 <- read_GRTSmh(brick = TRUE)
#' r10
#' }
#'
#' @export
Expand All @@ -323,26 +332,38 @@ read_GRTSmh <-
)
),
brick = FALSE) {
require_pkgs("raster")

if (isTRUE(n2khab_using_raster())) {
require_pkgs("raster")
read1 <- raster::raster
read2 <- raster::brick
nl <- raster::nlayers
`crsystem<-` <- raster::`crs<-`
} else {
require_pkgs("terra")
read1 <- read2 <- terra::rast
nl <- terra::nlyr
`crsystem<-` <- terra::`crs<-`
}

if (brick) {
if (missing(file)) {
b <- raster::brick(file[2])
b <- read2(file[2])
} else {
b <- raster::brick(file)
b <- read2(file)
}
names(b) <- str_c("level", 0:(raster::nlayers(b) - 1))
names(b) <- str_c("level", 0:(nl(b) - 1))
result <- b
} else {
if (missing(file)) {
r <- raster::raster(file[1])
r <- read1(file[1])
} else {
r <- raster::raster(file)
r <- read1(file)
}
result <- r
}
raster::crs(result) <- "EPSG:31370"
return(result)
crsystem(result) <- "EPSG:31370"
result
}


Expand All @@ -367,11 +388,11 @@ read_GRTSmh <-


#' Return the processed data source \code{GRTSmh_base4frac} as a
#' RasterLayer
#' SpatRaster
#'
#' The \code{GRTSmh_base4frac} data source is like a mirror to
#' \code{GRTSmaster_habitats}, holding the ranking numbers as base 4 fractions.
#' The function returns it as a RasterLayer in the Belgian Lambert 72 CRS
#' The function returns it as a SpatRaster in the Belgian Lambert 72 CRS
#' (EPSG-code \href{https://epsg.io/31370}{31370}).
#'
#' The data source file, read by the function, is a monolayered GeoTIFF in the
Expand Down Expand Up @@ -408,7 +429,11 @@ read_GRTSmh <-
#' @inheritParams read_habitatmap_stdized
#'
#' @return
#' A RasterLayer with 21041043 cells.
#' A SpatRaster with 21041043 cells.
#'
#' If the package is configured to use the raster package
#' (see \code{\link[=n2khab_options]{n2khab_options()}}), a RasterLayer is
#' returned instead.
#'
#' @family functions involved in processing the 'GRTSmaster_habitats' data source
#'
Expand All @@ -426,8 +451,10 @@ read_GRTSmh <-
#' # 'GRTSmh_base4frac' data source is present in the default subdirectory.
#' # In all other cases, this example won't work but at least you can consider
#' # what to do.
#' oldopt <- options(scipen = 999, digits = 15)
#' r <- read_GRTSmh_base4frac()
#' r
#' options(oldopt)
#' }
#'
#' @export
Expand All @@ -436,11 +463,16 @@ read_GRTSmh_base4frac <-
fileman_up("n2khab_data"),
"20_processed/GRTSmh_base4frac/GRTSmh_base4frac.tif"
)) {
require_pkgs("raster")

r <- raster::raster(file)
raster::crs(r) <- "EPSG:31370"
return(r)
if (isTRUE(n2khab_using_raster())) {
require_pkgs("raster")
r <- raster::raster(file)
raster::crs(r) <- "EPSG:31370"
} else {
require_pkgs("terra")
r <- terra::rast(file)
terra::crs(r) <- "EPSG:31370"
}
r
}


Expand Down Expand Up @@ -470,14 +502,14 @@ read_GRTSmh_base4frac <-



#' Return a RasterLayer or an \code{sf} polygon layer from the processed data
#' Return a SpatRaster or an \code{sf} polygon layer from the processed data
#' source \code{GRTSmh_diffres}
#'
#' The \code{GRTSmh_diffres} data source is derived from
#' \code{GRTSmh_brick}.
#' It provides the hierarchical levels 1 to 9 of the
#' GRTS cell addresses at the corresponding spatial resolution.
#' The function returns one selected level, either as a RasterLayer or as an
#' The function returns one selected level, either as a SpatRaster or as an
#' \code{sf} polygon layer (in the latter case, only levels 4 to 9 are
#' provided).
#' The coordinate reference system is 'BD72 / Belgian Lambert 72'
Expand Down Expand Up @@ -514,7 +546,7 @@ read_GRTSmh_base4frac <-
#' }
#'
#' The function returns the selected \code{level} either as an \code{sf} polygon
#' layer or as a RasterLayer, depending on the
#' layer or as a SpatRaster, depending on the
#' \code{polygon} argument.
#'
#' The higher-level ranking numbers (compared to the original level 0) allow
Expand Down Expand Up @@ -554,12 +586,16 @@ read_GRTSmh_base4frac <-
#' @param level Integer in the range from 1 to 9; determines the spatial
#' resolution. See the Details section.
#' @param polygon Logical; determines whether a polygon layer or a
#' RasterLayer is returned. See the Details section.
#' SpatRaster is returned. See the Details section.
#'
#' @return
#' Either a RasterLayer or a Simple feature collection of geometry type
#' Either a SpatRaster or a Simple feature collection of geometry type
#' \code{POLYGON}.
#'
#' If the package is configured to use the raster package
#' (see \code{\link[=n2khab_options]{n2khab_options()}}), a RasterLayer is
#' returned instead of a SpatRaster.
#'
#' @family functions involved in processing the 'GRTSmaster_habitats' data source
#'
#' @examples
Expand All @@ -572,10 +608,8 @@ read_GRTSmh_base4frac <-
#' # what to do.
#' r <- read_GRTSmh_diffres(level = 7)
#' r
#' raster::spplot(r)
#' p <- read_GRTSmh_diffres(level = 7, polygon = TRUE)
#' p
#' plot(p)
#' }
#'
#' @export
Expand Down Expand Up @@ -606,17 +640,24 @@ read_GRTSmh_diffres <-
suppressWarnings(st_crs(p) <- 31370)
p
} else {
require_pkgs("raster")

r <- raster::raster(file.path(
if (isTRUE(n2khab_using_raster())) {
require_pkgs("raster")
read <- raster::raster
`crsystem<-` <- raster::`crs<-`
} else {
require_pkgs("terra")
read <- terra::rast
`crsystem<-` <- terra::`crs<-`
}
r <- read(file.path(
dir,
str_c(
"GRTSmh_diffres.",
level, ".tif"
)
))
names(r) <- str_c("level", level)
raster::crs(r) <- "EPSG:31370"
crsystem(r) <- "EPSG:31370"
r
}
}
24 changes: 16 additions & 8 deletions R/read_raster_runif.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Return the data source \code{raster_runif} as a RasterLayer
#' Return the data source \code{raster_runif} as a SpatRaster
#'
#' The \code{raster_runif} data source covers Flanders and the Brussels
#' Capital Region
Expand All @@ -21,7 +21,11 @@
#' @inheritParams read_habitatmap_stdized
#'
#' @return
#' A RasterLayer.
#' A SpatRaster.
#'
#' If the package is configured to use the raster package
#' (see \code{\link[=n2khab_options]{n2khab_options()}}), a RasterLayer is
#' returned instead.
#'
#' @examples
#' \dontrun{
Expand All @@ -33,7 +37,6 @@
#' # what to do.
#' r <- read_raster_runif()
#' r
#' raster::spplot(r)
#' }
#'
#' @export
Expand All @@ -48,10 +51,15 @@ read_raster_runif <-
version = "raster_runif_v1") {
assert_that(file.exists(file))

require_pkgs("raster")

r <- raster::raster(file)
raster::crs(r) <- "EPSG:31370"
if (isTRUE(n2khab_using_raster())) {
require_pkgs("raster")
r <- raster::raster(file)
raster::crs(r) <- "EPSG:31370"
} else {
require_pkgs("terra")
r <- terra::rast(file)
terra::crs(r) <- "EPSG:31370"
}

return(r)
r
}
Loading

0 comments on commit 0d78032

Please sign in to comment.