问题描述
我正在尝试使用plumber R Package将函数包装到REST API中。作为Input,函数获取一个形状文件,并在转换后返回 GeoJSON作为输出。
#* Return Spatial Polygon Object in form of GeoJSON
#* @param dsn:character Path to Shapefile with Name
#* @param design:character one or two
#* @post /sprayermap
sprayer_map <-
function(dsn,design = c("one","two")) {
# library
require(rgeos)
require(rgdal)
require(sp)
require(raster)
require(cleangeo)
#Import Shapefile
a_shape <- raster::shapefile(dsn)
result <-
list("success" = F,additional_info = NULL,GeoJSON = NULL)
if (class(a_shape) == "SpatialPolygonsDataFrame") {
a_shape <- tryCatch (
rgeos::gBuffer(a_shape,byid = TRUE,width = 0),error = function(err) {
return(paste("sprayer map : ",err))
}
)
if (design == "one") {
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)
} else if (design == "two") {
return(paste0("Design Two !"))
}
temppath <- file.path(tempdir(),"sprayermap.GeoJSON")
rgdal::writeOGR(
sprayer_map,dsn = temppath,layer = "geojson",driver = "GeoJSON",overwrite_layer = TRUE
)
if (file.exists(temppath)) {
GeoJSON <- readLines(temppath)
result$success <- T
result$GeoJSON = GeoJSON
return(result)
} else {
return(paste0("GeoJSON Creation Failed!"))
}
} else {
return(paste0("Please provide spatial polygon object !"))
}
}
现在要使REST API在实现和使用方面更加通用,REST API的 Input 需要更改为 GeoJSON 作为请求正文(req $ postBody)代替形状文件路径导入方法。 寻找指导在这种情况下如何实现相同的目标。 Test Input Shape file as well as GeoJSON
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)