【问题标题】:sf dissolve/remove inner borders of POLYGONsf 溶解/移除 POLYGON 的内部边界
【发布时间】:2021-09-26 11:56:58
【问题描述】:

此代码创建两个组合形状的所需轮廓

square.df <- data.frame("x"=c(0,0,1,1,0),
           "y"=c(0,1,1,0,0),
           "ID"=rep("square",5))
square <- st_cast(st_combine(st_as_sf(square.df,coords = c("x", "y"))),"POLYGON")
plot(square)

triangle.df <- data.frame("x"=c(0,1,0.5,0),
                        "y"=c(1,1,1.5,1),
                        "ID"=rep("triangle",4))
triangle <- st_cast(st_combine(st_as_sf(triangle.df,coords = c("x", "y"))),"POLYGON")
plot(triangle)

plot(st_union(triangle,square))

但是如果我的数据框有多个形状,像这样呢?

shapes.df <- rbind(square.df,triangle.df)
shapes <- st_cast(st_combine(st_as_sf(shapes.df,coords = c("x", "y"))),"POLYGON")
plot(shapes)

我要创建什么 sf 对象以及如何获得组合多边形的轮廓?

【问题讨论】:

    标签: r spatial sf sp


    【解决方案1】:

    这是一个有趣的问题;直接的答案是考虑sf::st_union() - 但您的形状对象无效;您必须先致电sf::st_make_valid() 修复它。

    然后(并且只有这样)st_union 将按预期工作。

    st_is_valid(shapes)
    [1] FALSE
    
    house <- shapes %>% 
      st_make_valid() %>% 
      st_union()
    
    plot(house, col = "red")
    

    【讨论】:

    • 谢谢。比我的循环破解更有效率!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    相关资源
    最近更新 更多