输出R中igraph网络的shapefile

See*_*een 7 r spatial arcgis igraph

你好我在R中使用igraph库有一个网络

Vertices: 616 
Edges: 6270 
Directed: TRUE 
No graph attributes.
Vertex attributes: name, Lat, Lon.
Edge attributes: V3.
Run Code Online (Sandbox Code Playgroud)

如何使用顶点中的Lat,Lon信息为顶点和边创建两个shapefile?

dig*_*aps 9

您可以使用spmaptools包执行此操作.有方便的功能writePointsShape(),并writeLinesShape()maptools将写入ESRI shape文件格式.

在执行此操作之前,必须从图形顶点提取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)
Run Code Online (Sandbox Code Playgroud)

现在,SpatialPoints为顶点创建对象

library(sp)
library(maptools)

## Create SpatialPoints object containing coordinates
xV <- SpatialPoints(cbind(V(x)$lon, V(x)$lat))

## Write vertices to a shapefile
writePointsShape(xV, fn="vertices")
Run Code Online (Sandbox Code Playgroud)

最后,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")
Run Code Online (Sandbox Code Playgroud)