假设我们有两个应无缝连接的shapefile。只是,他们不是。有没有办法迫使他们在没有间隙的情况下坚持彼此?
我有两个shapefile:一个用于欧洲地区 - REG
,另一个用于邻国 - NEI
。两个shapefile都取自Eurostat repository并且应该很好地融合在一起;但是差距很小。此外,我需要简化多边形,然后差距变得非常显着。
我尝试了几种方法但没有成功。我看到实现所需结果的唯一方法需要执行以下步骤:
NEI
; NEI
shapefile剪切展开的REG
。显然,这是一个相当笨拙的解决方法。
# install dev version of ggplot2
devtools::dev_mode()
devtools::install_github("tidyverse/ggplot2")
library(tidyverse)
library(sf)
library(rmapshaper)
library(ggthemes)
# load data
source(file = url("https://gist.githubusercontent.com/ikashnitsky/4b92f6b9f4bcbd8b2190fb0796fd1ec0/raw/1e281b7bb8ec74c9c9989fe50a87b6021ddbad03/minimal-data.R"))
# test how good they fit together
ggplot() +
geom_sf(data = REG, color = "black", size = .2, fill = NA) +
geom_sf(data = NEI, color = "red", size = .2, fill = NA)+
coord_sf(datum = NA)+
theme_map()
ggsave("test-1.pdf", width = 12, height = 10)
# simplify
REGs <- REG %>% ms_simplify(keep = .5, keep_shapes = TRUE)
NEIs <- NEI %>% ms_simplify(keep = .5, keep_shapes = TRUE)
ggplot() +
geom_sf(data = REGs, color = "black", size = .2, fill = NA) +
geom_sf(data = NEIs, color = "red", size = .2, fill = NA)+
coord_sf(datum = NA)+
theme_map()
ggsave("test-2.pdf", width = 12, height = 10)
答案 0 :(得分:1)
ms_simplify
似乎适用于您的最小示例,但您首先需要将2个“shapefile”分组为一个“shapefile”。如果需要,在简化边界后将它们分开是很容易的
(注意:当rmapshaper
与ms_simplify
对象一起使用时,我的sf
版本会返回错误。这就是我将tmp
对象转换为sp
的原因对象as(tmp, "Spatial")
)
NEI <- st_transform(NEI, st_crs(REG)$epsg)
tmp <- rbind(REG , NEI)
tmp <- ms_simplify(as(tmp, "Spatial"), keep = .1, keep_shapes = T)
ggplot() + geom_sf(data = st_as_sf(tmp)) + theme_bw()