如何构建一个循环来逐步光栅化几个简单的功能?
library(sf)
library(fasterize)
b0 = st_polygon(list(rbind(c(-1,-1), c(1,-1), c(1,1), c(-1,1), c(-1,-1))))
b1 = b0 + 2
b2 = b0 + c(-0.2, 2)
x = st_sfc(b0, b1, b2)
x = st_sf(data.frame(a=1:3, geom=x))
pt1 = st_point(c(0.5,0.5))
pt2 = st_point(c(1.5,0.5))
pt3 = st_point(c(2.5,2.5))
d = st_sfc(pt1, pt2, pt3)
d = st_sf(data.frame(a=1:3, geom=d))
d.b <- st_buffer(d,1)
plot(x, col = c('red', 'green', 'gold'))
plot(st_geometry(d.b), add=TRUE)
y <- st_intersection(x, d.b)
plot(st_geometry(y[y$a.1==1,]))
plot(st_geometry(y[y$a.1==2,]))
plot(st_geometry(y[y$a.1==3,]))
r <- raster(x, 0.5)
f <- fasterize(y,r)
plot(f)
我正在寻找的可以通过以下方式创建:
f1 <- fasterize(y[y$a.1 == 1,], r)
f2 <- fasterize(y[y$a.1 == 2,], r)
f3 <- fasterize(y[y$a.1 == 3,], r)
plot(f1)
plot(f2)
plot(f3)
然而,我确信有一种方法可以通过循环获得相同的结果。结果可能是Rasterstack。
library(foreach)
l=foreach(i=1:3) %do% {
fx=fasterize(y[y$a.1 == i,], r)
}
raster::brick(l)