【问题标题】:Returning the boundary of a set of points in R as a polygon?将R中一组点的边界作为多边形返回?
【发布时间】: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


    【解决方案1】:

    我建议你考虑{concaveman} 包;在您的玩具示例的特定情况下,它似乎工作得很好。您可能需要稍微调整concavity 参数的值。

    我无法评论它将在您的实际数据上执行得有多好(或多快),但它应该给您一个开始。

    library(tidyverse)
    
    t <- 
      crossing(x=1:5, y=1:5) %>% 
      filter(!(x > 3 & y > 3)) %>% 
      sf::st_as_sf(coords=c("x", "y"))
    
    asdf <- t %>% 
      summarise() %>% 
      concaveman::concaveman(concavity = 1)
    
    plot(asdf)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多