Skip to content

Commit

Permalink
issue 327 fix download when PUs are sf (#328)
Browse files Browse the repository at this point in the history
Moved terra::rast() call into write_spatial_data().
Reverted assert() in write_spatial_data() to Raster. This will eventually get changed to SpatRaster with #315.

Fixes error where download failed because server_export_data.R was attempting to convert the sf object to a SpatRaster.

The error in this case was not crashing the app, the download widget within the browser was throwing an error and not exporting the .zip.

closes #327
  • Loading branch information
edwardsmarc authored Jun 29, 2023
1 parent b6bef9c commit 516af49
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion R/server_export_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ server_export_data <- quote({
}
# save spatial data
write_spatial_data(
x = terra::rast(d),
x = d,
dir = td,
name = "data"
)
Expand Down
6 changes: 3 additions & 3 deletions R/utils_spatial_io.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ read_spatial_data <- function(x) {
#' # read and write raster data
#' f1 <- system.file("external/rlogo.grd", package = "raster")
#' d1 <- read_spatial_data(f1)
#' write_spatial_data(terra::rast(d1), tempdir(), "rlogo")
#' write_spatial_data(d1, tempdir(), "rlogo")
#'
#' # read and write vector data
#' f2 <- system.file("shape/nc.shp", package = "sf")
Expand All @@ -73,7 +73,7 @@ read_spatial_data <- function(x) {
#' @export
write_spatial_data <- function(x, dir, name) {
assertthat::assert_that(
inherits(x, c("sf", "SpatRaster")),
inherits(x, c("sf", "Raster")),
assertthat::is.string(dir),
assertthat::noNA(dir),
assertthat::is.string(name),
Expand All @@ -87,7 +87,7 @@ write_spatial_data <- function(x, dir, name) {
## save raster data in GeoTIFF format
suppressWarnings({
writeNamedRaster(
x = x, filename = file.path(dir, paste0(name, ".tif")),
x = terra::rast(x), filename = file.path(dir, paste0(name, ".tif")),
overwrite = TRUE, NAflag = -9999
)
})
Expand Down
2 changes: 1 addition & 1 deletion man/write_spatial_data.Rd

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

2 changes: 0 additions & 2 deletions tests/testthat/test_write_spatial_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ test_that("vector data", {
test_that("raster data", {
# create dataset
d <- simulate_proportion_spatial_data(import_simple_raster_data(), 3)
# convert to SpatRaster
d <- terra::rast(d)
# save data
write_spatial_data(d, dir = tempdir(), name = "data2")
# run tests
Expand Down

0 comments on commit 516af49

Please sign in to comment.