微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

按非空间维度汇总可防止去除未溶解的斑点

如何解决按非空间维度汇总可防止去除未溶解的斑点

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)

enter image description here

#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)

enter image description here

#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)

enter image description here

您最终会绘制出两个相互重叠的多边形,没有斑点。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。