Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

mosaic

Merge Raster* objects using a function for overlapping areas


Description

Mosaic Raster* objects to form a new object with a larger spatial extent. A function is used to compute cell values in areas where layers overlap (in contrast to the merge function which uses the values of the 'upper' layer). All objects must have the same origin, resolution, and coordinate reference system.

Usage

## S4 method for signature 'Raster,Raster'
mosaic(x, y, ..., fun, tolerance=0.05, filename="")

Arguments

x

Raster* object

y

Raster* object

...

Additional Raster or Extent objects (and/or arguments for writing files as in writeRaster)

fun

Function. E.g. mean, min, or max. Must be a function that accepts a 'na.rm' argument

tolerance

Numeric. permissible difference in origin (relative to the cell resolution). See all.equal

filename

Character. Output filename (optional)

Details

The Raster objects must have the same origin and resolution.

Value

RasterLayer or RasterBrick object.

See Also

Examples

r <- raster(ncol=100, nrow=100)
r1 <- crop(r, extent(-10, 11, -10, 11))
r2 <- crop(r, extent(0, 20, 0, 20))
r3 <- crop(r, extent(9, 30, 9, 30))

values(r1) <- 1:ncell(r1)
values(r2) <- 1:ncell(r2)
values(r3) <- 1:ncell(r3)

m1 <- mosaic(r1, r2, r3, fun=mean)

s1 <- stack(r1, r1*2)
s2 <- stack(r2, r2/2)
s3 <- stack(r3, r3*4)
m2 <- mosaic(s1, s2, s3, fun=min)

# if you have a list of Raster objects, you can use do.call
x <- list(r1, r2, r3)
names(x)[1:2] <- c('x', 'y')
x$fun <- mean
x$na.rm <- TRUE

y <- do.call(mosaic, x)

raster

Geographic Data Analysis and Modeling

v3.4-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Jacob van Etten [ctb], Michael Sumner [ctb], Joe Cheng [ctb], Dan Baston [ctb], Andrew Bevan [ctb], Roger Bivand [ctb], Lorenzo Busetto [ctb], Mort Canty [ctb], Ben Fasoli [ctb], David Forrest [ctb], Aniruddha Ghosh [ctb], Duncan Golicher [ctb], Josh Gray [ctb], Jonathan A. Greenberg [ctb], Paul Hiemstra [ctb], Kassel Hingee [ctb], Institute for Mathematics Applied Geosciences [cph], Charles Karney [ctb], Matteo Mattiuzzi [ctb], Steven Mosher [ctb], Babak Naimi [ctb], Jakub Nowosad [ctb], Edzer Pebesma [ctb], Oscar Perpinan Lamigueiro [ctb], Etienne B. Racine [ctb], Barry Rowlingson [ctb], Ashton Shortridge [ctb], Bill Venables [ctb], Rafael Wueest [ctb]
Initial release
2021-05-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.