R:直接从 web url 读取 geotiff 数据(httr::GET 原始内容) [英] R: reading geotiff data straight from web url (httr::GET raw content)

查看:79
本文介绍了R:直接从 web url 读取 geotiff 数据(httr::GET 原始内容)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

库(httr)图书馆(光栅)网址 <- '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 <- 光栅(geotiff_file)我的_栅格

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

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

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

str(httr::content(response))

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

谢谢!

解决方案

GDAL 有一些很酷的

I would like to create a RasterLayer from GeoTIFF data provided by a server. I'll query the server for this data using a httr::GET call (the data is provided on-demand, so in the application there won't be a url ending in .tif but a query url).

After writing the result of this call to disk as a GeoTIFF file it's easy enough to create the RasterLayer from the resulting GeoTIFF file on disk:

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

However, I would like to skip the write to disk part and create the raster straight from the in-memory server response.

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

The content of the response is a raw string which I would need to interpret as geoTIFF data.

str(httr::content(response))

However, I can only find raster or rgdal functions to read from a file. Any suggestions on translating this raw string to a raster?

Thanks!

解决方案

GDAL has some cool virtual file system driver, one of which is /vsicurl that

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.

Since the raster package builds on rgdal you can simply do this:

library(raster)

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

plot(r)

这篇关于R:直接从 web url 读取 geotiff 数据(httr::GET 原始内容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆