如何使用 sf 和 R 将圆分成相等的 MULTIPOLYGON“切片”?

Dav*_*lin 0 r r-sf

我有这个圈子:

library(sf)
p <- st_sfc(st_point(c(0, 1))) 
circle <- st_buffer(p, dist = 1)  
plot(circle)
Run Code Online (Sandbox Code Playgroud)

sf_circle

如何将这个圆分成 4 个相等的“切片”?6等份?8等份?等等。我需要返回的对象是一个 MULTIPOLYGON。

Spa*_*man 6

使用这两个函数,一个在给定圆心、半径、起始角度、宽度和弧形部分的截面数的情况下创建单个楔形,另一个创建多个具有不同起始角度的楔形:

st_wedge <- function(x,y,r,start,width,n=20){
    theta = seq(start, start+width, length=n)
    xarc = x + r*sin(theta)
    yarc = y + r*cos(theta)
    xc = c(x, xarc, x)
    yc = c(y, yarc, y)
    st_polygon(list(cbind(xc,yc)))   
}

st_wedges <- function(x, y, r, nsegs){
    width = (2*pi)/nsegs
    starts = (1:nsegs)*width
    polys = lapply(starts, function(s){st_wedge(x,y,r,s,width)})
    mpoly = st_cast(do.call(st_sfc, polys), "MULTIPOLYGON")
    mpoly
}
Run Code Online (Sandbox Code Playgroud)

然后做这样的事情来得到五个以半径 10 的 5,1 为中心的楔子:

> w5 = st_wedges(5,1,10,5)
> plot(w5)
> class(w5)
[1] "sfc_MULTIPOLYGON" "sfc"             
> axis(1)
> axis(2)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明