简体   繁体   English

R:使用连接和栅格数据包从gz文件读取GeoTiff

[英]R: read GeoTiff from gz file with a connection and raster package

I would like to read a gzipped GeoTIFF from a server without downloading it. 我想从服务器读取gzip压缩的GeoTIFF,而不下载它。 I just don't want to create a lot of temporary files that I have to delete later on. 我只是不想创建很多临时文件,以后必须删除它们。

I see it is possible with .csv.gz files. 我看到 .csv.gz文件是可能的

With download I do it in the following way: 通过下载,我可以通过以下方式进行操作:

library(raster)
link <- "ftp://ftp.glcf.umd.edu/glcf/SRTM/Degree_Tiles/n000/SRTM_ff03_n000e010
/SRTM_ff03_n000e010.tif.gz"

download.file(link, "test.tif.gz")
gunzip("test.tif.gz")
myras <- raster("test.tif")
plot(myras)

I can read an uncompressed file directly from a link: 我可以直接从链接读取未压缩的文件:

link <- "http://download.osgeo.org/geotiff/samples/usgs/o41078a5.tif"
myras <- raster(link)
plot(myras)
myextent <- drawExtent()
plot(myras, ext=myextent)

Here I realize that it might not be a good Idea to not download it to local storage, because I assume that every action you subsequently do with myras needs the data to flow over the internet again. 在这里,我意识到不将其下载到本地存储可能不是一个好主意,因为我认为您随后对myras每个操作都需要数据再次通过Internet传输。 But anyway, just for proof of concept I would like to do it. 但是无论如何,我只是想证明概念。 And there are cases where you just want to display the TIFF without doing any furher calculations with it and therefore don't want to create a temporary file for it. 在某些情况下,您只想显示TIFF而不进行任何进一步的计算,因此不想为其创建一个临时文件。

To read the (downloaded) tiff.gz file without uncompressing it first I tried: tiff.gz读取(下载的) tiff.gz文件而不进行解压缩,我尝试了以下操作:

> raster(gzfile("test.tif.gz"))
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘raster’ for signature ‘"gzfile"’

To read a tiff.gz file directly from the server with a connection I tried the following: 要通过连接直接从服务器读取tiff.gz文件,我尝试了以下操作:

> con <- gzcon(url("ftp://ftp.glcf.umd.edu/glcf/SRTM/Degree_Tiles/n000/SRTM_ff03_n000e010/SRTM_ff03_n000e010.tif.gz"))
> raster(con)
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘raster’ for signature ‘"gzcon"’

> raw <- textConnection(readLines(con))
> raster(raw)
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘raster’ for signature ‘"textConnection"’

> rawBin <- textConnection(readBin(con))
Error in readBin(con) : argument "what" is missing, with no default

> con <- gzfile("ftp://ftp.glcf.umd.edu/glcf/SRTM/Degree_Tiles/n000/SRTM_ff03_n000e010/SRTM_ff03_n000e010.tif.gz")
> myras <- raster(con)
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘raster’ for signature ‘"gzfile"’

I found this Stackoverflow question about how to read a zipped binary file connection, but I am not sure whether and GeoTIFF is binary (is it?) and which parameters to pass to the readBin() function. 我发现了这个Stackoverflow问题,关于如何读取压缩的二进制文件连接,但是我不确定和GeoTIFF是否是二进制文件(是吗?)以及将哪些参数传递给readBin()函数。

I feel like randomly trying things out because I don't really understand how connections work. 我感觉就像是随机尝试,因为我不太了解连接的工作原理。 Can anyone help me with this? 谁能帮我这个?

I found a solution for this, maybe you have too. 我找到了解决方案,也许您也有。

The function readTIFF() from the tiff package can read a tiff image from a raw vector. 来自tiff包的函数readTIFF()可以从原始向量读取tiff图像。 So you can read your connection into a raw vector with readBin() , then read that raw vector with readTiff() . 所以,你可以看你的连接与原始载体readBin()然后阅读原始载体与readTiff()

# create connection to a gz file
con <- gzfile("test.tif.gz", open = "rb")

# read data from this connection into a raw vector
myras.raw <- readBin(con, what = "raw", n = 1e10)

# read this raw vector
myras <- readTIFF(myras.raw)

I hope this can help :) 我希望这可以帮助:)

Another solution is that the underlying GDAL library provides some specific 'virtual' file systems that allow a zipped and/or remote resource to be read. 另一个解决方案是基础GDAL库提供一些特定的“虚拟”文件系统,这些文件系统允许读取压缩和/或远程资源。

https://gdal.org/user/virtual_file_systems.html https://gdal.org/user/virtual_file_systems.html

The raster command supports those, basically passing the provided file path directly to GDAL to read the data. raster命令支持这些命令,基本上将提供的文件路径直接传递到GDAL以读取数据。 So you should be able to use the following to load the data directly. 因此,您应该能够使用以下内容直接加载数据。

library(raster)
link <- "/vsigzip//vsicurl/ftp://ftp.glcf.umd.edu/glcf/SRTM/Degree_Tiles/n000/SRTM_ff03_n000e010
/SRTM_ff03_n000e010.tif.gz"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM