如何将一个圆分成带有sf和R的相等MULTIPOLYGON“切片”?

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

我有这个圈子:

library(sf)
p <- st_sfc(st_point(c(0, 1))) 
circle <- st_buffer(p, dist = 1)  
plot(circle)

sf_circle

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

r sf
1个回答
0
投票

采用这两个功能,一个在给定圆弧部分的中心,半径,起始角度,宽度和截面数量的情况下创建单个楔形,另一个在多个起始角度不同的情况下创建多个楔形:

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
}

然后执行类似的操作,以使五个楔形居中,半径为10,5,1:

> w5 = st_wedges(5,1,10,5)
> plot(w5)
> class(w5)
[1] "sfc_MULTIPOLYGON" "sfc"             
> axis(1)
> axis(2)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.