在下面的代码中,我首先从两条水平线和两条垂直线创建一个矩形。然后,我使用st_polygonize
创建一个多边形。最后,我使用st_cast
创建类的对象
[1]“ XY”“ POLYGON”“ sfg”
这就是我所需要的。但是,当我尝试对wave执行类似操作时,它会失败。我通过复制波浪线,将其向上移动并添加边来创建波浪多边形。
wave_gc
表示可以,但是执行时:
wave_poly <- class(st_cast(wave_gc, "POLYGON"))
wave_poly为空。为什么是这样?我如何以类
结束[1]“ XY”“ POLYGON”“ sfg”
有效吗?
library(sf)
library(transformr)
# Rectangle #########################
# two vertical lines
v_lines <- st_multilinestring(list(rbind(c(0,0),c(0,1)), rbind(c(3,0),c(3,1))))
# two horizontal lines
h_lines <- st_multilinestring(list(rbind(c(0,0),c(3,0)), rbind(c(0,1),c(3,1))))
all_lines <- c(v_lines, h_lines)
rect_gc <- st_polygonize(all_lines)
rect_poly <- st_cast(rect_gc, "POLYGON")
class(rect_gc)
# [1] "XY" "GEOMETRYCOLLECTION" "sfg"
class(rect_poly)
# [1] "XY" "POLYGON" "sfg"
rect_poly
#################################################################
# Wave
################################################################
w1 <- path_waves(st = TRUE)[[1]]
w2 <- w1
# Add to second column, y values
w2[,2] <- w2[,2] + 0.1
# Vertical bars
nr <- nrow(w1)
v_bars <- st_multilinestring(list(rbind(c(w1[1, 1], w1[1, 2]), c(w2[1, 1], w2[1, 2])),
rbind(c(w1[nr, 1], w1[nr, 2]), c(w2[nr, 1], w2[nr, 2]))))
two_waves <- st_multilinestring(list(w1, w2))
full_wave <- c(v_bars, two_waves)
wave_gc = st_polygonize(full_wave)
wave_poly <- class(st_cast(wave_gc, "POLYGON"))
class(wave_gc)
# [1] "XY" "GEOMETRYCOLLECTION" "sfg"
plot(wave_gc)
wave_poly
# [1] "XY" "POLYGON" "sfg"