Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r raster Can I combine two raster bricks directly?

Tags:

r

r-raster

I want to combine two (or more) raster bricks. Here's an example of what I want to do

library(raster)

r <- raster(ncol=40, nrow=20)
r[] <- rnorm(n=ncell(r))

b1 <- brick(x=c(r, r*2, r))
b2 <- brick(x=c(r, r*3, r))

b <- brick(b1, b2)

This example returns the following error.

Error in (function (cl, name, valueClass)  :    assignment of an
object of class “RasterBrick” is not valid for @‘nlayers’ in an object
of class “.MultipleRasterData”; is(value, "integer") is not TRUE

If I create stacks instead of bricks as follows

b1 <- stack(x=c(r, r*2, r))
b2 <- stack(x=c(r, r*3, r))

b <- brick(b1, b2)

I get the following error message

Error in if (values) { : argument is not interpretable as logical
like image 295
JerryN Avatar asked Nov 27 '25 23:11

JerryN


1 Answers

You need stack to combine multiple rasters, stacks, or bricks that have the same extent and resolution into a single multi-layer object. Then you can convert to a brick

s <- stack(b1, b2)
nlayers(s)
#[1] 6
b <- brick(s)
print(b)

class       : RasterBrick 
dimensions  : 20, 40, 800, 6  (nrow, ncol, ncell, nlayers)
resolution  : 9, 9  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       :  layer.1.1,  layer.2.1,  layer.3.1,  layer.1.2,  layer.2.2,  layer.3.2 
min values  :  -3.604523,  -7.209047,  -3.604523,  -3.604523, -10.813570,  -3.604523 
max values  :   3.441872,   6.883743,   3.441872,   3.441872,  10.325615,   3.441872

See https://www.rspatial.org/raster/spatial/4-rasterdata.html#rasterstack-and-rasterbrick for more info

like image 167
ThetaFC Avatar answered Nov 30 '25 13:11

ThetaFC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!