Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to keep scene classification layer when using get_sentinel2_imagery #71

Closed
laurenkwick opened this issue Jun 24, 2024 · 4 comments · Fixed by #72
Closed

How to keep scene classification layer when using get_sentinel2_imagery #71

laurenkwick opened this issue Jun 24, 2024 · 4 comments · Fixed by #72

Comments

@laurenkwick
Copy link

laurenkwick commented Jun 24, 2024

Hi!

This is a great library and has been extremely helpful to work with geospatial data. I was wondering if there was a good way to copy and adjust rsi band mapping objects?

For sentinel-2 data, I would like to preserve the "scl" band. In the rsi band mapping object rsi::sentinel2_band_mapping$aws_v1, the "scl" band is defined in the mask_band attribute, but it is not in the band mapping vector. I have a scenario where I do not want to apply the masking function in the get_sentinel2_imagery function, but I still want to save the "scl" band as an asset. Is it possible to do this?

This is what the band mapping object currently looks like:

An rsi band mapping object with attributes:
names mask_band mask_function stac_source collection_name query_function download_function class

    blue  coastal    green      nir    nir08    nir09      red rededge1 rededge2 rededge3   swir16   swir22 
     "B"      "A"      "G"      "N"     "N2"     "WV"      "R"    "RE1"    "RE2"    "RE3"     "S1"     "S2" 

And this is what I want it to look like:

An rsi band mapping object with attributes:
names mask_band mask_function stac_source collection_name query_function download_function class

    blue  coastal    green      nir    nir08    nir09      red rededge1 rededge2 rededge3   swir16   swir22  scl
     "B"      "A"      "G"      "N"     "N2"     "WV"      "R"    "RE1"    "RE2"    "RE3"     "S1"     "S2"   "SCL"

I cannot add another element to the band mapping object without it removing all of the other attributes (i.e. stac_source, collection_name, etc.). So right now, I'm using this workaround where I create a custom named vector and then manually define the parameters that would look normally be looking at the asset_names argument:

  s2_vals <- c("B","A","G","N","N2","WV","R","RE1","RE2","RE3","S1","S2","SCL")
  names(s2_vals) <- c("blue", "coastal", "green", "nir", "nir08", "nir09", "red", 
                      "rededge1", "rededge2", "rededge3", "swir16", "swir22", "scl")
  s2_asset <- rsi::sentinel2_band_mapping$aws_v1
  
  # Retrieve raster data
  # Add a small buffer to aoi layer to get all pixels within the aoi boundary after reprojection (see rsi::get_stac_data help text)
  image <- rsi::get_sentinel2_imagery(
    aoi = sf::st_buffer(aoi_search, dist=50), 
    start_date = dates[1],
    end_date = dates[2],
    asset_names = s2_vals,
    stac_source = attr(s2_asset, "stac_source"),
    collection = attr(s2_asset, "collection_name"),
    query_function = attr(s2_asset, "query_function"),
    sign_function = attr(s2_asset, "sign_function"),
    composite_function = NULL,
    output_filename = tempfile(pattern="S2img", tmpdir=tempdir(), fileext=".tif")
)

I just wanted to see if there was a better way of adding the "scl" band as an asset at this time.
Thank you!

@mikemahoney218
Copy link
Collaborator

Thanks! This will be fixed by #72

@mikemahoney218
Copy link
Collaborator

(or, to answer your actual question -- there isn't a better way yet, but soon in dev running c(band_map, scl = scl) will get you there)

@mikemahoney218
Copy link
Collaborator

mikemahoney218 commented Jun 24, 2024

This is now in main:

c(rsi::sentinel2_band_mapping$planetary_computer_v1, scl = "scl")
#> An rsi band mapping object with attributes:
#> mask_band mask_function stac_source collection_name query_function class scl_name download_function sign_function names
#> 
#>   B01   B02   B03   B04   B05   B06   B07   B08   B8A   B09   B11   B12   scl 
#>   "A"   "B"   "G"   "R" "RE1" "RE2" "RE3"   "N"  "N2"  "WV"  "S1"  "S2" "scl"

Created on 2024-06-24 with reprex v2.1.0

If you don't name the additional assets, the asset name gets used as the output band name:

identical(
  c(rsi::sentinel2_band_mapping$planetary_computer_v1, scl = "scl"),
  c(rsi::sentinel2_band_mapping$planetary_computer_v1, "scl")
)
#> [1] TRUE

Created on 2024-06-24 with reprex v2.1.0

Thanks again for the report!

Copy link

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants