I have a raster and a polygon, and I want to make two masks:
Using my reproducible example, I can produce the first using terra::mask, and I can produce the second with the aid of terra::extract(... exact=TRUE).
In real life, my raster has the following dimensions, so I cannot use terra::extract with exact=T, as there is not enough memory (in my computer).
enter class : SpatRaster
dimensions : 42893, 52031, 1 (nrow, ncol, nlyr)
resolution : 100, 100 (x, y)
extent : -2253700, 2949400, -5218400, -929100 (xmin, xmax, ymin, ymax)
coord. ref. : GDA94 / Australian Albers (EPSG:3577)
source(s) : memory
name : update_veg
min value : 10005
max value : 4190069
here is my code that works, so you can see what I mean:
library(terra)
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
v <- v[1:2,]
z <- rast(v, resolution=.05, names="test")
values(z) <- 1:ncell(z)
mask_1 <- mask(z, v, touches=T, inverse=F)
plot(mask_1)
plot(v, add=T)
# this extract command will not run on my ginormous raster
k <- extract(z, v, ID=TRUE, touches=T, exact=T, cells=T)
k$mini <- k$test
k$mini[k$fraction < 1] <- NA
mask_2 <- z
values(mask_2) <- NA
values(mask_2)[k$cell] <- k$mini
plot(mask_2)
plot(v, add=T)
These plots appear as I would like to see them.
If anyone can help me with a method that might not pound my computer to smithereens, I will be highly appreciative.
You can do
mask_2 <- rasterize(as.lines(v), mask_1, NA, update=TRUE)
plot(mask_2)
lines(v)