From 2b72849d430f604c1cabb7e51bca5f33b3415886 Mon Sep 17 00:00:00 2001 From: kwundram2602 <134778951+kwundram2602@users.noreply.github.com> Date: Fri, 19 Jan 2024 15:40:42 +0100 Subject: [PATCH 1/2] Evi process added --- R/api.R | 1 + R/processes.R | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/R/api.R b/R/api.R index c46acfb..56a6dec 100644 --- a/R/api.R +++ b/R/api.R @@ -314,5 +314,6 @@ addEndpoint = function() { Session$assignProcess(subtract) Session$assignProcess(multiply) Session$assignProcess(divide) + Session$assignProcess(evi) } diff --git a/R/processes.R b/R/processes.R index 211d0c1..06a11e2 100644 --- a/R/processes.R +++ b/R/processes.R @@ -651,6 +651,92 @@ ndvi <- Process$new( } ) +#EVI +evi <- Process$new( + id = "evi", + description = "Computes the Enhanced Vegetation Index (EVI). The EVI is computed as 2.5 * (NIR - RED) / ((NIR + 6*RED - 7.5*BLUE) + 1)", + categories = as.array("cubes"), + summary = "Enhanced Vegetation Index", + parameters = list( + Parameter$new( + name = "data", + description = "A data cube with bands.", + schema = list( + type = "object", + subtype = "raster-cube" + ) + ), + Parameter$new( + name = "nir", + description = "The name of the NIR band. Defaults to the band that has the common name nir assigned. For Sentinel 2 'B08' is used.", + schema = list( + type = "string" + ), + optional = FALSE + ), + Parameter$new( + name = "shortwl_nir", + description = "The name of the VNIR band which has a shorter wavelength than the nir band. For Sentinel 2 'B06' is used.", + schema = list( + type = "string" + ), + optional = FALSE + ), + Parameter$new( + name = "red", + description = "The name of the red band. Defaults to the band that has the common name red assigned. For Sentinel 2 'B04' is used.", + schema = list( + type = "string" + ), + optional = FALSE + ), + Parameter$new( + name = "blue", + description = "The name of the blue band. Defaults to the band that has the common name blue assigned. For Sentinel 2 'B02' is used.", + schema = list( + type = "string" + ), + optional = FALSE + ), + Parameter$new( + name = "target_band", + description = "By default, the dimension of type bands is dropped. To keep the dimension specify a new band name in this parameter so that a new dimension label with the specified name will be added for the computed values.", + schema = list( + type = "string" + ), + optional = TRUE + ) + ), + returns = eo_datacube, + operation = function(data, nir = "nir",shortwl_nir="shortwl_nir", red = "red", blue = "blue", target_band = NULL, job){ + # Function to ensure band names are properly formatted + format_band_name <- function(band) { + if (grepl("^B\\d{2}$", band, ignore.case = TRUE)) { + return(toupper(band)) + } else { + return(band) + } + } + + # Apply formatting to band names + nir_formatted <- format_band_name(nir) + red_formatted <- format_band_name(red) + blue_formatted <- format_band_name(blue) + shortwl_nir_formatted <- format_band_name(shortwl_nir) + + # Construct the NDVI calculation formula + evi_formula <- sprintf("2.5*((%s-%s)/(%s+6*%s-7.5*%s)+1)", nir_formatted, red_formatted, nir_formatted, shortwl_nir_formatted,blue_formatted) + + # Apply the NDVI calculation + cube <- gdalcubes::apply_pixel(data, evi_formula, names = "EVI", keep_bands = FALSE) + + # Log and return the result + message("EVI calculated ....") + message(gdalcubes::as_json(cube)) + return(cube) + } +) + #' rename_dimension rename_dimension <- Process$new( From afc85b6eba64a088814aca660813d688bc7d4a28 Mon Sep 17 00:00:00 2001 From: kwundram2602 <134778951+kwundram2602@users.noreply.github.com> Date: Sun, 21 Jan 2024 15:27:35 +0100 Subject: [PATCH 2/2] change evi formula --- R/processes.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/processes.R b/R/processes.R index 06a11e2..5e8a551 100644 --- a/R/processes.R +++ b/R/processes.R @@ -725,7 +725,7 @@ evi <- Process$new( shortwl_nir_formatted <- format_band_name(shortwl_nir) # Construct the NDVI calculation formula - evi_formula <- sprintf("2.5*((%s-%s)/(%s+6*%s-7.5*%s)+1)", nir_formatted, red_formatted, nir_formatted, shortwl_nir_formatted,blue_formatted) + evi_formula <- sprintf("2.5*((%s-%s)/(%s+6*(%s)-7.5*(%s))+1)", nir_formatted, red_formatted, nir_formatted, shortwl_nir_formatted,blue_formatted) # Apply the NDVI calculation cube <- gdalcubes::apply_pixel(data, evi_formula, names = "EVI", keep_bands = FALSE)