我想从服务器提供的 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))
但是,我只能找到要从文件中读取的 raster 或 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)
关于R:直接从 web url 读取 geotiff 数据(httr::GET 原始内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52182635/