在R中输出igraph网络的shapefile [英] Output shapefile for the igraph network in R

查看:174
本文介绍了在R中输出igraph网络的shapefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello I have a network in R using the igraph library

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

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

How can I generate two shapefiles for the Vertices and the Edges using the Lat, Lon info in the vertex?

推荐答案

您可以使用spmaptools软件包进行此操作. maptools中有便捷的功能writePointsShape()writeLinesShape(),它们将写入ESRI shapefile格式.

You can do this using the sp and maptools packages. There are handy functions writePointsShape() and writeLinesShape() in maptools that will write to the ESRI shapefile format.

在执行此操作之前,有必要从图形顶点提取纬度/经度信息,并将其放入顶点的SpatialPoints对象和边缘的SpatialLinesDataFrame对象.

Before doing this, it is necessary to extract the lat/lon information from the graph vertices and put it into a SpatialPoints object for the vertices, and a SpatialLinesDataFrame object for the edges.

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

This code produces a very simple igraph object for the following example:

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对象的快速方法.

Finally, create the SpatialLinesDataFrame object for the edges. This is a little messy, but I am yet to find a quick way to produce a SpatialLines object given coordinates.

## 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")

这篇关于在R中输出igraph网络的shapefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆