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

标签 r raster httr rgdal geotiff

我想从服务器提供的 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)

enter image description here

关于R:直接从 web url 读取 geotiff 数据(httr::GET 原始内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52182635/

相关文章:

r - 如何知道R中栅格中某个值出现的百分比?

r - 使用 R 从网页中提取元描述

html - 在 R 中抓取动态表

r - 编织xtable : how to position tables between text?

javascript - 将 selectInput 传递给 html 标签

使用 terra 和 raster 包读取 tif 文件给出不同的结果

r - 两个或多个数据帧的平均单元格

python - 在 folium 中显示栅格数据(不处理任何数据值)。

r - 如何在 rvest 的 html_session 中使用 post

r - 带有多个 header 的httr请求