我试图使用管道工R包将一个函数包装到REST中。作为输入,函数接受一个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函数脚本运行良好
为了获得指导,如何使用装饰器将上述函数转换为Restful,其中输入和输出都是shapefile。测试形状文件:测试形状文件
发布于 2020-09-08 12:47:59
我相信你的功能设置正确。以下curl调用将帮助您解决问题:
curl -X POST "http://127.0.0.1:8000/sprayermap?pathtoshpfile=../Downloads/jnk/test.shp&design=one"(即)在现有调用中插入-X POST )。或者,通过--data
curl --data "pathtoshpfile=../Downloads/jnk/test.shp" "http://127.0.0.1:8000/sprayermap"在我的机器上,两个查询都返回
["SpatialPolygonsDataFrame"]field_sprayer_map被写到磁盘上。为了记录在案,这对当前的水管工 CRAN版本0.4.6都适用:
p = plumb("../Downloads/jnk/plumber.R")
p$run(port=8000)以及即将到来的v1.0.0发布候选版本
pr("../Downloads/jnk/plumber.R") %>%
pr_run(port=8000)https://stackoverflow.com/questions/63792729
复制相似问题