I would like to read a gzipped GeoTIFF from a server without downloading it. 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.
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. 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.
To read the (downloaded) tiff.gz
file without uncompressing it first I tried:
> 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:
> 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.
I feel like randomly trying things out because I don't really understand how connections work. Can anyone help me with this?