I'm trying to follow the example providede here https://stacspec.org/en/tutorials/1-download-data-using-r/ to download Sentinel 2 data from a defined bounding box determined by a custom shapefile that I have drawn.
I was successfull in using rstac::assets_download() function to download Sentinel 2 data for the whole scene from several months. That's in the first part of the tutorial. In trying to adapt the second part an error is being ouputed that I don't know how to solve.
Here is my code:
require(rstac)
stac_source <- rstac::stac(
"https://planetarycomputer.microsoft.com/api/stac/v1"
)
collections_query <- stac_source |>
rstac::collections()
available_collections <- rstac::get_request(collections_query)
available_collections
rstac::stac_search(
q = stac_source,
collections = "sentinel-2-l2a",
datetime = "2023-01-01/2023-01-31",
limit = 999
)
ashe <- sf::read_sf("E:/temp/test_stac1.shp")
ashe_bbox <- ashe |>
sf::st_transform(4326) |>
sf::st_bbox()
stac_query <- rstac::stac(
"https://planetarycomputer.microsoft.com/api/stac/v1"
) |>
rstac::stac_search(
collections = "sentinel-2-l2a",
bbox = ashe_bbox,
datetime = "2023-01-01/2023-01-31"
) |>
rstac::get_request()
make_vsicurl_url <- function(base_url) {
paste0(
"/vsicurl",
"?pc_url_signing=yes",
"&pc_collection=sentinel-2-l2a",
"&url=",
base_url
)
}
lcpri_url <- rstac::assets_url(executed_stac_query, "B02")
out_file <- tempfile(fileext = ".tif")
sf::gdal_utils(
"warp",
source = lcpri_url,
destination = out_file,
options = c(
"-t_srs", sf::st_crs(ashe)$wkt,
"-te", sf::st_bbox(ashe)
)
)
terra::rast(out_file) |>
terra::plot()
ashe |>
sf::st_geometry() |>
plot(lwd = 3, add = TRUE)
Error in sf::gdal_utils("warp", source = lcpri_url, destination = out_file, :
gdal_utils warp: an error occured
In addition: Warning messages:
1: In CPL_gdalwarp(source, destination, options, oo, doo, config_options, :
GDAL Error 1: HTTP error code : 404
2: In CPL_gdalwarp(source, destination, options, oo, doo, config_options, :
GDAL Error 1: HTTP error code : 404
3: In CPL_gdalwarp(source, destination, options, oo, doo, config_options, :
GDAL Error 1: HTTP error code : 404
4: In CPL_gdalwarp(source, destination, options, oo, doo, config_options, :
GDAL Error 1: HTTP error code : 404
5: In CPL_gdalwarp(source, destination, options, oo, doo, config_options, :
GDAL Error 1: HTTP error code : 404
6: In CPL_gdalwarp(source, destination, options, oo, doo, config_options, :
GDAL Error 1: HTTP error code : 404
With this code I had sucess download Sentinel2 data:
stac_source <- rstac::stac(
"https://planetarycomputer.microsoft.com/api/stac/v1"
)
stac_source
str(stac_source)
rstac::get_request(stac_source)
collections_query <- stac_source |>
rstac::collections()
collections_query
class(stac_source)
class(collections_query)
available_collections <- rstac::get_request(collections_query)
available_collections
rstac::stac_search(
q = stac_source,
collections = "sentinel-2-l2a",
datetime = "2023-02-01/2023-10-31",
limit = 999
)
ashe <- sf::read_sf("E:/temp/test_stac1.shp")
sf::st_geometry(ashe) |> plot()
ashe_bbox <- ashe |>
sf::st_transform(4326) |>
sf::st_bbox()
ashe_bbox
stac_query <- rstac::stac_search(
q = stac_source,
collections = "sentinel-2-l2a",
bbox = ashe_bbox,
datetime = "2023-10-01/2023-10-07"
)
executed_stac_query <- rstac::get_request(stac_query)
executed_stac_query
signed_stac_query <- rstac::items_sign(
executed_stac_query,
rstac::sign_planetary_computer()
)
signed_stac_query
rstac::assets_download(signed_stac_query, c("B02"), output_dir = "E:/temp/")
Here is one resulting image:
The shapefile used is avaiable here : https://drive.google.com/file/d/1NNtnglNJ3b-E9d_Z3fmUoghMYxwRM947/view?usp=sharing
sf::gdal_utils("warp",...)
(unclear because you've posted some more code after that, but I can't see how that line would cause it) then check whatlcpri_url
is, because that's important, and looking a line or two above that seems to depend onexecuted_stac_query
which isn't in that code chunk at all. Write stuff like this in functions so you don't have unwanted things lying around and you don't have to repeat code all the time.