You can do this using sp
and maptools
. maptools
has convenient writePointsShape()
and writeLinesShape()
functions that will be written in the ESRI shapefile format.
Before doing this, you need to extract the lat / lon information from the vertices of the graph and place it in the SpatialPoints
object for the vertices and the SpatialLinesDataFrame
object for the edges.
This code creates a very simple igraph
object for the following example:
library(igraph)
Now create a SpatialPoints
object for the vertices
library(sp) library(maptools)
Finally, create a SpatialLinesDataFrame
object for the edges. This is a bit dirty, but I have yet to find a quick way to create the SpatialLines object given by the 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")
source share