为自然地球 R 创建一个面具,给出意想不到的结果

Creating a mask for natural earth R giving unexpected results

我想创建一个遮罩来为图像中不在指定土地区域内的所有部分着色。我可以让它在两个国家(例如布基纳法索和尼日利亚)成功运作。但是,当我在同一地区尝试更长的 9 个国家/地区列表时,相同的代码不起作用。我收到的错误是: Error in [[<-.data.frame(tmp, attr(x, "sf_column"), value = list( : replacement has 2 rows, data has 1

作为 GIS 的新手,我不太确定问题是什么或如何解决。如果人们能提供一些见解,我将不胜感激。我使用的方法基于

接受的答案

相关代码如下。提前感谢任何能够提供帮助的人。

这段代码工作得很好

library(sf)
library(dplyr)
library(tidyverse)
library(rnaturalearth)
library(rnaturalearthdata)

# 
x_coord = c(-20,-20,27,27)
y_coord = c(-1,28,28,-1)

# Create rectalgular polygon for a mask (slightly bigger than the image)
polygon <-  cbind(x_coord, y_coord) %>%
  st_linestring() %>%
  st_cast("POLYGON") %>%
  st_sfc(crs = 4326, check_ring_dir = TRUE) %>%
  st_sf() %>%
  st_wrap_dateline()

land = ne_countries(scale = "medium", 
             returnclass = "sf") %>% 
     filter(admin %in% c("Nigeria", "Burkina Faso"))
    #filter(admin %in% c("Niger", "Burkina Faso", "Nigeria", "Mali", "Chad", "Mauritania",
    #              "Gambia", "Senegal", "Guinea", "Cameroon"))


# Get the difference between the bounding box and the area I want to plot
# This works fine when using only two countries 
land = st_union(land)
polygon_land_diff = st_difference(polygon, land)

# Plot the data 
plot(st_geometry(land))
plot(st_geometry(polygon), add=TRUE)
plot(st_geometry(polygon_land_diff),add=TRUE, col="green")

此代码产生错误,我不确定如何解决它。唯一的区别是选择的国家数量

x_coord = c(-20,-20,27,27)
y_coord = c(-1,28,28,-1)

# Create rectalgular polygon for a mask (slightly bigger than the image)
polygon <-  cbind(x_coord, y_coord) %>%
  st_linestring() %>%
  st_cast("POLYGON") %>%
  st_sfc(crs = 4326, check_ring_dir = TRUE) %>%
  st_sf() %>%
  st_wrap_dateline()

land = ne_countries(scale = "medium", 
             returnclass = "sf") %>% 
    filter(admin %in% c("Niger", "Burkina Faso", "Nigeria", "Mali", "Chad", "Mauritania",
                  "Gambia", "Senegal", "Guinea", "Cameroon"))


# get the difference between the bounding box and the area I want to plot
# not quite working. The rror occurs when running  `polygon_land_diff = st_difference(polygon, land)`

land = st_union(land)
polygon_land_diff = st_difference(polygon, land)

# Plot the data 
plot(st_geometry(land))
plot(st_geometry(polygon), add=TRUE)
plot(st_geometry(polygon_land_diff),add=TRUE, col="green")

st_difference() 是通用函数,我猜它在后一种情况下未能选择合适的方法。 (我没看源码,这是估计的。)
也许它选择的不是 sfc 而是 sf 方法。 (因为 Polygon 是 class sf
您可以通过制作多边形 sfc class 或直接使用 sf:::st_difference.sfc 来获得所需的输出。 下面是一个例子;

x_coord = c(-20,-20,27,27)
y_coord = c(-1,28,28,-1)


polygon <-  cbind(x_coord, y_coord) %>%
  st_linestring() %>%
  st_cast("POLYGON") %>%
  st_sfc(crs = 4326, check_ring_dir = TRUE)# %>%  # keep sfc class
#  st_sf() %>%
#  st_wrap_dateline()

land = ne_countries(scale = "medium", 
                    returnclass = "sf") %>% 
  filter(admin %in% c("Niger", "Burkina Faso", "Nigeria", "Mali", "Chad", "Mauritania",
                      "Gambia", "Senegal", "Guinea", "Cameroon"))

land = st_union(land)
polygon_land_diff = st_difference(polygon, land)

# Plot the data 
plot(st_geometry(land))
plot(st_geometry(polygon), add=TRUE)
plot(st_geometry(polygon_land_diff),add=TRUE, col="green")