从终端工作获取但是httr :: GET没有

时间:2018-03-09 12:21:27

标签: r httr

我正在尝试从httr::GET的服务器下载栅格。我们曾经这样做,但是对服务器进行了一些更改,现在它不起作用。

通过浏览器或通过终端中的GET(ubuntu 16.04)使用URL工作正常并返回一个正常工作的tif栅格。但是,使用httr::GET中的相同网址不起作用,并提供Status: 400

我唯一的猜测是它与数据的编码有关。但我真的不确定。

file <- paste0(tempdir(), '/file.tif')
r <- 
  httr::GET('https://map.ox.ac.uk/geoserver/Explorer/ows?service=WCS&version=2.0.1&request=GetCoverage&format=image/geotiff&coverageid=2015_Nature_Africa_PR&SUBSET=Long(-3,50.483779907)&SUBSET=Lat(-25.6089496609999,-11.9454326629999)&SUBSET=time(\"2015-01-01T00:00:00.000Z\")',
           httr::write_disk(file, overwrite = TRUE))

ras <- raster::raster(file)

# I'm now totally confused about when and where quotes are escaped, so just to make sure...
r <- 
  httr::GET('https://map.ox.ac.uk/geoserver/Explorer/ows?service=WCS&version=2.0.1&request=GetCoverage&format=image/geotiff&coverageid=2015_Nature_Africa_PR&SUBSET=Long(-3,50.483779907)&SUBSET=Lat(-25.6089496609999,-11.9454326629999)&SUBSET=time("2015-01-01T00:00:00.000Z")',
            httr::write_disk(file, overwrite = TRUE))

ras <- raster::raster(file)

# But just putting the url in the browser works fine.
# https://map.ox.ac.uk/geoserver/Explorer/ows?service=WCS&version=2.0.1&request=GetCoverage&format=image/geotiff&coverageid=2015_Nature_Africa_PR&SUBSET=Long(-3,50.483779907)&SUBSET=Lat(-25.6089496609999,-11.9454326629999)&SUBSET=time("2015-01-01T00:00:00.000Z")

# eg 
# rr <- raster::raster('~/Desktop/2015_Nature_Africa_PR3.tif')


# And using the URL with GET in the terminal works
# GET "https://map.ox.ac.uk/geoserver/Explorer/ows?service=WCS&version=2.0.1&request=GetCoverage&format=image/geotiff&coverageid=2015_Nature_Africa_PR&SUBSET=Long(-3,50.483779907)&SUBSET=Lat(-25.6089496609999,-11.9454326629999)&SUBSET=time(\"2015-01-01T00:00:00.000Z\")" > ~/Desktop/2015_Nature_Africa_PR4.tif
# eg
# rr <- raster::raster('~/Desktop/2015_Nature_Africa_PR4.tif')

1 个答案:

答案 0 :(得分:5)

包裹URLEncode为我工作:

library(httr)
library(raster)

file <- paste0(tempdir(), '/file.tif')
url1 <- 'https://map.ox.ac.uk/geoserver/Explorer/ows?service=WCS&version=2.0.1&request=GetCoverage&format=image/geotiff&coverageid=2015_Nature_Africa_PR&SUBSET=Long(-3,50.483779907)&SUBSET=Lat(-25.6089496609999,-11.9454326629999)&SUBSET=time("2015-01-01T00:00:00.000Z")'

r   <- GET(URLencode(url1), write_disk(file, overwrite = TRUE))
ras <- raster(file)