SpatialLines 到 iGraph 的转换简化了拓扑

SpatialLines to iGraph conversion simplifies topology

我正在尝试从 spatialLinesDataFrame 转换为 igraph 对象,我想我可能会丢失我想保留的信息。 igraph 相当新,所以请多多包涵。下面的例子说明:

# create sldf object
require(sp); require(igraph); require(shp2graph)
d = data.frame(x = c(0,80,100,0,-20,-8,0,3,-10,-5,80,75),
               y = c(0,-10,5,0,14,33,0,-4,-10,-12,-10,5),
               grp = c(1,1,1,2,2,2,3,3,3,3,4,4))

sl = SpatialLines(list(
  Lines(list(Line(d[d$grp == 1,1:2]),
             Line(d[d$grp == 4,1:2])), ID=1),
  Lines(Line(d[d$grp == 2,1:2]), ID=2),
  Lines(Line(d[d$grp == 3,1:2]), ID=3))
)

sldf = SpatialLinesDataFrame(sl, iris[1:3,])
plot(sldf)

现在转换为 igraph 并绘制:

read_sldf = readshpnw(sldf, ELComputed = T)
g = nel2igraph(read_sldf[[2]], read_sldf[[3]], weight=read_sldf[[4]])
plot(g)

第一 spdf 行 (sldf[1,]) 的分支已经丢失,我说得对吗?调用 as_edgelist(g) returns 3 行而不是 4.

只需在 readshpnw 中更改这些选项即可:

# create sldf object
require(sp); require(igraph); require(shp2graph)
d = data.frame(x = c(0,80,100,0,-20,-8,0,3,-10,-5,80,75),
               y = c(0,-10,5,0,14,33,0,-4,-10,-12,-10,5),
               grp = c(1,1,1,2,2,2,3,3,3,3,4,4))

sl = SpatialLines(list(
    Lines(list(Line(d[d$grp == 1,1:2]),
               Line(d[d$grp == 4,1:2])), ID=1),
    Lines(Line(d[d$grp == 2,1:2]), ID=2),
    Lines(Line(d[d$grp == 3,1:2]), ID=3))
)

sldf = SpatialLinesDataFrame(sl, iris[1:3,])
plot(sldf)

nodes = readshpnw(sldf, ELComputed = TRUE, Detailed = TRUE, ea.prop = names(sldf))
g = nel2igraph(nodes[[2]], nodes[[3]])
plot(g)