From f173a3571644d39f32db5e8c7b178e2a8877f6f6 Mon Sep 17 00:00:00 2001 From: almac2022 Date: Thu, 8 Jan 2026 10:51:28 -0800 Subject: [PATCH 1/2] add explanatory text and section structure --- vignettes/stac-spectral-indices.Rmd | 39 +++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/vignettes/stac-spectral-indices.Rmd b/vignettes/stac-spectral-indices.Rmd index 2cd970e..373921d 100644 --- a/vignettes/stac-spectral-indices.Rmd +++ b/vignettes/stac-spectral-indices.Rmd @@ -9,7 +9,7 @@ vignette: > ```{r, include = FALSE} knitr::opts_chunk$set( - echo = FALSE, + # echo = FALSE, collapse = TRUE, comment = "#>" ) @@ -19,6 +19,16 @@ knitr::opts_chunk$set( library(ngr) ``` +This vignette demonstrates computing NDVI from Landsat imagery using STAC APIs. The +`ngr_spk_stac_calc()` function provides a simple proof-of-concept approach using +`terra`. For production workflows involving time series, composites, or data cubes, +see the [gdalcubes](https://github.com/appelmar/gdalcubes) package. + +## Single Year Example + +Define an area of interest and query the Planetary Computer STAC catalog for +Landsat Collection 2 Level-2 imagery with low cloud cover. + ```{r rstac-query-2000} # Define an AOI from a bounding box (WGS84) bbox <- c( @@ -49,10 +59,20 @@ items <- stac_query |> rstac::items_fetch() |> rstac::items_sign_planetary_computer() +``` + +Compute NDVI for each returned scene using `ngr_spk_stac_calc()`, which reads the +red and NIR bands via GDAL's VSI interface and calculates `(NIR - red) / (NIR + red)`. + +```{r calc-ndvi-2000} ndvi_list <- items$features |> - purrr::map(ngr_spk_stac_calc, aoi = aoi, timing = TRUE) |> + purrr::map(ngr_spk_stac_calc, aoi = aoi, timing = TRUE) |> purrr::set_names(purrr::map_chr(items$features, "id")) +``` + +Create a mapview object for each NDVI raster with a red-yellow-green color scale. +```{r mapview-2000} mv <- purrr::imap( ndvi_list, function(x, nm) { @@ -76,16 +96,20 @@ mv <- purrr::imap( ``` +Combine all layers into a single interactive map. Use the layer control (top-left) +to toggle individual scenes on/off. + ```{r map1, out.width="100%"} -# combine into one map with toggleable layers purrr::reduce(mv, `+`) ``` +## Multi-Year Comparison -Create NDVI for multiple years - selecting the raster with the highest likely shrub/tree values. +Query multiple years and select the best scene per year based on vegetation coverage +(highest proportion of pixels with NDVI >= 0.4). This helps identify scenes with +peak growing-season conditions. ```{r multi-year, eval=TRUE} -#----------------------------------------------------------------------------------------------------- years <- seq(2000, 2025, by = 5) ndvi_by_year <- purrr::set_names(years) |> @@ -109,7 +133,6 @@ ndvi_by_year <- purrr::set_names(years) |> purrr::set_names(purrr::map_chr(items$features, "id")) }) -# ndvi_by_year[["2000"]] is your old ndvi_list for 2000 ndvi_best_by_year <- ndvi_by_year |> purrr::map(function(ndvi_list) { @@ -150,8 +173,10 @@ mv <- purrr::imap( ``` +Display the multi-year comparison. All year layers are visible by default; use the +layer control to toggle individual years on/off for comparison. + ```{r map2, out.width="100%", eval = TRUE} -# combine into one map with toggleable layers purrr::reduce(mv, `+`) ``` From f317b0ab63d6f53154121df166bd7c04ac3804e3 Mon Sep 17 00:00:00 2001 From: almac2022 Date: Thu, 8 Jan 2026 10:51:36 -0800 Subject: [PATCH 2/2] update documentation --- man/ngr_spk_stac_calc.Rd | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/man/ngr_spk_stac_calc.Rd b/man/ngr_spk_stac_calc.Rd index 039b8cc..1185f35 100644 --- a/man/ngr_spk_stac_calc.Rd +++ b/man/ngr_spk_stac_calc.Rd @@ -87,18 +87,16 @@ pixel sizes and coordinate reference systems, consider the \href{https://github.com/appelmar/gdalcubes}{gdalcubes} package. Key functions include: \itemize{ -\item \code{\link[gdalcubes:stac_image_collection]{gdalcubes::stac_image_collection()}}: Create an image collection -directly from STAC query results with automatic band detection and VSI -prefix handling. -\item \code{\link[gdalcubes:cube_view]{gdalcubes::cube_view()}}: Define spatiotemporal extent, resolution, -aggregation, and resampling method in a single specification. -\item \code{\link[gdalcubes:raster_cube]{gdalcubes::raster_cube()}}: Build a data cube from an image -collection, automatically reprojecting and resampling images to a common -grid. -\item \code{\link[gdalcubes:apply_pixel]{gdalcubes::apply_pixel()}}: Apply arithmetic expressions (e.g., +\item \code{stac_image_collection()}: Create an image collection directly from +STAC query results with automatic band detection and VSI prefix handling. +\item \code{cube_view()}: Define spatiotemporal extent, resolution, aggregation, +and resampling method in a single specification. +\item \code{raster_cube()}: Build a data cube from an image collection, +automatically reprojecting and resampling images to a common grid. +\item \code{apply_pixel()}: Apply arithmetic expressions (e.g., \code{"(nir - red) / (nir + red)"}) across all pixels. -\item \code{\link[gdalcubes:reduce_time]{gdalcubes::reduce_time()}}: Temporal reductions (mean, median, max, -etc.) to composite multi-date imagery. +\item \code{reduce_time()}: Temporal reductions (mean, median, max, etc.) to +composite multi-date imagery. } }