gpt4 book ai didi

r - 从终端获取有效,但 httr::GET 无效

转载 作者:行者123 更新时间:2023-12-01 14:03:13 25 4
gpt4 key购买 nike

我正在尝试使用 httr::GET 从服务器下载栅格。我们曾经可以使用此功能,但对服务器进行了一些更改,现在它无法使用。

通过浏览器或终端 (ubuntu 16.04) 中的 GET 使用 URL 可以正常工作并返回工作的 tif 栅格。但是使用 httr::GET 中的相同 url 不起作用,并给出 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')

最佳答案

包装在 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)

关于r - 从终端获取有效,但 httr::GET 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49193731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com