Mercurial > repos > ecology > otb_mean_shift_smoothing
comparison OTB_MeanShiftSmoothing.R @ 0:6c6e8b16dba6 draft
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/interpolation commit bd6f07bf29bad450af2e552f2524f23277edfef5-dirty
| author | ecology |
|---|---|
| date | Wed, 13 Mar 2024 19:12:46 +0000 |
| parents | |
| children | dd397752ad21 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:6c6e8b16dba6 |
|---|---|
| 1 # Run with Rscript ./OTB_MeanShiftSmoothing.R | |
| 2 #--file otb_band_math_test_input.txt | |
| 3 #--fOut float --fOutpos float --processingMemory 1024 --spatialR 5 --rangeR 15 | |
| 4 #--thresHold 0.1 --maxIter 100 --rangeRamp 0 --modeSearch False | |
| 5 #--outputType png | |
| 6 #--outputFormat download --outputData test1.png | |
| 7 | |
| 8 library("httr2") | |
| 9 library("jsonlite") | |
| 10 library("getopt") | |
| 11 | |
| 12 args <- commandArgs(trailingOnly = TRUE) | |
| 13 option_specification <- matrix(c( | |
| 14 "file", "i1", 1, "character", | |
| 15 "fOut", "i2", 1, "character", | |
| 16 "fOutpos", "i3", 1, "character", | |
| 17 "processingMemory", "i4", 1, "integer", | |
| 18 "spatialR", "i5", 2, "integer", | |
| 19 "rangeR", "i6", 2, "double", | |
| 20 "thresHold", "i7", 2, "double", | |
| 21 "maxIter", "i8", 2, "integer", | |
| 22 "rangeRamp", "i9", 2, "double", | |
| 23 "modeSearch", "i10", 1, "character", | |
| 24 "outputType", "i11", 1, "character", | |
| 25 "outputFormat", "i12", 1, "character", | |
| 26 "outputData", "o", 1, "character" | |
| 27 ), byrow = TRUE, ncol = 4) | |
| 28 options <- getopt(option_specification) | |
| 29 | |
| 30 file <- options$file | |
| 31 fout <- options$fOut | |
| 32 foutpos <- options$fOutpos | |
| 33 processing_memory <- options$processingMemory | |
| 34 spatialr <- options$spatialR | |
| 35 ranger <- options$rangeR | |
| 36 threshold <- options$thresHold | |
| 37 maxiter <- options$maxIter | |
| 38 rangeramp <- options$rangeRamp | |
| 39 modesearch <- options$modeSearch | |
| 40 output_type <- paste0("image/", options$outputType) | |
| 41 output_format <- options$outputFormat | |
| 42 output_data <- options$outputData | |
| 43 | |
| 44 cat("\n file: ", file) | |
| 45 cat("\n fout: ", fout) | |
| 46 cat("\n foutpos: ", foutpos) | |
| 47 cat("\n processing_memory: ", processing_memory) | |
| 48 cat("\n spatialr: ", spatialr) | |
| 49 cat("\n ranger: ", ranger) | |
| 50 cat("\n threshold: ", threshold) | |
| 51 cat("\n maxiter: ", maxiter) | |
| 52 cat("\n rangeramp: ", rangeramp) | |
| 53 cat("\n modesearch: ", modesearch) | |
| 54 cat("\n output_type: ", output_type) | |
| 55 cat("\n output_format: ", output_format) | |
| 56 | |
| 57 base_url <- "https://ospd.geolabs.fr:8300/ogc-api/" | |
| 58 execute <- "processes/OTB.MeanShiftSmoothing/execution" | |
| 59 get_status <- "jobs/" | |
| 60 get_result <- "/results" | |
| 61 | |
| 62 file_urls <- readLines(file, warn = FALSE) | |
| 63 | |
| 64 il_list <- lapply(file_urls, function(url) { | |
| 65 list("href" = url) | |
| 66 }) | |
| 67 | |
| 68 json_data <- list( | |
| 69 "inputs" = list( | |
| 70 "in" = il_list, | |
| 71 "fout" = fout, | |
| 72 "foutpos" = foutpos, | |
| 73 "ram" = processing_memory, | |
| 74 "spatialr" = spatialr, | |
| 75 "ranger" = ranger, | |
| 76 "thres" = threshold, | |
| 77 "maxiter" = maxiter, | |
| 78 "rangeramp" = rangeramp, | |
| 79 "modesearch" = modesearch | |
| 80 ), | |
| 81 "outputs" = list( | |
| 82 "fout" = list( | |
| 83 "format" = list( | |
| 84 "mediaType" = output_type | |
| 85 ), | |
| 86 "transmissionMode" = "reference" | |
| 87 ), | |
| 88 "foutpos" = list( | |
| 89 "format" = list( | |
| 90 "mediaType" = output_type | |
| 91 ), | |
| 92 "transmissionMode" = "reference" | |
| 93 ) | |
| 94 ) | |
| 95 ) | |
| 96 | |
| 97 make_response_body_readable <- function(body) { | |
| 98 hex <- c(body) | |
| 99 int_values <- as.integer(hex) | |
| 100 raw_vector <- as.raw(int_values) | |
| 101 readable_output <- rawToChar(raw_vector) | |
| 102 json_object <- jsonlite::fromJSON(readable_output) | |
| 103 return(json_object) | |
| 104 } | |
| 105 | |
| 106 tryCatch({ | |
| 107 # Request 1 | |
| 108 resp1 <- request(paste0(base_url, execute)) %>% | |
| 109 req_headers( | |
| 110 "accept" = "/*", | |
| 111 "Prefer" = "respond-async;return=representation", | |
| 112 "Content-Type" = "application/json" | |
| 113 ) %>% | |
| 114 req_body_json(json_data) %>% | |
| 115 req_perform() | |
| 116 response <- make_response_body_readable(resp1$body) | |
| 117 status_code1 <- resp1$status_code | |
| 118 if (status_code1 == 201) { | |
| 119 status <- "running" | |
| 120 attempt <- 1 | |
| 121 while (status == "running") { | |
| 122 # Request 2 | |
| 123 resp2 <- request(paste0(base_url, get_status, response$jobID)) %>% | |
| 124 req_headers( | |
| 125 "accept" = "application/json" | |
| 126 ) %>% | |
| 127 req_perform() | |
| 128 status_code2 <- resp2$status_code | |
| 129 if (status_code2 == 200) { | |
| 130 response2 <- make_response_body_readable(resp2$body) | |
| 131 cat("\n", response2$status) | |
| 132 if (response2$status == "successful") { | |
| 133 status <- "successful" | |
| 134 # Request 3 | |
| 135 resp3 <- request(paste0( | |
| 136 base_url, | |
| 137 get_status, response2$jobID, get_result | |
| 138 )) %>% | |
| 139 req_headers( | |
| 140 "accept" = "application/json" | |
| 141 ) %>% | |
| 142 req_perform() | |
| 143 status_code3 <- resp3$status_code | |
| 144 if (status_code3 == 200) { | |
| 145 response3 <- make_response_body_readable(resp3$body) | |
| 146 if (output_format == "download") { | |
| 147 options(timeout = 600) | |
| 148 download.file(response3$fout$href, | |
| 149 destfile = paste0("output1.", options$outputType), | |
| 150 mode = "wb" | |
| 151 ) | |
| 152 download.file(response3$foutpos$href, | |
| 153 destfile = paste0("output2.", options$outputType), | |
| 154 mode = "wb" | |
| 155 ) | |
| 156 } else if (output_format == "getUrl") { | |
| 157 writeLines(paste(response3$fout$href, response3$foutpos$href, | |
| 158 sep = "\n" | |
| 159 ), con = "output.txt") | |
| 160 } | |
| 161 } else if (status_code3 == 404) { | |
| 162 print("The requested URI was not found.") | |
| 163 } else if (status_code3 == 500) { | |
| 164 print("A server error occurred.") | |
| 165 } else { | |
| 166 print(paste("HTTP", status_code3, "Error:", resp3$status_message)) | |
| 167 } | |
| 168 } else if (response2$status == "failed") { | |
| 169 status <- "failed" | |
| 170 } | |
| 171 } else { | |
| 172 status <- "failed" | |
| 173 print(paste("HTTP", status_code2, "Error:", resp2$status_message)) | |
| 174 } | |
| 175 Sys.sleep(3) | |
| 176 } | |
| 177 print(status) | |
| 178 } else if (status_code1 == 400) { | |
| 179 print("A query parameter has an invalid value.") | |
| 180 } else if (status_code1 == 404) { | |
| 181 print("The requested URI was not found.") | |
| 182 } else if (status_code1 == 500) { | |
| 183 print("The requested URI was not found.") | |
| 184 } else { | |
| 185 print(paste("HTTP", status_code1, "Error:", resp1$status_message)) | |
| 186 } | |
| 187 }) |
