带有R Plumber的REST API-GIS Shapefiles

问题描述

我正在尝试使用plumber R Package将函数包装到REST API中。作为输入,函数接受一个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"),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 API(输入和输出均为shapefile)的指导。 测试形状文件Test Shape File

解决方法

我相信您的功能已正确设置。以下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 release candidate

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...