rgeospatialr-rasterterra

Mask and Crop functions in Terra R


When the raster package was still active, I would usually have to do the following to crop the rasterlayer to an irregular polygon (with some functions updated for terra). I am wondering there is a faster/more efficient way to do this same thing?:

kml <- vect("pathtokml.kml")
r <- rast("pathtoraster.tif")

crp <- mask(r, project(kml, r)) #fix for different CRS's
crp2 <- crop(crp, ext(kml))
plot(crp2)

Solution

  • It is more efficient to first crop and then mask, and with terra you can do this in one step.

    x <- crop(r, project(kml, r), mask=TRUE)