R:直接从网络读取 geotiff 数据 url(httr::GET 原始内容)

R: reading geotiff data straight from web url (httr::GET raw content)

我想根据服务器提供的 GeoTIFF 数据创建一个 RasterLayer。我将使用 httr::GET 调用向服务器查询此数据(数据是按需提供的,因此在应用程序中不会有以 .tif 结尾的 url,而是查询 url).

将此调用的结果作为 GeoTIFF 文件写入磁盘后,很容易从磁盘上生成的 GeoTIFF 文件创建 RasterLayer:

library(httr)
library(raster)

url <- 'http://download.osgeo.org/geotiff/samples/gdal_eg/cea.tif'

geotiff_file <- tempfile(fileext='.tif')
httr::GET(url,httr::write_disk(path=geotiff_file))
my_raster <- raster(geotiff_file)
my_raster

但是,我想跳过写入磁盘部分并直接从内存服务器响应创建栅格。

response <- httr::GET(url,httr::write_memory())
response

响应的内容是一个原始字符串,我需要将其解释为 geoTIFF 数据。

str(httr::content(response))

但是,我只能找到光栅或 rgdal 函数来从文件中读取。关于将此原始字符串转换为栅格有什么建议吗?

谢谢!

GDAL 有一些很酷的东西 virtual file system driver,其中之一是 /vsicurl

allows on-the-fly random reading of files available through HTTP/FTP web protocols, without prior download of the entire file. It requires GDAL to be built against libcurl.

由于 raster 软件包建立在 rgdal 之上,您可以简单地这样做:

library(raster)

r <- raster('/vsicurl/http://download.osgeo.org/geotiff/samples/gdal_eg/cea.tif')

plot(r)