if (FALSE) { # interactive()
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), 100)
get_stac_data(aoi,
start_date = "2022-06-01",
end_date = "2022-06-30",
pixel_x_size = 30,
pixel_y_size = 30,
asset_names = c(
"red", "blue", "green"
),
stac_source = "https://planetarycomputer.microsoft.com/api/stac/v1/",
collection = "landsat-c2-l2",
query_function = rsi_query_api,
sign_function = sign_planetary_computer,
mask_band = "qa_pixel",
mask_function = landsat_mask_function,
item_filter_function = landsat_platform_filter,
platforms = c("landsat-9", "landsat-8"),
output_filename = tempfile(fileext = ".tif")
)
# or, mostly equivalently (will download more bands):
landsat_image <- get_landsat_imagery(
aoi,
start_date = "2022-06-01",
end_date = "2022-08-30",
output_filename = tempfile(fileext = ".tif")
)
landsat_image |>
terra::rast() |>
terra::stretch() |>
terra::plotRGB()
# The `get_*_imagery()` functions will download
# all available "data" assets by default
# (usually including measured values, and excluding derived bands)
sentinel1_data <- get_sentinel1_imagery(
aoi,
start_date = "2022-06-01",
end_date = "2022-07-01",
output_filename = tempfile(fileext = ".tif")
)
names(terra::rast(sentinel1_data))
# You can see what bands will be downloaded by a function
# by inspecting the corresponding `band_mapping` object:
sentinel2_band_mapping$planetary_computer_v1
# And you can add additional assets using `c()`:
c(
sentinel2_band_mapping$planetary_computer_v1,
"scl"
)
# Or subset the assets downloaded using `[` or `[[`:
sentinel2_imagery <- get_sentinel2_imagery(
aoi,
start_date = "2022-06-01",
end_date = "2022-07-01",
asset_names = sentinel2_band_mapping$planetary_computer_v1["B01"],
output_filename = tempfile(fileext = ".tif")
)
names(terra::rast(sentinel2_imagery))
# If you're downloading data for a particularly large AOI,
# and can't composite the resulting images or want to make
# sure you can continue an interrupted download,
# consider tiling your AOI and requesting each tile separately:
aoi <- sf::st_make_grid(aoi, n = 2)
tiles <- lapply(
seq_along(aoi),
function(i) {
get_landsat_imagery(
aoi[i],
start_date = "2022-06-01",
end_date = "2022-08-30",
output_filename = tempfile(fileext = ".tif")
)
}
)
# You'll get a list of tiles that you can then composite or
# work with however you wish:
unlist(tiles)
}
Run the code above in your browser using DataLab