Aly*_*a C 6 mapping maps r geospatial rnaturalearth
我正在尝试创建一个包含多边形的地图,这些多边形代表从每个国家/地区的海岸线到海洋的 25 公里缓冲区,以便我可以计算每个国家/地区的缓冲区内的面积。我在使用上传的海岸线文件和自然地球数据执行此操作时遇到困难。到目前为止,这是我尝试过的:
library(rgdal)
library(sf)
library(rnaturalearthdata)
library(rnaturalearth)
library(rgeos)
library(maps)
library(countrycode)
# World polygons from the maps package
world_shp <- sf::st_as_sf(maps::map("world", plot = T, fill = TRUE))
world_shp_df <- world_shp %>%
mutate(Alpha.3.code = countrycode(ID, "country.name", "iso3c", warn = FALSE))
# Get coastlines using rnaturalearth
coastline <- ne_coastline(scale = "medium", returnclass = "sf")
# Buffer the coastlines by 25 km
buffer_distance <- 25000 # 25 km in meters
coastline_buffers <- st_buffer(coastline, dist = buffer_distance) %>%
st_make_valid()
ggplot() +
geom_sf(data = coastline_buffers , fill = "lightblue")
Run Code Online (Sandbox Code Playgroud)
这会产生一个带有水平线的地图,请参见图像:
我尝试过使用不同的 crs 来简化几何形状,但我似乎无法弄清楚。任何帮助将非常感激!
当缓冲区跨越 180\xc2\xb0 纬度时,st_buffer将纬度限制为 -180\xc2\xb0,导致缓冲区多边形内部和外部的内容反转,从而产生水平条纹。
带条纹的多边形可以通过它们的边界框进行排序,因为它们xmin = -180:
stripes <- sapply(coastline_buffers$geometry,\\(x) {unname(st_bbox(x)$xmin == -180)})\nwhich(stripes)\n#[1] 1 61 138 297 298 299 811 1352 1353 1387 1388 1404\n\nggplot() +\n geom_sf(data = coastline_buffers[stripes,] , fill = "lightblue")\nRun Code Online (Sandbox Code Playgroud)\n\n由于错误点并不多,因此快速解决方法是从多边形中删除这些错误点:
\n# Create a band from latitudes -175\xc2\xb0 to 180\xc2\xb0 \ncleanup <- data.frame(lon=c(-175,-175,180,180,-175),lat=c(90,-90,-90,90,90)) %>% \n st_as_sf(coords = c("lon", "lat"), \n crs = st_crs(p[1,])) %>% \n st_bbox() %>% \n st_as_sfc()\n\n# Remove points in this band\ncoastline_buffers_cleaned <- st_difference(coastline_buffers,cleanup)\n\n\nggplot() +\n geom_sf(data = coastline_buffers_cleaned , fill = "lightblue")\nRun Code Online (Sandbox Code Playgroud)\n\n这种清理并不是 100% 准确,因为地图两端的一些沿海区域消失了,但条纹已经消失,并且与总缓冲区相比,删除的区域可以忽略不计。
\n