R中igraph网络的输出shapefile

R中igraph网络的输出shapefile,r,spatial,arcgis,igraph,R,Spatial,Arcgis,Igraph,您好,我在R有一个使用igraph库的网络 Vertices: 616 Edges: 6270 Directed: TRUE No graph attributes. Vertex attributes: name, Lat, Lon. Edge attributes: V3. 如何使用顶点中的Lat、Lon信息为顶点和边生成两个形状文件 您可以使用sp和maptools软件包执行此操作。maptools中有方便的函数writePointsShape()和writeLinesShape(

您好,我在R有一个使用igraph库的网络

Vertices: 616 
Edges: 6270 
Directed: TRUE 
No graph attributes.
Vertex attributes: name, Lat, Lon.
Edge attributes: V3.

如何使用顶点中的Lat、Lon信息为顶点和边生成两个形状文件

您可以使用
sp
maptools
软件包执行此操作。
maptools
中有方便的函数
writePointsShape()
writeLinesShape()
将写入ESRI形状文件格式

在执行此操作之前,需要从图形顶点提取lat/lon信息,并将其放入顶点的
SpatialPoints
对象和边的
SpatialLinesDataFrame
对象中

此代码为以下示例生成一个非常简单的
igraph
对象:

library(igraph)

## Produce a ring graph with 4 vertices
x <- graph.ring(4)

## Add lat/lon information to vertices
V(x)$lat <- c(50, 50, 51, 51)
V(x)$lon <- c(40, 41, 41, 40)
最后,为边创建
SpatialLinesDataFrame
对象。这有点混乱,但我还没有找到一种快速的方法来生成给定坐标的SpatialLines对象

## Create SpatialLinesDataFrame object describing edges
edges <- get.edgelist(x)+1
edges <- cbind(edgeNum=1:nrow(edges), v1=edges[,1], v2=edges[,2])
xE <- apply(edges, 1, function(i) Lines(Line(cbind(c(V(x)$lon[i["v1"]], V(x)$lon[i["v2"]]), c(V(x)$lat[i["v1"]], V(x)$lat[i["v2"]]))), ID=as.character(i["edgeNum"])))
xE <- SpatialLinesDataFrame(SpatialLines(xE), data=data.frame(edgeNum=1:nrow(edges)))

## Write edges to a shapefile
writeLinesShape(xE, fn="edges")
##创建描述边的SpatialLinesDataFrame对象

非常感谢。我现在就去试试。把Lat和Lon调换了。现在更正。跟我重复:(Lon=X,Lat=Y):)validityMethod(对象)中的错误:coords不能包含缺少的值。这就是我如何得到该行的“apply”函数。你有什么建议我可以检查这些值吗。对不起,我是R方面的新手…我假设您在616顶点网络上运行此操作,而不是给定的示例4顶点环。听起来有些顶点没有纬度/经度信息。您可以使用
print(yourGraph,vertex=T)
进行检查,最终我成功了!谢谢剩下的最后一个问题是区分图的边的方向。。。。
## Create SpatialLinesDataFrame object describing edges
edges <- get.edgelist(x)+1
edges <- cbind(edgeNum=1:nrow(edges), v1=edges[,1], v2=edges[,2])
xE <- apply(edges, 1, function(i) Lines(Line(cbind(c(V(x)$lon[i["v1"]], V(x)$lon[i["v2"]]), c(V(x)$lat[i["v1"]], V(x)$lat[i["v2"]]))), ID=as.character(i["edgeNum"])))
xE <- SpatialLinesDataFrame(SpatialLines(xE), data=data.frame(edgeNum=1:nrow(edges)))

## Write edges to a shapefile
writeLinesShape(xE, fn="edges")