Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Use terra in place of raster
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhsparks committed Dec 1, 2020
1 parent 1e1f93d commit 092c9c9
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ Imports:
jsonlite (>= 1.5),
lubridate,
magrittr (>= 1.5),
raster,
rgdal,
readr (>= 1.1.1),
rvest,
terra,
tidyr (>= 0.6.3),
tools,
utils,
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ importFrom(httr,content)
importFrom(magrittr,"%$%")
importFrom(magrittr,"%>%")
importFrom(magrittr,use_series)
importFrom(raster,plot)
importFrom(terra,plot)
15 changes: 7 additions & 8 deletions R/get_radar_imagery.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ get_available_radar <- function(radar_id = "all") {
#'
#' Fetch \acronym{BOM} radar imagery from
#' \url{ftp://ftp.bom.gov.au/anon/gen/radar/} and return a
#' \code{\link[raster]{raster}} layer object. Files available are the most
#' \code{\link[terra]{SpatRaster}} layer object. Files available are the most
#' recent radar snapshot which are updated approximately every 6 to 10 minutes.
#' Suggested to check file availability first by using
#' \code{\link{get_available_radar}}.
#'
#' @param product_id Character. \acronym{BOM} product ID to download and import
#' as a \code{\link[raster]{raster}} object. Value is required.
#' as a \code{\link[terra]{SpatRaster}} object. Value is required.
#'
#' @param path Character. A character string with the name where the downloaded
#' file is saved. If not provided, the default value \code{NULL} is used which
#' saves the file in a temp directory.
#'
#' @param download_only Logical. Whether the radar image is loaded into the
#' environment as a \code{\link[raster]{raster}} layer, or just downloaded.
#' environment as a \code{\link[terra]{SpatRaster}} layer, or just downloaded.
#'
#' @details Valid \acronym{BOM} \acronym{Radar} Product IDs for radar imagery
#' can be obtained from \code{\link{get_available_radar}}.
Expand All @@ -107,7 +107,6 @@ get_available_radar <- function(radar_id = "all") {
#' @examples
#' \donttest{
#' # Fetch most recent radar image for Wollongong 256km radar
#' library(raster)
#' imagery <- get_radar_imagery(product_id = "IDR032")
#' plot(imagery)
#' }
Expand Down Expand Up @@ -151,13 +150,13 @@ get_radar_imagery <- get_radar <-
quiet = TRUE
)
message("file downloaded to:", path)
y <- raster::raster(x = path)
y <- terra::rast(x = path)
y[is.na(y)] <- 999
return(y)
}
},
error = function() {
return(raster::raster(
return(terra::rast(
system.file("error_images",
"image_error_message.png",
package = "bomrang")
Expand All @@ -166,6 +165,6 @@ get_radar_imagery <- get_radar <-
}

# Export raster plot functionality to plot radar imagery
#' @importFrom raster plot
#' @importFrom terra plot
#' @export
raster::plot
terra::plot
16 changes: 8 additions & 8 deletions R/get_satellite_imagery.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ get_available_imagery <- function(product_id = "all") {
#'
#' Fetch \acronym{BOM} satellite GeoTIFF imagery from
#' \url{ftp://ftp.bom.gov.au/anon/gen/gms/} and return a raster
#' \code{\link[raster]{stack}} object of 'GeoTIFF' files. Files are available at
#' \code{\link[terra]{SpatRaster}} object of 'GeoTIFF' files. Files are available at
#' ten minute update frequency with a 24 hour delete time. Suggested to check
#' file availability first by using \code{\link{get_available_imagery}}.
#'
#' @param product_id Character. \acronym{BOM} product ID to download in
#' 'GeoTIFF' format and import as a \code{\link[raster]{stack}} object. A
#' 'GeoTIFF' format and import as a \code{\link[terra]{SpatRaster}} object. A
#' vector of values from \code{\link{get_available_imagery}} may be used here.
#' Value is required.
#' @param scans Numeric. Number of scans to download, starting with most recent
Expand Down Expand Up @@ -116,8 +116,8 @@ get_available_imagery <- function(product_id = "all") {
#' \code{\link{manage_cache}}
#'
#' @return
#' A raster stack of GeoTIFF images with layers named by \acronym{BOM} Product
#' ID, timestamp and band.
#' A \code{SpatRaster} object of GeoTIFF images with layers named by
#' \acronym{BOM} Product ID, timestamp and band.
#'
#' @references
#' Australian Bureau of Meteorology (BOM) high-definition satellite images \cr
Expand All @@ -126,7 +126,7 @@ get_available_imagery <- function(product_id = "all") {
#' @examples
#' \donttest{
#' # Fetch AHI VIS (true colour) / IR (Ch13 greyscale) composite 1km FD
#' # GEOS GIS raster stack for most recent single scan available
#' # GEOS GIS \code{SpatRaster} object for most recent single scan available
#'
#' imagery <- get_satellite_imagery(product_id = "IDE00425", scans = 1)
#'
Expand Down Expand Up @@ -199,7 +199,7 @@ get_satellite_imagery <- get_satellite <-
)
},
error = function() {
return(raster::raster(
return(terra::rast(
system.file("error_images",
"image_error_message.png",
package = "bomrang")
Expand All @@ -212,10 +212,10 @@ get_satellite_imagery <- get_satellite <-
files <- basename(files)[basename(files) %in% basename(tif_files)]
files <- file.path(cache_dir, files)
if (all(substr(files, nchar(files) - 3, nchar(files)) == ".tif")) {
read_tif <- raster::stack(files)
read_tif <- terra::rast(files)
} else {
stop(paste0(
"\nCannot create a raster stack object of ", files, ".\n",
"\nCannot create a `SpatRaster` object of ", files, ".\n",
"\nPerhaps the file download corrupted?\n",
"\nYou might also check your cache directory for the files.\n"
))
Expand Down
7 changes: 3 additions & 4 deletions man/get_radar_imagery.Rd

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

10 changes: 5 additions & 5 deletions man/get_satellite_imagery.Rd

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

2 changes: 1 addition & 1 deletion man/reexports.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/test-file_caching.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ test_that("caching utils list files in cache and delete when asked", {
skip_on_cran()
manage_cache$cache_path_set("foobar", type = "tempdir")
f <-
raster::raster(system.file("external/test.grd", package = "raster"))
terra::rast(system.file("external/test.grd", package = "raster"))
cache_dir <- manage_cache$cache_path_get()
raster::writeRaster(f, file.path(cache_dir, "file1.tif"), format = "GTiff")
raster::writeRaster(f, file.path(cache_dir, "file2.tif"), format = "GTiff")
terra::writeRaster(f, file.path(cache_dir, "file1.tif"), format = "GTiff")
terra::writeRaster(f, file.path(cache_dir, "file2.tif"), format = "GTiff")

# test bomrang cache list
k <- basename(manage_cache$list())
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-get_radar_imagery.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test_that("Error handling works", {
test_that("get_radar_imagery functions properly", {
skip_on_cran()
y <- get_radar_imagery(product_id = "IDR022")
expect_is(y, "RasterLayer")
expect_is(y, "SpatRaster")

yy <- get_radar_imagery(product_id = "IDR022", download_only = TRUE)
expect_null(yy)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-get_satellite_imagery.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test_that("get_available_imagery functions properly", {
get_satellite_imagery(product_id = "IDE00425",
scans = 1,
cache = TRUE)
expect_is(j, "RasterStack")
expect_is(j, "SpatRaster")
expect_true(dir.exists(manage_cache$cache_path_get()))

})
Expand Down

0 comments on commit 092c9c9

Please sign in to comment.