You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey @mikemahoney218! So, the HLS data (https://hls.gsfc.nasa.gov/) are a game changer for getting a high return rate of images and building lovely mosaics - the provided cloud masks are also excellent.
However, it is a little different compared to working with other STAC collections as there are two distinct collections and these have different band numbers and mappings.
The following reprex works great but I wanted to get your thoughts on a few things:
Should we add more high-level get_stac_data functions to the package?
And if so, how would we tackle this?
just offer the two collections as get_hsl_ss_imagery and get_hsl_sl_imagery
Three versions? the two above and get_hsl_imagery to wrap both of the others?
Or does adding all of this stuff become burdensome, and are better of just chucking something like the following reprex in a vignette?
I'm happy to write a PR but wanted to get your thoughts first.
So yeah let me know your thoughts and as always, thanks for all your fab work on this package!
# Description: This script is used to download HLS data from NASA's Earthdata# STAC API and build a cloud-free composite image.aoi<-sf::st_bbox(
c(
xmin=35.401471, ymin=-3.352265,
xmax=35.492570, ymax=-3.261740
),
crs="EPSG:4326"
) |>sf::st_as_sfc()
# band mapping for HLS SL datahlssl_band_mapping<- c(
B01="A", B02="B", B03="G", B04="R",
B05="N2", B06="S1", B07="S2",
B09="C", B10="T1",
B11="T2"
)
# band mapping for HLS SS datahlsss_band_mapping<- c(
B01="A", B02="B", B03="G", B04="R",
B05="RE1", B06="RE2", B07="RE3",
B08="N", B8A="N2", B09="WV",
B10="C", B11="S1", B12="S2"
)
Mask function for HLS data
raster a SpatRaster object. The mask asset of a given HLS item.
bits a numeric vector. The bits to be used for masking.
a SpatRaster with logical values.
This function is used to mask HLS data using the Fmask band.
the bit values relate to the folloeing feautres in the Fmask band:
0: Cirrus, 1: Cloud, 2: Adjacent to cloud/shadow, 3: Cloud shadow,
4: Snow/ice, 5: Water, 6-7: aerosol level
The defaults are quite agressive but do a good job of removing clouds and
shadows.
Cloud cover filter for HLS data
A function factory creating a rsi query function that filters HLS data
based on cloud cover proportion.
eo_cloud_cover a numeric value. The maximum cloud cover proportion to
filter HLS data by. must be between 0 and 100.
a function that can be used as the query_function argument in
rsi::get_stac_data.
hls_cloud_cover_filter<-function(eo_cloud_cover=25) {
if (!rlang::is_bare_numeric(eo_cloud_cover)) {
rlang::abort("`eo_cloud_cover`` must be a numeric value",
class="eo_cloud_cover_not_numeric"
)
}
if (rlang::is_false(eo_cloud_cover>=0&&eo_cloud_cover<=100)) {
rlang::abort("`eo_cloud_cover` must be between 0 and 100",
class="eo_cloud_cover_out_of_range"
)
}
function(bbox, stac_source, collection, start_date, end_date, ...) {
request<-rstac::stac(stac_source) |>rstac::stac_search(
collections=collection,
bbox=bbox[1:4],
datetime= paste(start_date, end_date, sep="/")
)
its<-rstac::items_fetch(rstac::post_request(request)) |>rstac::items_filter(filter_fn=function(x) {
x$properties$`eo:cloud_cover`<=eo_cloud_cover
})
if (rstac::items_length(its) ==0) {
cli::cli_abort("No items found with cloud cover <= {eo_cloud_cover}")
} else {
cli::cli_alert_info(
"Found {rstac::items_length(its)} items with cloud cover < {eo_cloud_cover}"
)
}
return(its)
}
}
# GDAL options for downloading HLS data - you must set the EARTHDATA_TOKEN# environment variable to your Earthdata tokennasa_ed_gdal_config_options<- c(rsi::rsi_gdal_config_options(),
GDAL_HTTP_HEADERS=glue::glue(
"Authorization: Bearer {Sys.getenv('EARTHDATA_TOKEN')}"
)
)
# shared argstarget_dir<-"hls-tz-24"if (!dir.exists(target_dir)) dir.create(target_dir)
start_date<-"2024-01-01"end_date<-"2024-02-29"mask_band<-"Fmask"# get Landsat datahlssl_paths<-rsi::get_stac_data(
aoi=aoi,
start_date=start_date,
end_date=end_date,
asset_names=hlssl_band_mapping,
stac_source="https://cmr.earthdata.nasa.gov/cloudstac/LPCLOUD/",
collection="HLSL30.v2.0",
query_function= hls_cloud_cover_filter(eo_cloud_cover=25),
output_filename= file.path(target_dir, "TZ-HLS-SL-cc25.tif"),
mask_band=mask_band,
mask_function=hls_mask_fun,
composite_function=NULL,
gdal_config_options=nasa_ed_gdal_config_options
)
#> ℹ Found 2 items#> with cloud cover < 25# get Sentinel-2 datahlsss_paths<-rsi::get_stac_data(
aoi=aoi,
start_date=start_date,
end_date=end_date,
asset_names=hlsss_band_mapping,
stac_source="https://cmr.earthdata.nasa.gov/cloudstac/LPCLOUD/",
collection="HLSS30.v2.0",
query_function= hls_cloud_cover_filter(eo_cloud_cover=25),
output_filename= file.path(target_dir, "TZ-HLS-SS-cc25.tif"),
mask_band=mask_band,
mask_function=hls_mask_fun,
composite_function=NULL,
gdal_config_options=nasa_ed_gdal_config_options
)
#> | |======================================================================| 100%#> ℹ Found 4 items#> with cloud cover < 25#> Warning: Failed to download HLS.S30.T36MYB.2024036T075121.v2.0 from#> 2024-02-05T08:10:50.306Z# get names for unique and shared bands between sentinel and landasathlsss_unique<- setdiff(hlsss_band_mapping, hlssl_band_mapping)
hlssl_unique<- setdiff(hlssl_band_mapping, hlsss_band_mapping)
hls_shared<- intersect(hlssl_band_mapping, hlsss_band_mapping)
Build a mosaic from a list of rasters and specified band names
param rlist a character vector of raster file paths
param bands a character vector of band names to extract from the rasters
return a SpatRaster object
Hey @mikemahoney218! So, the HLS data (https://hls.gsfc.nasa.gov/) are a game changer for getting a high return rate of images and building lovely mosaics - the provided cloud masks are also excellent.
However, it is a little different compared to working with other STAC collections as there are two distinct collections and these have different band numbers and mappings.
The following reprex works great but I wanted to get your thoughts on a few things:
get_stac_data
functions to the package?get_hsl_ss_imagery
andget_hsl_sl_imagery
get_hsl_imagery
to wrap both of the others?I'm happy to write a PR but wanted to get your thoughts first.
So yeah let me know your thoughts and as always, thanks for all your fab work on this package!
Mask function for HLS data
raster a SpatRaster object. The mask asset of a given HLS item.
bits a numeric vector. The bits to be used for masking.
a SpatRaster with logical values.
This function is used to mask HLS data using the Fmask band.
the bit values relate to the folloeing feautres in the Fmask band:
0: Cirrus, 1: Cloud, 2: Adjacent to cloud/shadow, 3: Cloud shadow,
4: Snow/ice, 5: Water, 6-7: aerosol level
The defaults are quite agressive but do a good job of removing clouds and
shadows.
Cloud cover filter for HLS data
A function factory creating a rsi query function that filters HLS data
based on cloud cover proportion.
eo_cloud_cover a numeric value. The maximum cloud cover proportion to
filter HLS data by. must be between 0 and 100.
a function that can be used as the query_function argument in
rsi::get_stac_data.
Build a mosaic from a list of rasters and specified band names
param rlist a character vector of raster file paths
param bands a character vector of band names to extract from the rasters
return a SpatRaster object
Created on 2024-10-16 with reprex v2.1.1
The text was updated successfully, but these errors were encountered: