【发布时间】:2021-06-28 07:20:22
【问题描述】:
我试图返回一组点的边界(如下所示)。我使用 {sf} 包尝试了三种方法。
创建测试数据
t <-
crossing(x=1:5, y=1:5) %>%
filter(!(x > 3 & y > 3)) %>%
st_as_sf(coords=c("x", "y"))
我想要什么
ggplot()+
geom_sf(data=t)+
geom_segment(aes(x = c(1, 1, 3, 3, 5, 5),
xend = c(1, 3, 3, 5, 5, 1),
y = c(1, 5, 5, 3, 3, 1),
yend = c(5, 5, 3, 3, 1, 1),
col = "What I want"))
我尝试过的
ggplot()+
geom_sf(data=t)+
geom_sf(data = t %>% st_union() %>% st_convex_hull(), aes(col="st_convex_hull"), fill=NA)+
geom_sf(data = t %>% st_union() %>% st_cast("POLYGON") %>% st_boundary(), aes(col = "st_boundary"))+
geom_sf(data =
t %>%
st_union() %>% .
st_buffer(0.5, endCapStyle = "SQUARE") %>%
st_buffer(-0.5, endCapStyle = "SQUARE"),
aes(col="st_buffer twice"), fill=NA)
我想我可能会使用 st_buffer() 两次,但它仍然给出了一个弯曲的轮廓。此外,抛开曲线的问题,它在我的真实数据(包含数十万个点)上运行非常缓慢。
【问题讨论】:
标签: r polygon spatial sf boundary