diff aquainfra_ogc_api_processes.R @ 1:84557c0b3eef draft

planemo upload for repository https://github.com/AquaINFRA/tools-ecology/tree/master commit 3989415663771a85b3a0be6c02308994ac4adc85
author ecology
date Wed, 23 Oct 2024 22:35:12 +0000
parents 0117fab93b87
children e1f41a25ff24
line wrap: on
line diff
--- a/aquainfra_ogc_api_processes.R	Mon Oct 14 12:22:48 2024 +0000
+++ b/aquainfra_ogc_api_processes.R	Wed Oct 23 22:35:12 2024 +0000
@@ -5,9 +5,7 @@
 cat("start generic wrapper service \n")
 
 remove_null_values <- function(x) {
-  # Check if the input is a list
   if (is.list(x)) {
-    # Remove NULL values and apply the function recursively to sublists
     x <- lapply(x, remove_null_values)
     x <- x[!sapply(x, is.null)]
   }
@@ -18,11 +16,10 @@
   con <- file("inputs.json", "r")
   lines <- readLines(con)
   close(con)
-  
+
   json_string <- paste(lines, collapse = "\n")
   json_data <- fromJSON(json_string)
-  
-  # Remove NULL values from json_data
+
   cleaned_json_data <- remove_null_values(json_data)
   return(cleaned_json_data$conditional_process)
 }
@@ -38,9 +35,10 @@
 
 getOutputs <- function(inputs, output, server) {
   url <-
-    paste(paste(server, "/processes/", sep = ""),
+    paste(paste(server, "processes/", sep = ""),
           inputs$select_process,
           sep = "")
+  print(url)
   request <- request(url)
   response <- req_perform(request)
   responseBody <- parseResponseBody(response$body)
@@ -81,10 +79,6 @@
   
   cat("\n Process executed")
   cat("\n status: ", response$status_code)
-  #if ( process == "barplot-trend-results") {
-  #  process = "batplot-trend-results"
-  #}
-  #href <- parseResponseBody(response$body)$outputs[[gsub("-", "_", process)]]$href
   jobId <- parseResponseBody(response$body)$jobID
   
   return(jobId)
@@ -115,20 +109,14 @@
   return(response)
 }
 
-# Recursive function to search for href in a nested list
 findHref <- function(obj) {
-  hrefs <- c()  # Initialize an empty vector to store hrefs
-  
+  hrefs <- c()
   if (is.list(obj)) {
-    # If the object is a list, loop through its elements
     for (name in names(obj)) {
       element <- obj[[name]]
-      
       if (is.list(element)) {
-        # Recursively search if the element is another list
         hrefs <- c(hrefs, findHref(element))
       } else if (name == "href") {
-        # If the element has a name "href", capture its value
         hrefs <- c(hrefs, element)
       }
     }
@@ -154,16 +142,11 @@
         if (result$status_code == 200) {
           resultBody <- parseResponseBody(result$body)
           print(resultBody)
-          
-          # Call the recursive function to find all hrefs
           hrefs <- findHref(resultBody)
           
           if (length(hrefs) > 0) {
-            # Collapse the URLs with a newline
             urls_with_newline <- paste(hrefs, collapse = "\n")
             print(urls_with_newline)
-            
-            # Write the URLs to a file
             con <- file(outputData, "w")
             writeLines(urls_with_newline, con = con)
             close(con)
@@ -190,8 +173,6 @@
   }
 }
 
-
-
 saveResult <- function(href, outputData) {
   con <- file(outputData, "w")
     writeLines(href, con = con)
@@ -206,7 +187,6 @@
 
 print("--> Retrieve parameters")
 inputParameters <- getParameters()
-#print(inputParameters)
 print("--> Parameters retrieved")
 
 args <- commandArgs(trailingOnly = TRUE)
@@ -217,54 +197,24 @@
 print("--> Outputs retrieved")
 
 print("--> Parse inputs")
+
 convertedKeys <- c()
+
 for (key in names(inputParameters)) {
   if (is.character(inputParameters[[key]]) &&
       (endsWith(inputParameters[[key]], ".dat") ||
        endsWith(inputParameters[[key]], ".txt"))) {
     con <- file(inputParameters[[key]], "r")
     url_list <- list()
-    #while (length(line <- readLines(con, n = 1)) > 0) {
-    #  if (is_url(line)) {
-    #    url_list <- c(url_list, list(list(href = trimws(line))))
-    #  }
-    #}
+
     con <- file(inputParameters[[key]], "r")
     lines <- readLines(con)
-    print("--------------------------------------------------------------------1")
     print(length(lines))
     close(con)
-    if (!length(lines) > 1 && endsWith(lines, ".jp2") && startsWith(lines, "https")) {
-      print("--------------------------------------------------------------------2")
-      tmp <- list()
-      tmp$href <- lines
-      tmp$type <- "image/jp2"
-      inputParameters[[key]] <- tmp
-    }
-    else if (!length(lines) > 1 && endsWith(lines, ".zip") && startsWith(lines, "https")) {
-      print("--------------------------------------------------------------------3")
-      json_string <- paste(lines, collapse = "\n")
-      inputParameters[[key]] <- json_string
-    } else if (!length(lines) > 1 && (endsWith(lines, ".xlsx") || endsWith(lines, ".csv") || grepl("f=csv", lines)) && startsWith(lines, "https")) {
-      print("--------------------------------------------------------------------4")
-      json_string <- paste(lines, collapse = "\n")
-      inputParameters[[key]] <- json_string
-    } else if (inputParameters$select_process == "plot-image" ||
-               inputParameters$select_process == "reproject-image") {
-      print("--------------------------------------------------------------------5")
-      tmp <- list()
-      tmp$href <- lines
-      tmp$type <- "image/tiff; application=geotiff"
-      if (inputParameters$select_process == "reproject-image") {
-        tmp$type <- "image/tiff; subtype=geotiff"
-      }
-      inputParameters[[key]] <- tmp
-    } else {
-      print("-----------------------------------6")
-      json_string <- paste(lines, collapse = "\n")
-      json_data <- fromJSON(json_string)
-      inputParameters[[key]] <- json_data
-    }
+
+    json_string <- paste(lines, collapse = "\n")
+    inputParameters[[key]] <- json_string
+
     convertedKeys <- append(convertedKeys, key)
   }
   else if (grepl("_Array_", key)) {
@@ -296,24 +246,15 @@
     }
     
     inputParameters[[key]] <- convertedValues
-    print("-------------------------")
-    print(convertedValues)
-    print("-------------------------")
     convertedKeys <- append(convertedKeys, convertedKey)
   } else {
-    print("-------------------------")
-    print(key)
-    print(inputParameters[[key]])
     if (!is.null(inputParameters[[key]])) {
       convertedKeys <- append(convertedKeys, key)
     }
-    print("-------------------------")
-    
   }
 }
 print(inputParameters)
 names(inputParameters) <- convertedKeys
-#print(inputParameters)
 print("--> Inputs parsed")
 
 print("--> Prepare process execution")