shapefile to neural network in R

ぃ、小莉子 提交于 2019-12-10 17:12:40

问题


I need to convert a shapefile (ESRI) of roads type SpatialLinesDataFrame in a neural network in R.

I do not know how to remove nodes or vertices of the shape. Determine the length of each edge between nodes. With these parameters I can create the network using the packet (network).

Summary: Input shapefile for the igraph network in R.

Thank you from the South of Chile.


回答1:


Here is a try --

library(rgdal)
library(igraph)

dsn <- system.file("vectors", package = "rgdal")[1]
sl <- readOGR(dsn=dsn, layer="kiritimati_primary_roads")
lines2xcoord <- function(lns) sapply(lns@Lines, function(l) l@coords[,1])
lines2ycoord <- function(lns) sapply(lns@Lines, function(l) l@coords[,2])

x <- unlist(sapply(sl@lines, lines2xcoord))
y <- unlist(sapply(sl@lines, lines2ycoord))

g <- graph.empty(n=length(x), directed=FALSE)
V(g)$lat <- x
V(g)$lng <- y
e <- c(t(matrix(c(head(V(g),-1),tail(V(g),-1)), ncol=2)))
add.edges(g,e)

Now g is an igraph with the lines. It assumes incorrectly the lines in the shapefile to be connected, though. Also, it does not store lat/lon in this example, but the projected coordinates.




回答2:


The shp2graph package can convert SpatialLinesDataFrame objects into igraph objects. Have a look at the nel2igraph function. Here is an example taken from the help file:

data(ORN)
rtNEL<-readshpnw(rn, ELComputed=TRUE)
#Add the edge length as the weight for graph
igr<-nel2igraph(rtNEL[[2]],rtNEL[[3]],weight=rtNEL[[4]])
plot(igr, vertex.label=NA, vertex.size=2,vertex.size2=2)
#plot(rn)

rn is a SpatialLinesDataFrame object that is first converted into a list object, and then converted into an igraph object.



来源:https://stackoverflow.com/questions/11943208/shapefile-to-neural-network-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!