Skip to content

Commit

Permalink
Don't include qa band in output (#36)
Browse files Browse the repository at this point in the history
* Don't include qa band in output

* Fix test

* Don't expect T

* Redocument
  • Loading branch information
mikemahoney218 authored Jan 23, 2024
1 parent fc2b981 commit 25e2dd2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 65 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
109 changes: 57 additions & 52 deletions R/get_stac_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
52 changes: 39 additions & 13 deletions tests/testthat/test-get_stac_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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/",
Expand All @@ -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(
Expand All @@ -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")
)
})

0 comments on commit 25e2dd2

Please sign in to comment.