From 25e2dd293270767b720db1e46d6f6d49ad3049dd Mon Sep 17 00:00:00 2001 From: Michael Mahoney Date: Tue, 23 Jan 2024 16:47:02 -0500 Subject: [PATCH] Don't include qa band in output (#36) * Don't include qa band in output * Fix test * Don't expect T * Redocument --- NEWS.md | 4 + R/get_stac_data.R | 109 +++++++++++++++------------- tests/testthat/test-get_stac_data.R | 52 +++++++++---- 3 files changed, 100 insertions(+), 65 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0ccc502..6f5d7b0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # rsi (development version) +* `get_stac_data()` no longer includes `mask_band` in its outputs when + `composite_function = NULL`. Add this band to `asset_names` to include it in + the download. + # rsi 0.1.1 * `get_stac_data()` now removes colons (`:`) from the file names generated when diff --git a/R/get_stac_data.R b/R/get_stac_data.R index 33abfe9..cf7b4f9 100644 --- a/R/get_stac_data.R +++ b/R/get_stac_data.R @@ -745,26 +745,70 @@ complex_download <- function(items, if (!is.null(mask_band)) apply_masks(mask_band, mask_function, download_locations, p) + out <- make_composite_bands( + download_locations[, names(download_locations) %in% names(asset_names), drop = FALSE], + composite_function, + p + ) + + if (rescale_bands) lapply(out$final_bands, rescale_band, scale_strings, p) + out +} + + +make_composite_bands <- function(downloaded_bands, composite_function, p) { + if (is.null(composite_function)) { - out_vrt <- replicate(nrow(download_locations), tempfile(fileext = ".tif")) - final_bands <- apply(download_locations, 1, identity, simplify = FALSE) - } else { - out_vrt <- tempfile(fileext = ".vrt") - final_bands <- list( - make_composite_bands( - download_locations[, names(download_locations) %in% names(asset_names), drop = FALSE], - composite_function, - p + return( + list( + out_vrt = replicate(nrow(downloaded_bands), tempfile(fileext = ".tif")), + final_bands = apply(downloaded_bands, 1, identity, simplify = FALSE) ) ) } - if (rescale_bands) lapply(final_bands, rescale_band, scale_strings, p) + + download_dir <- file.path(tempdir(), "composite_dir") + if (!dir.exists(download_dir)) dir.create(download_dir) + + out <- vapply( + names(downloaded_bands), + function(band_name) { + p(glue::glue("Compositing {band_name}")) + out_file <- file.path(download_dir, paste0(toupper(band_name), ".tif")) + + if (length(downloaded_bands[[band_name]]) == 1) { + file.copy(downloaded_bands[[band_name]], out_file) + } else if (composite_function == "merge") { + do.call( + terra::merge, + list( + x = terra::sprc(lapply(downloaded_bands[[band_name]], terra::rast)), + filename = out_file, + overwrite = TRUE + ) + ) + } else { + do.call( + terra::mosaic, + list( + x = terra::sprc(lapply(downloaded_bands[[band_name]], terra::rast)), + fun = composite_function, + filename = out_file, + overwrite = TRUE + ) + ) + } + + out_file + }, + character(1) + ) + list( - final_bands = final_bands, - out_vrt = out_vrt + out_vrt = tempfile(fileext = ".vrt"), + final_bands = list(out) ) } - calc_scale_strings <- function(download_locations, items) { # Assign scale, offset attributes if they exist scales <- vapply( @@ -889,45 +933,6 @@ extract_urls <- function(asset_names, items) { items_urls } -make_composite_bands <- function(downloaded_bands, composite_function, p) { - download_dir <- file.path(tempdir(), "composite_dir") - if (!dir.exists(download_dir)) dir.create(download_dir) - - vapply( - names(downloaded_bands), - function(band_name) { - p(glue::glue("Compositing {band_name}")) - out_file <- file.path(download_dir, paste0(toupper(band_name), ".tif")) - - if (length(downloaded_bands[[band_name]]) == 1) { - file.copy(downloaded_bands[[band_name]], out_file) - } else if (composite_function == "merge") { - do.call( - terra::merge, - list( - x = terra::sprc(lapply(downloaded_bands[[band_name]], terra::rast)), - filename = out_file, - overwrite = TRUE - ) - ) - } else { - do.call( - terra::mosaic, - list( - x = terra::sprc(lapply(downloaded_bands[[band_name]], terra::rast)), - fun = composite_function, - filename = out_file, - overwrite = TRUE - ) - ) - } - - out_file - }, - character(1) - ) -} - maybe_sign_items <- function(items, sign_function) { if (!is.null(sign_function)) { items <- sign_function(items) diff --git a/tests/testthat/test-get_stac_data.R b/tests/testthat/test-get_stac_data.R index 8215b93..3e847cf 100644 --- a/tests/testthat/test-get_stac_data.R +++ b/tests/testthat/test-get_stac_data.R @@ -2,7 +2,7 @@ test_that("get_landsat_imagery() is stable", { skip_on_cran() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) - aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 1000) + aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) expect_no_error( out <- get_landsat_imagery( @@ -25,7 +25,7 @@ test_that("get_sentinel1_imagery() is stable", { skip_on_cran() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) - aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 1000) + aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) expect_no_error( out <- get_sentinel1_imagery( @@ -48,7 +48,7 @@ test_that("get_sentinel2_imagery() is stable", { skip_on_cran() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) - aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 1000) + aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) expect_no_error( out <- get_sentinel2_imagery( @@ -72,7 +72,7 @@ test_that("get_dem() is stable", { skip_on_cran() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) - aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 1000) + aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) expect_no_error( out <- get_dem( @@ -93,7 +93,7 @@ test_that("non-default mappings work", { skip_on_cran() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) - aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 1000) + aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) expect_no_error( out <- get_sentinel2_imagery( @@ -118,7 +118,7 @@ test_that("can download RTC products", { skip_on_cran() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) - aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 1000) + aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) expect_no_error( out <- get_sentinel1_imagery( @@ -142,7 +142,7 @@ test_that("hidden arguments work", { skip_on_cran() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) - aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 1000) + aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) expect_no_error( out <- get_landsat_imagery( @@ -167,7 +167,7 @@ test_that("simple merge method works", { skip_on_cran() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) - aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 1000) + aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) expect_no_error( out <- get_stac_data( @@ -189,7 +189,7 @@ test_that("warning (but not error) fires if `mask_band` is not NULL with NULL `m skip_on_cran() aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) - aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 1000) + aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100) expect_snapshot( x <- get_landsat_imagery( @@ -207,7 +207,7 @@ test_that("get_*_data works with mapply() (#17)", { skip_on_cran() san_antonio = sf::st_point(c(-98.491142, 29.424349)) san_antonio = sf::st_sfc(san_antonio, crs = "EPSG:4326") - san_antonio = sf::st_buffer(sf::st_transform(san_antonio, "EPSG:3081"), 1000) + san_antonio = sf::st_buffer(sf::st_transform(san_antonio, "EPSG:3081"), 100) expect_no_error( mapply( @@ -226,12 +226,12 @@ test_that("proper error if no items are found", { aoi <- sf::st_point(c(-74.912131, 44.080410)) aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326) - aoi <- sf::st_buffer(sf::st_transform(aoi, 5070), 1000) + aoi <- sf::st_buffer(sf::st_transform(aoi, 5070), 100) expect_error( get_stac_data( aoi, - start_date = "1970-01-01", + start_date = "1970-01-01", # pre-LCMAP dates end_date = "1970-12-31", asset_names = "lcpri", stac_source = "https://planetarycomputer.microsoft.com/api/stac/v1/", @@ -253,7 +253,7 @@ test_that("no-composite paths work on Windows #29, #32", { sf::st_sfc(sf::st_point(c(-74.912131, 44.080410)), crs = 4326), 3857 ), - 1000 + 100 ) expect_no_error( @@ -264,5 +264,31 @@ test_that("no-composite paths work on Windows #29, #32", { composite_function = NULL ) ) +}) + +test_that("no-composites return the same data", { + skip_if_offline() + skip_on_cran() + aoi <- sf::st_buffer( + sf::st_transform( + sf::st_sfc(sf::st_point(c(-74.912131, 44.080410)), crs = 4326), + 3857 + ), + 100 + ) + + expect_contains( + names( + terra::rast( + get_landsat_imagery( + aoi = aoi, + start_date = "2022-07-01", + end_date = "2022-07-05", + composite_function = NULL + ) + ) + ), + setdiff(rsi::landsat_band_mapping$planetary_computer_v1, "T") + ) })