如何使用 sf::st_intersects 从邻域列表中过滤掉每个多边形

How to filter out each polygon from its list of neighbors with sf::st_intersects

我需要为 sf 数据集中的每个多边形提取相邻多边形。

这是一个简单的例子:

library(tidyverse)
library(sf)

demo(nc, ask = FALSE, verbose = FALSE)
nc <- nc %>% 
  mutate(polygon_id = row_number())

我已经设法用 sf::st_intersects

提取邻居
neighbors <- st_intersects(nc, nc)
neighbors[[5]]
[1]  5  6  9 16 28

问题是每个多边形(这里是 5 个)都包含在邻域列表中。仅使用一个 nc 数据集得到相同的结果

neighbors <- st_intersects(nc)
neighbors[[5]]
[1]  5  6  9 16 28

关于如何从 adjacent/neighboring 多边形列表中过滤出实际多边形的任何提示?

好问题。这个问题可以有很多解。但是这个问题的简单答案:"Any tips on how to filter out the actual polygon from the list of adjacent/neighboring polygons?",是使用 Jupyter Lab IDE 和 R 内核完成的。以下代码提供了一种回答问题的方法。

nc 数据集中有 100 个县。此代码以颜色显示所选县并显示所有邻近县。此代码适用于北卡罗来纳州 100 个县中的任何一个。这里选了县100

代码:

nc1 <- nc %>% mutate(c_id = 1:nrow(nc))        
n = 100                  
grp <- st_intersects(nc1, nc1[n,1]   , sparse = F ) 

neighborhood <- nc1[grp,]
neighborhood 

plot(neighborhood$geom)
plot(nc1[n,1], col = 'blue', add = TRUE)   #

这段代码很容易扩展。我写了一个显示邻国名称的快速小函数(此处未显示),但这个问题似乎很可能是一个与绘图相关的问题。

情节显示在Link