sf 溶解/删除 POLYGON 的内边界

Mar*_*k R 3 r spatial r-sp r-sf

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

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))
Run Code Online (Sandbox Code Playgroud)

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

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

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

Jin*_*cko 5

这是一个有趣的问题;直接的答案是考虑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")
Run Code Online (Sandbox Code Playgroud)

房子图像