crop {terra} | R Documentation |
Cut out a part of a SpatRaster with a SpatExtent, or another object from which an extent can be obtained. With a SpatRaster you can only extract rectangular areas, but see mask
for setting cell values within SpatRaster to NA
.
You can crop a SpatVector with a rectangle, or with another vector (if these are not polygons, the minimum convex hull is used). Unlike with intersect
the geometries and attributes of y
are not transferred to the output.
## S4 method for signature 'SpatRaster'
crop(x, y, snap="near", mask=FALSE, filename="", ...)
## S4 method for signature 'SpatRasterDataset'
crop(x, y, snap="near", filename="", ...)
## S4 method for signature 'SpatVector'
crop(x, y)
x |
SpatRaster or SpatVector |
y |
SpatRaster, SpatVector, SpatExtent or other object that has a SpatExtent ( |
snap |
character. One of "near", "in", or "out". Used to align |
mask |
logical. Should |
filename |
character. Output filename |
... |
additional arguments for writing files as in |
SpatRaster
intersect
r <- rast(xmin=0, xmax=10, ymin=0, ymax=10, nrows=25, ncols=25)
values(r) <- 1:ncell(r)
e <- ext(-5, 5, -5, 5)
rc <- crop(r, e)
# crop and mask
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
cm <- crop(r, v[9:12,], mask=TRUE)
plot(cm)
lines(v)
# crop vector
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
e <- ext(6.15, 6.3, 49.7, 49.8)
x <- crop(v, e)
plot(x, "NAME_1")