download.Rmd
The mapme.vegetation
package allows us to easily
download and work with Sentinel-2 (S2) Level 2A datasets. These datasets
represent bottom of atmosphere (BOA) reflectance values processed with
sen2cor.
The data is made available via the Earth Search
STAC API from AWS. mapme.vegetation
can be used to query
the server for a given spatio-temporal extent of interest and download
matching results. S2 download from AWS is free of cost and does not
require a user account. However, users should be aware that there is a
maximum of 500 items per query. In case the spatio-temporal extent will
lead to more items it is up to the user to split up the query into
suitable chunks. The package allows for querying the data without a
direct download in order to check the items returned.
library(mapme.vegetation)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
aoi = st_read(system.file("extdata", "exp_region_wgs.gpkg", package = "mapme.vegetation"), quiet = T)
bbox = st_bbox(aoi)
rundir = file.path(tempdir(), "mapme.vegetation")
dir.create(rundir, showWarnings = F)
items = download_aws(bbox = bbox,
after = "2017-05-01",
before = "2017-07-31",
timeframe = "full",
max.cloud = 40,
query_only = TRUE)
#> STAC version: 1.0.0-beta.2
#> Querying data for temporal window 2017-05-01/2017-07-31
#> The following items matched the specified spatiotemporal extent.
#> ###STACItemCollection
#> - matched feature(s): 11
#> - features (11 item(s) / 0 not fetched):
#> - S2B_38PLR_20170727_0_L2A
#> - S2A_38PLR_20170722_0_L2A
#> - S2B_38PLR_20170717_0_L2A
#> - S2A_38PLR_20170712_0_L2A
#> - S2B_38PLR_20170707_0_L2A
#> - S2A_38PLR_20170702_0_L2A
#> - S2A_38PLR_20170622_0_L2A
#> - S2A_38PLR_20170612_0_L2A
#> - S2A_38PLR_20170602_0_L2A
#> - S2A_38PLR_20170523_0_L2A
#> - S2A_38PLR_20170503_0_L2A
#> - assets:
#> AOT, B01, B02, B03, B04, B05, B06, B07, B08, B09, B11, B12, B8A, info, metadata, overview, SCL, thumbnail, visual, WVP
#> - item's fields:
#> assets, bbox, collection, geometry, id, links, properties, properties.sentinel:boa_offset_applied, stac_extensions, stac_version, type
The query resulted in 11 items matching the spatio-temporal extent
with a maximum cloud cover of 40%. We can proceed downloading the data
by setting query_only = FALSE
and supply additional
arguments of where to write the files
items = download_aws(bbox = bbox,
after = "2017-05-01",
before = "2017-07-31",
timeframe = "full",
max.cloud = 40,
outdir = rundir,
query_only = FALSE)
The above call with download the data sequentially. As a alternative
mapme.vegetation
allows to download the data via the aria2 download utility. User’s have
to install the program manually on their machine and indicate the path
to the aria2 binary. Additionall arguments are required to specify how
many connections to the server should be established to download
matching items in parallel. The function call below will download 4
items in parallel with a total of 4 connections per file to further
speed up the download. Also, the retry argument can be set to specify
the number of time the the function will retry to download items for
which the download failed.
items = download_aws(bbox = bbox,
after = "2017-05-01",
before = "2017-07-30",
timeframe = "full",
max.cloud = 40,
outdir = rundir,
query_only = FALSE,
use_aria = T,
aria_bin = "/usr/bin/aria2c",
max_concurrent = 4,
max_connections = 4,
retry = 1)
Note that in the above function calls
timeframe = "full"
. This way, the function will download
all the matching items between the after
and
before
dates (inclusive). If you want to request only
specific months but across years specify
timeframe = "seasonal"
to loop over the years and download
the matching time period. Note that the items are returned as a list
with each object representing one of the queried years.
items = download_aws(bbox = bbox,
after = "2017-05-01",
before = "2020-07-30",
timeframe = "seasonal",
max.cloud = 40,
query_only = TRUE)
#> STAC version: 1.0.0-beta.2
#> Querying data for temporal window 2017-05-01/2017-07-30
#> The following items matched the specified spatiotemporal extent.
#> ###STACItemCollection
#> - matched feature(s): 11
#> - features (11 item(s) / 0 not fetched):
#> - S2B_38PLR_20170727_0_L2A
#> - S2A_38PLR_20170722_0_L2A
#> - S2B_38PLR_20170717_0_L2A
#> - S2A_38PLR_20170712_0_L2A
#> - S2B_38PLR_20170707_0_L2A
#> - S2A_38PLR_20170702_0_L2A
#> - S2A_38PLR_20170622_0_L2A
#> - S2A_38PLR_20170612_0_L2A
#> - S2A_38PLR_20170602_0_L2A
#> - S2A_38PLR_20170523_0_L2A
#> - S2A_38PLR_20170503_0_L2A
#> - assets:
#> AOT, B01, B02, B03, B04, B05, B06, B07, B08, B09, B11, B12, B8A, info, metadata, overview, SCL, thumbnail, visual, WVP
#> - item's fields:
#> assets, bbox, collection, geometry, id, links, properties, properties.sentinel:boa_offset_applied, stac_extensions, stac_version, type
#> Querying data for temporal window 2018-05-01/2018-07-30
#> The following items matched the specified spatiotemporal extent.
#> ###STACItemCollection
#> - matched feature(s): 17
#> - features (17 item(s) / 0 not fetched):
#> - S2A_38PLR_20180727_0_L2A
#> - S2B_38PLR_20180722_0_L2A
#> - S2A_38PLR_20180717_0_L2A
#> - S2B_38PLR_20180712_0_L2A
#> - S2A_38PLR_20180707_0_L2A
#> - S2B_38PLR_20180702_0_L2A
#> - S2A_38PLR_20180627_0_L2A
#> - S2B_38PLR_20180622_0_L2A
#> - S2A_38PLR_20180617_0_L2A
#> - S2B_38PLR_20180612_0_L2A
#> - S2A_38PLR_20180607_0_L2A
#> - S2B_38PLR_20180602_0_L2A
#> - S2A_38PLR_20180528_0_L2A
#> - S2B_38PLR_20180523_0_L2A
#> - S2B_38PLR_20180513_0_L2A
#> - S2A_38PLR_20180508_0_L2A
#> - S2B_38PLR_20180503_0_L2A
#> - assets:
#> AOT, B01, B02, B03, B04, B05, B06, B07, B08, B09, B11, B12, B8A, info, metadata, overview, SCL, thumbnail, visual, WVP
#> - item's fields:
#> assets, bbox, collection, geometry, id, links, properties, properties.sentinel:boa_offset_applied, stac_extensions, stac_version, type
#> Querying data for temporal window 2019-05-01/2019-07-30
#> The following items matched the specified spatiotemporal extent.
#> ###STACItemCollection
#> - matched feature(s): 17
#> - features (17 item(s) / 0 not fetched):
#> - S2B_38PLR_20190727_0_L2A
#> - S2A_38PLR_20190722_0_L2A
#> - S2B_38PLR_20190717_0_L2A
#> - S2B_38PLR_20190707_0_L2A
#> - S2A_38PLR_20190702_0_L2A
#> - S2B_38PLR_20190627_0_L2A
#> - S2A_38PLR_20190622_0_L2A
#> - S2B_38PLR_20190617_0_L2A
#> - S2A_38PLR_20190612_0_L2A
#> - S2B_38PLR_20190607_0_L2A
#> - S2B_38PLR_20190528_0_L2A
#> - S2A_38PLR_20190523_0_L2A
#> - S2B_38PLR_20190518_0_L2A
#> - S2A_38PLR_20190513_0_L2A
#> - S2B_38PLR_20190508_1_L2A
#> - S2B_38PLR_20190508_0_L2A
#> - S2A_38PLR_20190503_0_L2A
#> - assets:
#> AOT, B01, B02, B03, B04, B05, B06, B07, B08, B09, B11, B12, B8A, info, metadata, overview, SCL, thumbnail, visual, WVP
#> - item's fields:
#> assets, bbox, collection, geometry, id, links, properties, properties.sentinel:boa_offset_applied, stac_extensions, stac_version, type
#> Querying data for temporal window 2020-05-01/2020-07-30
#> The following items matched the specified spatiotemporal extent.
#> ###STACItemCollection
#> - matched feature(s): 10
#> - features (10 item(s) / 0 not fetched):
#> - S2B_38PLR_20200711_0_L2A
#> - S2A_38PLR_20200706_0_L2A
#> - S2B_38PLR_20200701_0_L2A
#> - S2A_38PLR_20200626_0_L2A
#> - S2B_38PLR_20200621_0_L2A
#> - S2A_38PLR_20200616_0_L2A
#> - S2A_38PLR_20200527_0_L2A
#> - S2B_38PLR_20200522_0_L2A
#> - S2A_38PLR_20200517_0_L2A
#> - S2B_38PLR_20200512_0_L2A
#> - assets:
#> AOT, B01, B02, B03, B04, B05, B06, B07, B08, B09, B11, B12, B8A, info, metadata, overview, SCL, thumbnail, visual, WVP
#> - item's fields:
#> assets, bbox, collection, geometry, id, links, properties, properties.sentinel:boa_offset_applied, stac_extensions, stac_version, type
If you are only interested in specific bands you can reduce the files to download by specyfing the assets you wish to download.
items = download_aws(bbox = bbox,
after = "2017-05-01",
before = "2017-07-30",
timeframe = "full",
assets = c("B02", "B04", "B08", "SCL"),
max.cloud = 40,
query_only = F)