【发布时间】:2020-12-26 18:28:46
【问题描述】:
我正在尝试使用管道工 R 包将函数包装到 REST API 中。作为 Input ,函数接受 shapefile 并在转换后以 .shp 格式和 GeoJSON 格式返回 shapefile。现在在装饰器的帮助下,我需要将其增强为 Web 服务。
R 文件:
#* Spatial Polygon Object
#* @param pathtoshpfile:character Path to Shapefile with Name
#* @param design:character one or two
#* @post /sprayermap
function(pathtoshpfile, design = c("one", "two")) {
# library
require(rgeos)
require(sp)
require(rgdal)
require(raster)
#Importing Shapefile
a_shape <- raster::shapefile(pathtoshpfile)
if (class(a_shape) == "SpatialPolygonsDataFrame") {
if (design == "one") {
a_shape <- tryCatch (
rgeos::gBuffer(a_shape, byid = TRUE, width = 0),
error = function(err) {
return(paste("sprayer map : ", err))
}
)
sprayer_map <- tryCatch (
aggregate(a_shape, "Rx"),
error = function(err) {
return(paste("sprayer map : ", err))
}
)
sprayer_map@data$Rx <- as.integer(sprayer_map@data$Rx)
raster::shapefile(sprayer_map, filename = "field_sprayer_map", overwrite =
TRUE)
rgdal::writeOGR(
sprayer_map,
dsn = "field_sprayer_map.GeoJSON",
layer = "geojson",
driver = "GeoJSON",
overwrite_layer = TRUE
)
return(paste0("SpatialPolygonsDataFrame"))
} else {
return(paste0("design two"))
}
} else {
return(paste0("Please provide spatial polygon object !"))
}
}
管道工部分:
library(plumber)
# 'plumber.R' is the location of the file shown above
pr("plumber.R") %>%
pr_run(port=8000)
#############################
curl "http://127.0.0.1:8000/sprayermap?pathtoshpfile=/path/to/directory/test.shp&design=one"
基于我对创建 API 的有限理解,我创建了上面的脚本并得到了以下错误。
错误:“curl”http://127.0.0.1:8000/sprayermap 中有意外的字符串常量?
虽然普通的 R 函数脚本效果很好
寻找如何使用装饰器将上述函数转换为输入和输出都是 shapefile 的 Restful API 的指导。 测试形状文件:Test Shape File
【问题讨论】:
-
你试过
@get /sprayermap吗? -
@inscaven 是的,同样的问题错误:“curl 中出现意外的字符串常量”
标签: r rest r-raster rgdal plumber