为R中的空间线生成邻居列表对象

问题描述 投票:0回答:1

我的目标是在道路网络中的空间线之间生成邻域关系的对象。如果我的数据是空间多边形,则可以使用spdep::poly2nb来执行此操作,但是我很难确定如何对空间线执行此操作。

在下面的示例中,我尝试使用igraph::as_adjacency_matrix创建一个邻接矩阵,然后使用spdep::mat2listw将其转换为邻居列表对象。但这是正确的方法吗?

一旦有了邻居列表,我也想用road_id属性标记。

library(sfnetworks)
library(sf)
library(spdep)
library(igraph)

net <- as_sfnetwork(roxel, directed = FALSE) %>% 
  activate("edges") %>% 
  mutate(road_id = row_number()+1000)

# Make adjacency matrix
B_net <- igraph::as_adjacency_matrix(net, edges = TRUE, attr = names)

# Make neighbour list
nb_net <- mat2listw(B_net)$neighbours
# Can't use row.names in mat2listw because how do I set row.names in igraph::as_adjacency_matrix

编辑:在此sfnetworks问题https://github.com/luukvdmeer/sfnetworks/issues/10中使用methof的新方法给出了邻居列表。

net_sf <- st_as_sf(net)

net_neigh <- st_touches(net_sf)

# define ad-hoc function to translate sgbp into nb (as documented in 
# https://r-spatial.github.io/spdep/articles/nb_sf.html#creating-neighbours-using-sf-objects)
as.nb.sgbp <- function(x) {
  attrs <- attributes(x)
  x <- lapply(x, function(i) { if(length(i) == 0L) 0L else i } )
  attributes(x) <- attrs
  class(x) <- "nb"
  x
}

net_nb <- as.nb.sgbp(net_neigh)
net_lw <- nb2listw(net_nb)
r igraph sf spdep
1个回答
0
投票

编辑以显示此sfnetworks问题https://github.com/luukvdmeer/sfnetworks/issues/10中的解决方案>

© www.soinside.com 2019 - 2024. All rights reserved.