如何构建循环以光栅化几个简单的功能?

问题描述 投票:0回答:1

如何构建循环以逐步栅格化几个简单功能?

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。

r loops intersection rasterize
1个回答
0
投票
library(foreach)
l=foreach(i=1:3) %do% {
  fx=fasterize(y[y$a.1 == i,], r)
}

raster::brick(l)
© www.soinside.com 2019 - 2024. All rights reserved.