merge: Combine two or more rasters with different extents and fill...

merge,GRaster,GRaster-methodR Documentation

Combine two or more rasters with different extents and fill in NAs

Description

merge() combines two or more GRasters, possibly with different extents, into a single larger GRaster. Where the same cell has different values in each raster, the value of the first raster's cell is used. If this is NA, then the value of the second raster's cell is used, and so on.

Usage

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

Arguments

x, y, ...

GRasters.

Value

A GRaster.

See Also

terra::merge(), terra::mosaic(), and GRASS module r.patch

Examples

if (grassStarted()) {

# Setup
library(sf)
library(terra)

# Example data
madElev <- fastData("madElev")
madCoast4 <- fastData("madCoast4")
madCoast4 <- vect(madCoast4)

# For the example, crop the elevation raster to two communes
madAnt <- madCoast4[madCoast4$NAME_4 == "Antanambe", ]
madMan <- madCoast4[madCoast4$NAME_4 == "Manompana", ]

elevAnt <- crop(madElev, madAnt)
elevMan <- crop(madElev, madMan)

plot(madElev)
plot(elevAnt, col = "red", legend = FALSE, add = TRUE)
plot(elevMan, col = "blue", legend = FALSE, add = TRUE)

# Convert a SpatRaster to a GRaster
ant <- fast(elevAnt)
man <- fast(elevMan)

# merge
antMan <- merge(ant, man)
plot(antMan, main = "Antman!")

}

adamlilith/fasterRaster documentation built on Dec. 21, 2024, 2:04 a.m.