Learn R Programming

terra (version 0.2-9)

merge: Merge SpatRaster or SpatExtent objects

Description

Merge SpatRasters to form a new SpatRaster object with a larger spatial extent. If objects overlap, the values get priority in the same order as the arguments. See classify to merge a SpatRaster and a data.frame. You can also merge SpatExtent objects.

Usage

# S4 method for SpatRaster,SpatRaster
merge(x, y, ..., filename="", overwrite=FALSE, wopt=list())
# S4 method for SpatExtent,SpatExtent
merge(x, y, ...)

Arguments

x

SpatRaster or SpatExtent object

y

object of same class as x

...

additional objects of the same class as x

filename

character. Output filename. Optional

overwrite

logical. If TRUE, filename is overwritten

wopt

list. Options for writing files as in writeRaster

Value

SpatRaster or SpatExtent

Details

The SpatRaster objects must have the same origin and spatial resolution. In areas where the SpatRaster objects overlap, the values of the SpatRaster that is last in the sequence of arguments will be retained.

Examples

Run this code
# NOT RUN {
x <- rast(xmin=-110, xmax=-50, ymin=40, ymax=70, ncols=60, nrows=30)
y <- rast(xmin=-80, xmax=-20, ymax=60, ymin=30)
res(y) <- res(x)
values(x) <- 1:ncell(x)
values(y) <- 1:ncell(y)
mr <- merge(x, y)
plot(mr)
mr <- merge(y, x)

# if you have many SpatRaster objects in a list
# you can use do.call:
s <- list(x, y)
# add arguments such as filename
# s$filename <- 'test.tif'
m <- do.call(merge, s)
# }

Run the code above in your browser using DataLab