Search code examples
rrasterrgdalgeotiff

r raster read .vrt in a zip file


I have a large zip file with a bunch of geotiff files in it and a .vrt file with the needed info for each. Rather than unzip all the files, I'd like to work directly with the zip file.

This command, with the .vrt and some of the geotiff files works, in the sense that temp raster is created as a rasterLayer but since not all the geotiff files are unzipped, plot(rasterLayer) fails with a missing data error.

tempraster <- raster("data-raw/CoastalDEMv1.1/tiles.vrt")

This command fails, presumably because I has specified the path incorrectly

tempraster <- raster("data-raw/Global_90.zip/CoastalDEMv1.1/tiles.vrt")

The error message is

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",: Cannot create a RasterLayer object from this file. (file does not exist)

Presumably, this means raster thinks the zip part of the path is just another folder. I need it to look inside the zip.

I'm running on a Mac, OS is Catalina.


Solution

  • Some text from gdal.org

    To point to a file inside a zip file, the filename must be of the form /vsizip/path/to/the/file.zip/path/inside/the/zip/file, where path/to/the/file.zip is relative or absolute and path/inside/the/zip/file is the relative path to the file inside the archive.

    So for my dataset I use

    link <- "/vsizip/data-raw/Global_90.zip/CoastalDEMv1.1/tiles.vrt"
    test <- raster(link)
    

    test has the following characteristics

    class      : RasterLayer 
    dimensions : 139316, 432360, 60234665760  (nrow, ncol, ncell)
    resolution : 0.0008326395, 0.0008326395  (x, y)
    extent     : -180, 180, -56, 60  (xmin, xmax, ymin, ymax)
    crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
    source     : /vsizip/data-raw/Global_90.zip/CoastalDEMv1.1/tiles.vrt 
    names      : tiles 
    

    As I work with this and learn more, I'll update the answer.