4

我需要SpatialLinesDataFrame在 R 的神经网络中转换道路类型的 shapefile (ESRI)。

我不知道如何删除形状的节点或顶点。确定节点之间每条边的长度。使用这些参数,我可以使用数据包(网络)创建网络。

摘要:R 中 igraph 网络的输入 shapefile。

谢谢你来自智利南部。

4

2 回答 2

1

这里试一试——

library(rgdal)
library(igraph)

dsn <- system.file("vectors", package = "rgdal")[1]
sl <- readOGR(dsn=dsn, layer="kiritimati_primary_roads")
lines2xcoord <- function(lns) sapply(lns@Lines, function(l) l@coords[,1])
lines2ycoord <- function(lns) sapply(lns@Lines, function(l) l@coords[,2])

x <- unlist(sapply(sl@lines, lines2xcoord))
y <- unlist(sapply(sl@lines, lines2ycoord))

g <- graph.empty(n=length(x), directed=FALSE)
V(g)$lat <- x
V(g)$lng <- y
e <- c(t(matrix(c(head(V(g),-1),tail(V(g),-1)), ncol=2)))
add.edges(g,e)

现在g是带有线条的 igraph。但是,它错误地假定要连接 shapefile 中的线。此外,在此示例中,它不存储纬度/经度,而是投影坐标。

于 2012-12-12T00:32:43.947 回答
1

shp2graph包可以将对象转换SpatialLinesDataFrame为 igraph 对象。看看nel2igraph功能。以下是从帮助文件中获取的示例:

data(ORN)
rtNEL<-readshpnw(rn, ELComputed=TRUE)
#Add the edge length as the weight for graph
igr<-nel2igraph(rtNEL[[2]],rtNEL[[3]],weight=rtNEL[[4]])
plot(igr, vertex.label=NA, vertex.size=2,vertex.size2=2)
#plot(rn)

rnSpatialLinesDataFrame先转换成list对象,再转换成igraph对象的对象。

于 2018-04-05T13:50:27.587 回答