Plumber: raster as input

Dear All,
I have developed a model with tidymodels that I would like to make available through plumber.

This model in order to perform prediction needs the following parameters 1 raster, 2 dates, 2 characters and 2 number variables.

I have found a way to input the dates, characters and numbers into the api, instead what I find considerable difficulty is the raster.

Furthermore, this model allows the calculation of raster that i would like to provide as a tif at the end of the api call.

Have any of you ever had experience with raster calculation in plumber?

Also, I cannot find any documentation on the internet about it

Use a file type parameter, you will get the binary content of the uploaded raster.

See : Annotations reference • plumber

You could use the tiff serializer, depending on the actual output of your model.

1 Like

Dear @meztez
Thanks for your suggestion.

I have tried to build the API but i have still have problem, i will report the code and error output.

library(plumber)
library(tidyverse)
library(readxl)
library(raster)
library(sp)
library(sf)
library(tidyverse)
library(nasapower)
library(tidymodels)
library(xgboost)
options("plumber.port" = 8000)

final_model_xgb<-readRDS("model/model.RDS")

#* @apiTitleAPI
#* @apiVersion 1.0.1
#* @tag API
#* @param firststring:string
#* @param seconstring:string 
#* @param thirdstring:string
#* @param raster:file 
#* @param fourthstring:string 
#* @param firstnumber:number
#* @param secondnumber:number 
#* @post /prediction
#* @serializer tiff

function(firststring, seconstring, thirdstring, raster, fourthstring, firstnumber, secondnumber){

    print(firststring)
    print(seconstring)
    print(thirdstring)
    print(fourthstring)
    print(firstnumber)
    print(secondnumber)

    raster<-readBin(raster[1], 'double')

    print(raster)

}

The error output is the following

<simpleError in readBin(raster[1], "double"): invalid connection>

What can i do to overpass this problem?

raster[1] is not a valid connection to a file, it is a raw vector in memory. The raw vector is the binary representation of the file you submit to your API.

put a browser() instruction just before raster<-... and play with it interactively. You will see what I mean.

Have a good day.

1 Like

I have tried several ways how to figure it out by using the browser function, but i am still blocked

This is the code I am using, I reduced it to make numerous attempts.

#* @apiTitle API
#* @apiVersion 1.0.1
#* @tag API
#* @param raster:binary
#* @post /prediction
#* @serializer tiff

function(raster){

  raster<-browser(readBin(raster, 'raw'))

  print(raster)

}

and i have the follow output

<simpleError in readBin(raster, "raw"): invalid connection>

Moreover i am attaching the follow picture to provide more information about my problem.

#* @apiTitle API
#* @apiVersion 1.0.1
#* @tag API

library(raster)

#* @param raster:binary
#* @post /prediction
#* @serializer tiff
function(raster){
  
  # Before using a function, it is good to
  # check its documentation using ?<function>
  # Here ?browser
  browser()
  
  filename <- names(raster)
  content <- raster[[1]]
  file <- tempfile(fileext = filename)
  on.exit(unlink(file), add = TRUE)
  writeBin(content, file)
  
  r <- raster::raster(file)
  # plot using breaks.
  plot(
    r, 
    breaks = c(300, 350, 400, 450), 
    col = terrain.colors(3),
    main="Digital Surface Model (DSM)\n NEON Harvard Forest Field Site"
  )
  
}
1 Like

Dear @meztez it worked perfectly and now the plumber API is running locally.

Thanks again for the suggestions.

But now I have a new problem, and I apologize for bothering you again.

I am trying to submit the raster file by using httr package, but I am finding some difficulties to insert the raster as body of the API Call

I am using this code but it is not working for me

prediction<-raster(POST("http://localhost:8000/prediction?",
     upload_file("C:/Users/Utente/Desktop/2022-05-10.tif")))

Could you help me for the last time? :grinning:

dont forget to extract the post response content using httr content function.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.