Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for RDS files #72

Closed
wants to merge 9 commits into from
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ vignettes/*.pdf
#mac
.DS_Store

#results from backend jobs
/results
# test workspace
/test_workspace

# jobInfos from backend jobs
/jobs
170 changes: 167 additions & 3 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,187 @@ NULL
job = newJob$run()
format = job$output


#TODO: IMPLEMENT CHECK, IF job$results == datacube
# if Datacube: write in desired format as is
# if data.frame (NetCDF): write whole data.frame as NetCDF

# TODO: harmonize whole file save process do reduce redundancy

if (class(format) == "list") {
if (format$title == "Network Common Data Form") {
file = gdalcubes::write_ncdf(job$results)

# if no error occurs, job$results == datacube
tryCatch(
{
file = gdalcubes::write_ncdf(job$results)
break
},
error = function(error)
{
message('An Error Occurred.')
message(toString(error))
},
warning = function(warning) {
message('A Warning Occurred')
message(toString(warning))
})
# when error then proceed with the "data.frame" variant
tryCatch(
{

# convert data.frame in "spatial dataframe"
data = sf::st_as_sf(job$results)

file = base::tempfile()

# save whole file
sf::st_write(data, file, driver = "netCDF")

res$status = 200
res$body = readBin(file, "raw", n = file.info(file)$size)

content_type = plumber:::getContentType(tools::file_ext(file))
res$setHeader("Content-Type", content_type)

return(res)
},
error = function(error)
{
message('An Error Occurred.')
message(toString(error))
},
warning = function(warning) {
message('A Warning Occurred')
message(toString(warning))
})
}
else if (format$title == "GeoTiff") {
file = gdalcubes::write_tif(job$results)
}
else if (format$title == "R Data Set")
{
# THINK ABOUT PRETTIER SOLUTION
tryCatch(
{
job$results = gdalcubes::as_json(job$results)
message(class(job$results))
},
error = function(error)
{
message('An Error Occurred. Passed data was not of type "datacube".')
message(error)
},
warning = function(warning) {
message('A Warning Occurred')
message(warning)
},
finally = {

# perform rest of the result
temp = base::tempfile()
base::saveRDS(job$results, temp)
res$status = 200
res$body = readBin(temp, "raw", n = file.info(temp)$size)

content_type = plumber:::getContentType(tools::file_ext(temp))
res$setHeader("Content-Type", content_type)

return(res)
})
}

else {
throwError("FormatUnsupported")
}
}
else {
if (format == "NetCDF") {
file = gdalcubes::write_ncdf(job$results)

# if no error occurs, job$results == datacube
tryCatch(
{
file = gdalcubes::write_ncdf(job$results)
break
},
error = function(error)
{
message('An Error Occurred.')
message(toString(error))
},
warning = function(warning) {
message('A Warning Occurred')
message(toString(warning))
})
# when error then proceed with the "data.frame" variant
tryCatch(
{

# convert data.frame in "spatial dataframe"
data = sf::st_as_sf(job$results)

file = base::tempfile()

# save whole file
sf::st_write(data, file, driver = "netCDF")

res$status = 200
res$body = readBin(file, "raw", n = file.info(file)$size)

content_type = plumber:::getContentType(tools::file_ext(file))
res$setHeader("Content-Type", content_type)

return(res)
},
error = function(error)
{
message('An Error Occurred.')
message(toString(error))
},
warning = function(warning) {
message('A Warning Occurred')
message(toString(warning))
})


}
else if (format == "GTiff") {
file = gdalcubes::write_tif(job$results)
}

else if (format == "RDS") {

# THINK ABOUT PRETTIER SOLUTION
tryCatch(
{
job$results = gdalcubes::as_json(job$results)
message(class(job$results))
},
error = function(error)
{
message('An Error Occurred. Passed data was not of type "datacube".')
message(error)
},
warning = function(warning) {
message('A Warning Occurred')
message(warning)
},
finally = {

# perform rest of the result
temp = base::tempfile()
base::saveRDS(job$results, temp)
res$status = 200
res$body = readBin(temp, "raw", n = file.info(temp)$size)

content_type = plumber:::getContentType(tools::file_ext(temp))
res$setHeader("Content-Type", content_type)

return(res)
})

}

else {
throwError("FormatUnsupported")
}
Expand Down Expand Up @@ -314,5 +477,6 @@ addEndpoint = function() {
Session$assignProcess(subtract)
Session$assignProcess(multiply)
Session$assignProcess(divide)

Session$assignProcess(train_model)
Session$assignProcess(predict_model)
}
Loading
Loading