问题描述
This shape 在溶解内部边界后保留残留的斑点或碎屑。当非空间维度(时间)包含在溶解/聚合过程中时,包 rmapshaper
无法去除斑点。是否可以去除斑点(有或没有 rmapshaper
),希望不会单独溶解和聚合然后合并结果?
library(sf)
library(dplyr)
library(rmapshaper)
#download file "shape.txt" from Pastebin link above
shape <- dget("shape.txt")
class(shape)
[1] "sf" "data.frame"
plot(shape$geometry)
#aggregate with year
shape1 <- shape %>% group_by(a1,year) %>% summarise(count=sum(count)) %>% ungroup()
#aggregate without year
shape2 <- shape %>% group_by(a1) %>% summarise(count=sum(count)) %>% ungroup()
#results contain specks
plot(shape1$geometry)
plot(shape2$geometry)
#remove specks: unsuccessful
shape1 <- ms_filter_islands(shape1,min_area=1000000)
plot(shape1$geometry)
#remove specks: successful
shape2 <- ms_filter_islands(shape2,min_area=1000000)
plot(shape2$geometry)
解决方法
首先:您的示例中按年份分组感觉不对。这些区域看起来像行政单位,最终会绘制出两个相互叠加的汇总多边形。
在理想情况下,我可能会将几何对象与年度数据分开。
但话虽如此,我会将您的示例视为一个玩具示例,旨在演示一个问题,而不是使其本身有意义。
考虑一下这段代码:由于斑点很小,因此可以通过将原始多边形缓冲 10 个单位,然后将最终多边形缓冲相同的数量来移除它们(或者更确切地说是避免创建它们)。
>library(sf)
library(dplyr)
#download file "shape.txt" from Pastebin link above
shape <- dget("shape.txt")
# buffer & unbuffer
shape2 <- shape %>%
st_buffer(10) %>% # first create a small buffer ...
group_by(a1,year) %>%
summarise(count=sum(count)) %>%
ungroup() %>%
st_buffer(-10) # ... and then remove it to preserve area
plot(shape2$geometry)
您最终会绘制出两个相互重叠的多边形,没有斑点。