You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
trying to handle multi-layer SpatRasters and I am running into an issue when cropping the data. This error occurs as soon as the layer count exceeds 65535.
Minimal Working Example
Below is a minimal working example adapted from the terra::crop() documentation:
library(terra)
## data to cropf<- system.file("ex/elev.tif", package="terra")
rr<- rast(f)
r<- c(rep(rr, 65535+1)) # repeat base layer one too many times for cropping## shape to crop withf<- system.file("ex/lux.shp", package="terra")
v<- vect(f)
## cropping that workscm<- crop(r[[-1]], v, mask=TRUE, touches=TRUE) # reduce layer count by one## cropping that failscm<- crop(r, v, mask=TRUE, touches=TRUE)
#Error: [crop] failed writing GTiff file#In addition: Warning message:#/private/var/folders/c9/5tlmp10s517_f5qmn120ctjc0000gp/T/RtmpUJljN2/spat_9a79465d898_39545_2.tif: Attempt to #create 94x88x65536 TIFF file, but bands must be lesser or equal to 65535. (GDAL error 1)
Proposed Solution
How about adding a check to the cropping (and probably also masking) function and carry out cropping on maximally stacked SpatRasters and fusing them re-stacking them after cropping has been done? Like so:
library(terra)
library(sf)
## data to cropf<- system.file("ex/elev.tif", package="terra")
r<- rast(f)
r<- c(rep(r, ceiling(65535*1.7))) # a lot more layers than crop can handle at once## shape to crop withf<- system.file("ex/lux.shp", package="terra")
v<- vect(f)
## identifying layer indices for splitting into SpatRasters of maximum 65535 layersIndices<- ceiling((1:nlyr(r))/65535)
## splitting SpatRaster into list of SpatRastersr_ls<-terra::split(x=r, f=Indices)
## Cropping list of SpatRasterscm_ls<- lapply(r_ls, terra::crop, y=v, mask=TRUE, touches=TRUE)
## Fusing list of cropped SpatRasters back togethercm<- do.call(c, cm_ls)
When checking that this cropping worked as intended, I find no issue:
I will look into using a different file format for temporary files, that can have more than than 65535 layers. Your solution can work, but I need something that automatically works (in the C++ code) for all raster methods.
Hiya,
trying to handle multi-layer SpatRasters and I am running into an issue when cropping the data. This error occurs as soon as the layer count exceeds 65535.
Minimal Working Example
Below is a minimal working example adapted from the
terra::crop()
documentation:Proposed Solution
How about adding a check to the cropping (and probably also masking) function and carry out cropping on maximally stacked SpatRasters and fusing them re-stacking them after cropping has been done? Like so:
When checking that this cropping worked as intended, I find no issue:
Maybe this could even be combined with the addition of a
cores
argument akin to the implementation in thetapp
function.Operating System & Package Versions
I am on MacOS Sonoma (v14.5).
The text was updated successfully, but these errors were encountered: