merge {terra}R Documentation

Merge multiple SpatRaster objects or SpatExtent objects, or merge a SpatVector with a data.frame

Description

Merge multiple SpatRasters to create a new SpatRaster object with a larger spatial extent. The SpatRasters must have the same origin and spatial resolution. In areas where the SpatRasters overlap, the values of the SpatRaster that is first in the sequence of arguments (or in the SpatRasterCollection) will be retained (unless first=FALSE.

See classify to merge a SpatRaster and a data.frame. You can also merge SpatExtent objects.

There is a also a method for merging SpatVector with a data.frame; that is, to join the data.frame to the attribute table of the SpatVector.

Usage

## S4 method for signature 'SpatRaster,SpatRaster'
merge(x, y, ..., first=TRUE, filename="", overwrite=FALSE, wopt=list())

## S4 method for signature 'SpatRasterCollection,missing'
merge(x, first=TRUE, filename="", ...)

## S4 method for signature 'SpatExtent,SpatExtent'
merge(x, y, ...)

## S4 method for signature 'SpatVector,data.frame'
merge(x, y, ...)

Arguments

x

SpatRaster or SpatExtent

y

object of same class as x

...

if x is a SpatRaster: additional objects of the same class as x. If x is a SpatRasterCollection: options for writing files as in writeRaster. If x is a SpatVector, the same arguments as in merge

first

logical. If TRUE, in areas where rasters overlap, the first value that is not NA is used. Otherwise the last value that is not NA is used

filename

character. Output filename

overwrite

logical. If TRUE, filename is overwritten

wopt

list with named options for writing files as in writeRaster

Value

SpatRaster or SpatExtent

Note

You can use merge with do.call to merge a list of SpatRasters (see example). But note that if the list is named, these names are used by merge. So if all elements are named, there should be one element with a SpatRaster called x and another one called y. For example with names(x)[1:2] <- c("x"m "y"). You can also removed the names of the the first two elements (assuming these are SpatRasters) with names(x)[1:2] <- "".

See Also

Combining tiles with vrt may be more efficient. See mosaic for averaging overlapping regions.

Examples

x <- rast(xmin=-110, xmax=-80, ymin=40, ymax=70, res=1, vals=1)
y <- rast(xmin=-85, xmax=-55, ymax=60, ymin=30, res=1, vals=2)
z <- rast(xmin=-60, xmax=-30, ymax=50, ymin=20, res=1, vals=3)

m1 <- merge(x, y, z)
m2 <- merge(z, y, x)
m3 <- merge(y, x, z)

# if you have many SpatRasters make a SpatRasterCollection from a list
rlist <- list(x, y, z)
rsrc <- sprc(rlist)

m <- merge(rsrc)


## SpatVector with data.frame
f <- system.file("ex/lux.shp", package="terra")
p <- vect(f)
dfr <- data.frame(District=p$NAME_1, Canton=p$NAME_2, Value=round(runif(length(p), 100, 1000)))
dfr <- dfr[1:5, ]
pm <- merge(p, dfr, all.x=TRUE, by.x=c('NAME_1', 'NAME_2'), by.y=c('District', 'Canton'))
pm
values(pm)

[Package terra version 1.6-33 Index]