Search code examples
rrasterterrawcs

Problems reading in large raster files from WCS connection using Terra


I am running into new issues accessing .tif files via WCS connection in R using the package terra. The issue seems to be related to the size of the coverage area. I've been running similar code in terra for some time with no issue, and am not sure why this is happening. To illustrate the issue, the below WCS calls (big and small) both connect via read_stars() but only the small coverage area works for rast().

library(terra)
library(stars)

smaller_dim <- 'https://www.mrlc.gov/geoserver/rcmap_anhb/wcs?request=GetCoverage&service=WCS&version=2.0.1&coverageid=rcmap_anhb__rcmap_annual_herbaceous_2020&subset=X(-1026935.24156938,-1015591.34251064)&subset=Y(2005364.48088881,2014307.50831563)'
bigger_dim <- 'https://www.mrlc.gov/geoserver/rcmap_anhb/wcs?request=GetCoverage&service=WCS&version=2.0.1&coverageid=rcmap_anhb__rcmap_annual_herbaceous_2020&subset=X(-1054529.13659319,-1022600.05347719)&subset=Y(2020307.11783295,2056926.21900819)'

## both work
x <- rast(smaller_dim)
y <- read_stars(smaller_dim)

## read_stars works, but rast() can't find the file

xx <- rast(bigger_dim)
#Error: [rast] file does not exist: /vsicurl/https://www.mrlc.gov/geoserver/rcmap_anhb/wcs?request=GetCoverage&service=WCS&version=2.0.1&coverageid=rcmap_anhb__rcmap_annual_herbaceous_2020&subset=X(-1054529.13659319,-1022600.05347719)&subset=Y(2020307.11783295,2056926.21900819)
#In addition: Warning message:
#`/vsicurl/https://www.mrlc.gov/geoserver/rcmap_anhb/wcs?request=GetCoverage&service=WCS&version=2.0.1&coverageid=rcmap_anhb__rcmap_annual_herbaceous_2020&subset=X(-1054529.13659319,-1022600.05347719)&subset=Y(2020307.11783295,2056926.21900819)' not recognized as a supported file format. (GDAL error 4) 

yy <- read_stars(bigger_dim)

Solution

  • It works if you set argument "vsi" to FALSE

    rast(bigger_dim, vsi=FALSE)
    #class       : SpatRaster 
    #dimensions  : 1221, 1064, 1  (nrow, ncol, nlyr)
    #resolution  : 30, 30  (x, y)
    #extent      : -1054515, -1022595, 2020305, 2056935  (xmin, xmax, ymin, ymax)
    #coord. ref. : NAD83 / Conus Albers (EPSG:5070) 
    #source      : wcs?request=GetCoverage&service=WCS&version=2.0.1&coverageid=rcmap_anhb__rcmap_annual_herbaceous_2020&subset=X(-1054529.13659319,-1022600.05347719)&subset=Y(2020307.11783295,2056926.21900819) 
    #color table : 1 
    #varname     : wcs?request=GetCoverage&service=WCS&version=2.0.1&coverageid=rcmap_anhb__rcmap_annual_herbaceous_2020&subset=X(-1054529.13659319,-1022600.05347719)&subset=Y(2020307.11783295,2056926 
    #name        : wcs?request=GetCoverage&servic~set=Y(2020307.11783295,2056926 
    

    Suggesting that "vsi=TRUE" might not be a good default (because not all https requests point to an actual file).