【问题标题】:Output shapefile for the igraph network in RR中igraph网络的输出shapefile
【发布时间】:2012-03-01 14:11:06
【问题描述】:

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

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

如何使用顶点中的纬度、经度信息为顶点和边生成两个 shapefile?

【问题讨论】:

    标签: r spatial arcgis igraph


    【解决方案1】:

    您可以使用spmaptools 包来执行此操作。 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")
    

    【讨论】:

    • 非常感谢。我现在就试试。
    • 已经调换了经纬度。现已更正。在我之后重复:(Lon=X,Lat=Y):)
    • 有效性方法(对象)中的错误:坐标不能包含缺失值。这就是我获得该行的“应用”功能的方式。你有什么建议我可以检查这些值吗?抱歉,我是 R 的新手...
    • 我假设您在 616 顶点网络上运行它,而不是给出的示例 4 顶点环。听起来有些顶点没有纬度/经度信息。您可以使用print(yourGraph, vertex=T) 进行检查
    • 终于成功了!谢谢!剩下的最后一个问题是区分图形边缘的方向......
    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2017-02-15
    相关资源
    最近更新 更多