world我正在尝试使用极坐标投影的数据集制作地图spData。一切看起来都很棒,除了俄罗斯境内有穿过 180 度子午线的内部边界,而且似乎st_union()不会溶解跨过该边界的多边形,至少在我使用它时是这样。如何消除边界以使它们不会显示在我的地图上?我更喜欢使用的答案sf。
library(sf)
library(spData)
library(dplyr)
library(ggplot2)
# polygon to crop the countries to only the northernmost ones
crop_poly <- tibble(geometry = st_sfc(st_point(c(0, 90)),
crs = 'EPSG:4326')) %>%
st_sf() %>%
st_transform(crs = 'EPSG:3413') %>%
st_buffer(dist = 3909167) %>%
smoothr::densify(n = 3) %>%
st_transform(crs = 'EPSG:4326')
# crop the world dataset
world_north <- world %>%
st_intersection(crop_poly) %>%
st_transform(crs = 'EPSG:3413') %>%
select(name_long, continent, region_un, subregion) %>%
st_union(by_feature = TRUE) # this is the suggested method for dissolving internal boundaries
# make the map
ggplot(world_north) +
geom_sf(data = crop_poly,
color = 'transparent',
fill = 'gray90',
alpha = 0.5) +
geom_sf(color = 'black', fill = 'gray80') +
geom_sf(data = crop_poly,
color = 'gray70',
fill = 'transparent',
inherit.aes = FALSE) +
# add an arrow to highlight location of undesired internal boundaries
geom_segment(x = 0, xend = -1300000,
y = 0, yend = 1300000,
arrow = arrow(),
color = 'red') +
theme_bw()
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用st_wrap_dateline(),虽然我不太确定这是否是一个有用的应用程序,但我无法找到解决问题的方法。我尝试使用这篇文章st_union()中的建议来解决我的问题,但是当我尝试 EPSG:32635、和的各种组合时,我没有看到任何差异(内部边界没有溶解)st_simplify()。为了保持这篇文章的可读性,我尝试过的各种代码排列未包含在此处。
俄罗斯是数据中的第四个特征,为了方便起见,我们将其取出:
russia = world_north$geom[4]
Run Code Online (Sandbox Code Playgroud)
它是一个多重多边形:
> russia
Geometry set for 1 feature
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -2235484 ymin: -1622414 xmax: 3909167 ymax: 3909167
Projected CRS: WGS 84 / NSIDC Sea Ice Polar Stereographic North
MULTIPOLYGON (((-1448716 1400198, -1507001 1384...
>
Run Code Online (Sandbox Code Playgroud)
因此,让我们将其拆分以查看各个多边形:
u = st_cast(russia,"POLYGON")
Run Code Online (Sandbox Code Playgroud)
并且(通过各种检查,例如最终plot(u[c(5,12)]))我们可以得到两个部分之间跨越 180 纬度线的距离:
> st_distance(u[c(5,12)])
Units: [m]
[,1] [,2]
[1,] 0.0000000 0.4020763
[2,] 0.4020763 0.0000000
Run Code Online (Sandbox Code Playgroud)
告诉我们它们之间有40.2厘米的间隙,所以它们不会溶解。这可能是一块非常重要的 40.2 厘米宽的区域。或者数值精度问题。
如果您不介意冒发生国际事件的风险,您可以向俄罗斯边界全面添加 25 厘米,然后当sf重建多多边形几何体使其有效时,该线就会消失 - 不需要联合(地缘政治影响较小的解决方案是编辑顶点级别的多边形,只需更改连接处的一些点 - 易于与 GIS 包交互地完成):
plot(russia)
plot(st_buffer(russia,0.25))
Run Code Online (Sandbox Code Playgroud)
这也去掉了小岛上180的边界,我怀疑这些部分也相距厘米。
您可以将其直接修复到数据框中:
world_north$geom[4] = st_buffer(world_north$geom[4],.25)
Run Code Online (Sandbox Code Playgroud)
我们就完成了: