将一个空间多边形用一条线分成两个多边形。

huangapple go评论88阅读模式
英文:

Split a spatial polygon into two polygons with a line

问题

我想要取一条线,并将其用于将一个多边形分割成多个多边形,或者在原始多边形中创建两个单独的命名区域(如果可能的话)。最终目标是使落入两个区域之一的点,然后绘制填充为该区域中的点数的多边形。

我已经尝试了一段时间使用sf和terra。欢迎任何方法来完成这个任务。

  1. library(sf)
  2. # 创建一个多边形和一条线
  3. poly <- st_polygon(list(rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0))))
  4. line <- st_linestring(rbind(c(0.5, -0.5), c(0.5, 1.5)))
  5. # 绘制多边形和线
  6. plot(poly)
  7. plot(line, add = TRUE)
  8. # 使用调整后的线将多边形分割成两部分
  9. poly_split <- st_intersection(poly, line)
  10. # 绘制两个结果多边形
  11. plot(poly_split)
英文:

I want to take a line and use it to split a polygon into multiple polygons, or to create two separate named region in the original polygon (if that's possible). The end goal would be having points that fall into one of the two regions and then plot the polygons where fill = number of points in the region.

I have tried using sf for a while and also terra. Any method of doing this would be appreciated.

  1. library(sf)
  2. # create a polygon and a line
  3. poly &lt;- st_polygon(list(rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0))))
  4. line &lt;- st_linestring(rbind(c(0.5, -0.5), c(0.5, 1.5)))
  5. # plot the polygon and line
  6. plot(poly)
  7. plot(line, add = TRUE)
  8. # split the polygon into two using the adjusted line
  9. poly_split &lt;- st_intersection(poly, line)
  10. # plot the two resulting polygons
  11. plot(poly_split)

答案1

得分: 1

对于这个简单的案例,你可以这样做:

  1. library(terra)
  2. splitp <- function(pol, lin) {
  3. x <- rbind(as.lines(pol), lin)
  4. a <- aggregate(x)
  5. m <- makeNodes(a)
  6. as.polygons(m)
  7. }
  8. library(terra)
  9. poly <- vect(rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0)), "poly")
  10. line <- vect(rbind(c(0.5, -0.5), c(0.5, 1.5)), "line")
  11. p <- splitp(poly, line)
  12. plot(p, col=c("blue", "red"))

使用 terra 1.7-23(目前的开发版本),你可以使用 split

  1. library(terra)
  2. # terra 1.7.23
  3. poly <- vect(rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0)), "poly")
  4. line <- vect(rbind(c(0.5, -0.5), c(0.5, 1.5)), "line")
  5. p <- split(poly, line)
  6. plot(p, col=c("blue", "red"))

请注意,我已经保留了代码部分的内容不进行翻译,只提供了翻译好的文字部分。

英文:

For this simple case, you could do

  1. library(terra)
  2. splitp &lt;- function(pol, lin) {
  3. x &lt;- rbind(as.lines(pol), lin)
  4. a &lt;- aggregate(x)
  5. m &lt;- makeNodes(a)
  6. as.polygons(m)
  7. }
  8. library(terra)
  9. poly &lt;- vect(rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0)), &quot;poly&quot;)
  10. line &lt;- vect(rbind(c(0.5, -0.5), c(0.5, 1.5)), &quot;line&quot;)
  11. p &lt;- splitp(poly, line)
  12. plot(p, col=c(&quot;blue&quot;, &quot;red&quot;))

With terra 1.7-23 (currently the development version) you can use split

  1. library(terra)
  2. # terra 1.7.23
  3. poly &lt;- vect(rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0)), &quot;poly&quot;)
  4. line &lt;- vect(rbind(c(0.5, -0.5), c(0.5, 1.5)), &quot;line&quot;)
  5. p &lt;- split(poly, line)
  6. plot(p, col=c(&quot;blue&quot;, &quot;red&quot;))

huangapple
  • 本文由 发表于 2023年4月7日 01:18:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定