【问题标题】:REST APIs with R Plumber - GIS ShapefilesREST API 与 R Plumber - GIS Shapefiles
【发布时间】: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


【解决方案1】:

我相信您的功能设置正确。以下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 被写入磁盘。作为记录,这适用于当前 plumber CRAN 0.4.6 版:

p = plumb("../Downloads/jnk/plumber.R")
p$run(port=8000)

还有即将到来的v1.0.0 release candidate

pr("../Downloads/jnk/plumber.R") %>%
  pr_run(port=8000)

【讨论】:

  • 谢谢 fdetsch。生成的本地招摇对我有用,它给出了您建议的功能看起来不错的输出。对我来说 curl 仍然给出问题 - 错误:“curl -X POST”中的意外符号。我正在使用在 ubuntu 机器中配置的 RStudio Server 并使用 0.4.9 rstudio 版本的管道工。看来我犯了一些愚蠢的错误。有什么想法吗?
  • 你能分享你的curl -X POST电话吗?
  • 您好,需要您的建议/指导才能将 GeoJSON 作为请求正文作为输入 - POST 请求管道工 API 中的 json 正文或 json 文件,而不是 shapefile 路径。如果需要,我可以分享可重复的示例。谢谢。
猜你喜欢
  • 2021-01-21
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 2014-11-30
  • 2019-04-22
相关资源
最近更新 更多