您好,我正在使用igraph库在R中进行网络操作。
Vertices: 616
Edges: 6270
Directed: TRUE
No graph attributes.
Vertex attributes: name, Lat, Lon.
Edge attributes: V3.
我如何使用顶点中的纬度和经度信息生成两个形状文件,一个用于顶点,另一个用于边缘?
sp
和maptools
包来完成此操作。 在maptools
中有方便的函数writePointsShape()
和writeLinesShape()
可将其写入ESRI shapefile格式。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)
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")
最后,创建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")
print(yourGraph, vertex=T)
进行检查。 - digitalmaps