3

I am trying to create an empty raster layer and I want this empty raster to have the same dimensions, pixel size and CRS based on another raster, but no pixel values.

I am getting errors when I try to do this, based on what I am trying. For example:

library(terra)
cr = rast(path/test.tif) # extract CRS, pixel size, dimensions

**Try 1**

er_crs = crs(cr)
er_dim = dim(cr)
er_res = res(cr)

er = rast(`dim<-`(er_dim),
          `res<-`(er_res),
          `crs<-`(er_crs))

Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'rast': 1 argument passed to 'dim<-' which requires 2

**Try 2**

er_crs = crs(cr)
er_res = res(cr)
er_row = nrow(cr)
er_col = ncol(cr)

er = rast(`nrow<-`(er_row),
          `ncol<-`(er_col),
          `res<-`(er_res),
          `crs<-`(er_crs))

Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'rast': unable to find an inherited method for function ‘nrow<-’ for signature ‘"numeric", "missing"’

I have tried few more times but I am getting more errors.

Here is the raster:

cr = rast(ncols=23, nrows=43, nlyrs=1, xmin=583280, xmax=593860, ymin=1006020, ymax=1025800, names=c('test'), crs='EPSG:7767')
1
  • I'm curious to know why you are trying to use nrow<- in your calls like rast(`nrow<-`(er_row),.... This isn't how you pass named parameters in R, you have to use the equals sign: rast(nrow=er_row,...).
    – Spacedman
    Nov 1, 2022 at 8:11

3 Answers 3

7

You can pass a SpatExtent (extent) object to terra::rast to create a new empty raster with the same extent. If you also provide the resolution argument that will address the resolution, row, column parts as well.

library(terra)

( r <- rast(system.file("ex/elev.tif", package="terra")) )

( er <- rast(ext(r), resolution=res(r)) ) 
  crs(er) <- crs(r)
3
  • The crs can be defined as a parameter within the rast function so you could run this as a one liner if needed. er <- rast(ext(r), resolution=res(r), crs = crs(r))
    – carverd
    Mar 1 at 22:14
  • 1
    I tend to assign the csr after-the-fact because not all methods in terra::rast accept the csr argument (eg., list, character). However, in this case, proving the argument is certainly more efficient. Mar 2 at 19:51
  • Thanks for the clarification there. Learned something new!
    – carverd
    Mar 4 at 18:37
1

Another approach would be to make a copy of your original raster and set all values to NA:

library(terra)
#> terra 1.6.33

r <- rast(system.file("ex/elev.tif", package="terra"))
r
#> class       : SpatRaster 
#> dimensions  : 90, 95, 1  (nrow, ncol, nlyr)
#> resolution  : 0.008333333, 0.008333333  (x, y)
#> extent      : 5.741667, 6.533333, 49.44167, 50.19167  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source      : elev.tif 
#> name        : elevation 
#> min value   :       141 
#> max value   :       547

s <- r
values(s) <- NA
s
#> class       : SpatRaster 
#> dimensions  : 90, 95, 1  (nrow, ncol, nlyr)
#> resolution  : 0.008333333, 0.008333333  (x, y)
#> extent      : 5.741667, 6.533333, 49.44167, 50.19167  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source(s)   : memory
#> name        : elevation 
#> min value   :       NaN 
#> max value   :       NaN

As you can see, dimensions, resolution and crs are all the same.

1
  • 1
    do that with s <- rast(r)
    – mdsumner
    Oct 31, 2022 at 10:08
1

Another call to rast() does this.

library(terra)
## has values
r <- rast(matrix(1:12, 3))

## has no values (not even NA)
rast(r)

It was the same in principle with 'raster', but the details are very different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.